简体   繁体   中英

Polygon coordinates to PostgreSQL/PostGIS

I have a geojson file of polygon coordinates. Trying to insert the coordinates into postgreSQL database in python using the psycopg2 api. Here are the coordinates I am working with

"geometry": { "type": "Polygon", "coordinates": [ [ [ -74.0407457, 40.7130407 ], [ -74.0406424, 40.7130291 ], [ -74.0406471, 40.7130128 ], [ -74.0406625, 40.7130141 ], [ -74.0406659, 40.7130016 ], [ -74.0406531, 40.7129996 ], [ -74.0406377, 40.7129826 ], [ -74.0406401, 40.7129721 ], [ -74.04062, 40.7129693 ], [ -74.0406173, 40.7129823 ], [ -74.0405894, 40.7129785 ], [ -74.0405874, 40.7129932 ], [ -74.0405764, 40.7129914 ], [ -74.040574, 40.7129998 ], [ -74.040519, 40.7129937 ], [ -74.0405227, 40.7129752 ], [ -74.0404268, 40.712964 ], [ -74.0404235, 40.7129823 ], [ -74.0403662, 40.7129765 ], [ -74.0403675, 40.7129671 ], [ -74.0403578, 40.712966 ], [ -74.0403605, 40.7129508 ], [ -74.0403303, 40.7129475 ], [ -74.0403326, 40.7129368 ], [ -74.0403165, 40.7129348 ], [ -74.0403149, 40.7129465 ], [ -74.0402931, 40.7129571 ], [ -74.04028, 40.7129551 ], [ -74.040276, 40.7129698 ], [ -74.0402941, 40.7129737 ], [ -74.0402917, 40.7129805 ], [ -74.0403102, 40.7129828 ], [ -74.0403028, 40.7130151 ], [ -74.0403256, 40.7130184 ], [ -74.0403179, 40.7130608 ], [ -74.0403085, 40.7130596 ], [ -74.0403071, 40.7130669 ], [ -74.0402867, 40.7130646 ], [ -74.0402827, 40.7130875 ], [ -74.0402706, 40.7130862 ], [ -74.0402689, 40.7130962 ], [ -74.0402522, 40.7130944 ], [ -74.0402495, 40.7131083 ], [ -74.0402629, 40.7131104 ], [ -74.0402773, 40.7131251 ], [ -74.040275, 40.7131371 ], [ -74.040291, 40.7131388 ], [ -74.0402931, 40.7131256 ], [ -74.0403256, 40.7131292 ], [ -74.0403286, 40.7131144 ], [ -74.0403387, 40.7131155 ], [ -74.0403407, 40.7131078 ], [ -74.040398, 40.7131147 ], [ -74.040394, 40.713131 ], [ -74.0404902, 40.7131434 ], [ -74.0404939, 40.7131272 ], [ -74.0405492, 40.7131327 ], [ -74.0405469, 40.7131401 ], [ -74.0405573, 40.7131414 ], [ -74.0405546, 40.7131546 ], [ -74.0405864, 40.7131594 ], [ -74.0405841, 40.7131709 ], [ -74.0405978, 40.7131726 ], [ -74.0405998, 40.7131635 ], [ -74.040623, 40.7131516 ], [ -74.0406364, 40.7131531 ], [ -74.0406394, 40.7131363 ], [ -74.0406226, 40.7131343 ], [ -74.0406257, 40.7131211 ], [ -74.0407266, 40.713133 ], [ -74.0406843, 40.7133394 ], [ -74.0401087, 40.7132723 ], [ -74.0401036, 40.7132987 ], [ -74.0400151, 40.7132885 ], [ -74.0400165, 40.7132847 ], [ -74.0399266, 40.7132743 ], [ -74.0400315, 40.712747 ], [ -74.0407879, 40.7128354 ], [ -74.0407457, 40.7130407 ] ], [ [ -74.0400371, 40.7131942 ], [ -74.0401257, 40.7132042 ], [ -74.0401615, 40.7130218 ], [ -74.0400729, 40.7130118 ], [ -74.0400371, 40.7131942 ] ] ] }

to insert into postgres they need to be in this format (the coordinates below are from http://www.postgis.net/docs/ST_GeomFromText.html )

SELECT ST_GeomFromText('POLYGON((-71.1776585052917 42.3902909739571,-71.1776820268866 42.3903701743239,
-71.1776063012595 42.3903825660754,-71.1775826583081 42.3903033653531,-71.1776585052917 42.3902909739571))');

Essentially the list needs to be a tuple and the coordinate pairs cannot have commas separating the lat long values and there cannot be ' or " around the coordinates

i cannot figure out a way to create that type of data format

i have tried this:

geojson = json.dumps(i['geometry'])
qry = "insert into AbeBuildings (geom) values(ST_SetSRID(ST_GeomFromGeoJSON(%s), 4326));"
cur.execute(qry,(geojson))

error says

{"type": "Polygon", "coordinates": [[[-74.0410635, 40.7112018], [-74.0409077, 40.7119448], [-74.0408936, 40.7119431], [-74.0402319, 40.7118572], [-74.0403913, 40.7111239], [-74.0406334, 40.711153], [-74.0405859, 40.7113794], [-74.0406154, 40.7113997], [-74.0407442, 40.7114119], [-74.0407737, 40.7113936], [-74.04082, 40.7111725], [-74.0410635, 40.7112018]], [[-74.0406941, 40.7118065], [-74.0407382, 40.7116013], [-74.0405315, 40.7115758], [-74.0404874, 40.711781], [-74.0406941, 40.7118065]]]}
Traceback (most recent call last):
  File "C:\Users\Ralph\Desktop\myfile.py", line 28, in <module>
    cur.execute(qry,(geojson))
TypeError: not all arguments converted during string formatting

Psycopg2 requires arguments to be a list or a tuple, you specified just one value. Try following:

cur.execute(qry, (geojson,))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM