5

I am trying to import GeoJSON file with Python to my PostgreSQL db.

Here is full code:

with open('C:/xampp/htdocs/app/data/'+a) as file:
 gj=geojson.load(file)
 for feature in gj['features']:
 geom=feature['geometry']
 kc_broj=feature['properties']['BROJ_KC']
 ko_broj=feature['properties']['KO']
 ko_naziv=feature['properties']['K.O.']
 jls='RUGVICA1'
 id=str(ko_broj)+'_'+kc_broj
 cur.execute('INSERT INTO dkp (id,geom,ko_broj,kc_broj,ko_naziv,jls) VALUES (%s, ST_GeomFromText(ST_AsText(ST_GeomFromGeoJSON(%s))), %s, %s, %s, %s)', (id, geom ,ko_broj, kc_broj, ko_naziv, jls,))

And I get error :

psycopg2.ProgrammingError: can't adapt type 'Polygon'

Does anyone know how to fix this?

asked Feb 3, 2019 at 12:27
3
  • In the insert command some apostrophes missing. Print your Insert command from Python and try to execute in psql or pgadmin. Commented Feb 3, 2019 at 13:38
  • @Zoltan can you underline where apostrophes are missing because when I copy this command into sql command window it works fine Commented Feb 3, 2019 at 14:04
  • If it works from the SQL prompt then my assumption was false. Commented Feb 3, 2019 at 14:26

1 Answer 1

8

Insert statement seems OK. But you could try to replace this line:

geom=feature['geometry']

with

geom = (json.dumps(feature['geometry']))))

Because feature['geometry'] is an object and you should convert it to json.

And in insert statement you should set SRID for geometry if you have constraint in PostGIS. Like this:

ST_SetSRID(ST_GeomFromGeoJSON(%s), 4326)
answered Feb 3, 2019 at 14:12
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.