How to insert geojson data to geometry field in postgresql

点点圈 提交于 2021-02-08 10:01:54

问题


I want to insert geoJSON to a geometry column of a table. I have already inserted CSV file to the same column following this tutorial, I wonder how to insert the geoJSON to any geometry column? I tried following this answer but could not get what is going on there.


回答1:


Just use an update with the function ST_GeomFromGeoJSON:

UPDATE mytable SET geom = ST_GeomFromGeoJSON(json_column);

The following example inserts a GeoJSON point into a JSON column and afterwards updates the geometry column with the above mentioned function.

CREATE TEMPORARY TABLE mytable(
json_column json,
geom geometry);

INSERT INTO mytable (json_column) VALUES ('{
    "type": "Point",
    "coordinates": [7.0069, 51.1623]
}'); 

UPDATE mytable SET geom = ST_GeomFromGeoJSON(json_column);

SELECT * FROM mytable;

             json_column              |                    geom                    
--------------------------------------+--------------------------------------------
 {                                   +| 01010000009E5E29CB10071C400612143FC6944940
     "type": "Point",                +| 
     "coordinates": [7.0069, 51.1623]+| 
 }                                    | 
(1 Zeile)


来源:https://stackoverflow.com/questions/60039007/how-to-insert-geojson-data-to-geometry-field-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!