I am trying to import a shapefile into Postgre using python and psycopg2. I have run into a few issues.
I am using ST_GeometryFromText() of a WKT string.
First, the shape@WKT returns MULTIPOLYGON ZM at the beginning of the string. I believe that it is only supposed to be MULTIPOLYGON correct?
I fixed this by doing a replace on the string but this does not seem like what should be done. What do I do to make this correct?
Second, the shape@WKT contains X, Y, 0, NAN as values. I am expecting only 2 values, x and y. Why am I getting 0 and NAN?
with arcpy.da.SearchCursor(input_fc, ["FID", "Shape@WKT"]) as da_cursor:
for row in da_cursor:
geoms = row[1].replace(' ZM ','')
print geoms
pg_cursor.execute("INSERT INTO %s (fid_p, geom) VALUES (%s, ST_GeometryFromText('%s', %s))" %(dataset_name, int(row[0]), geoms, int(coordinate_system)))
pg_connection.commit()
RESULT of print geoms:
MULTIPOLYGON ZM (((440185.43178632273 6350644.499023444 0 NAN, 440189.09859540447 6350575.940429694 0 NAN, .........
PYTHON ERROR:
InternalError: parse error - invalid geometry HINT: "...85.43178632273 6350644.499023444 0 NA" <-- parse error at position 60 within geometry
1 Answer 1
The last two values are Z and M values for each vertex. Looks like your dataset is Z/M enabled, but the features you're importing don't have values set, thus you see 0/NaN.
If you copy the features into a non ZM-enabled feature class, you won't have to worry about the ZM replace or parsing the 0/NaNs in the WKT string.
MULTIPOLYGON ZM
, and what you want, which is justMULTIPOLYGON
. You are receiving a python error because there are values for the Z and M coordinates in the shapefile geometry. You need to either resave the shapefile before running this script, to strip out the ZM values, or specify a parameter on this function to only look at the 2D elements.