i have a postgres database with millions of rows in it it has a column called geom which contains the boundary of a property.
using a python script i am extracting the information from this table and re-inserting it into a new table.
when i insert in the new table the script bugs out with the following:
Traceback (most recent call last):
File "build_parcels.py", line 258, in <module>
main()
File "build_parcels.py", line 166, in main
update_cursor.executemany("insert into parcels (par_id, street_add, title_no, proprietors, au_name, ua_name, geom) VALUES (%s, %s, %s, %s, %s, %s, %s)", inserts)
psycopg2.IntegrityError: new row for relation "parcels" violates check constraint "enforce_geotype_geom"
The new table has a check constraint enforce_geotype_geom = ((geometrytype(geom) = ‘POLYGON’::text) OR (geom IS NULL)) whereas the old table does not, so im guessing theres dud data or non polygon (perhaps multipolygon data?) in the old table. i want to keep the new data as polygon so want to not insert anything else.
Initially i tried wrapping the query with standard python error handling with the hope that the dud geom rows would fail but the script would keep running , but the script has been written to commit at the end not each row so it doesnt work.
I think what i need to do is iterate through the old table geom rows and check what type of geometry they are so i can establish whether or not i want to keep it or throw it away before i insert into the new table
Whats the best way of going about this?
This astonishingly useful bit of PostGIS SQL should help you figure it out… there are many geometry type tests in here: