1

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
    I think there might be some confusion between what is actually encoded in the shapefile, which is the MULTIPOLYGON ZM, and what you want, which is just MULTIPOLYGON. 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. – Get Spatial Nov 23 '16 at 20:49
  • It's actually trivial to just copy a shapefile to PostgreSQL using ArcGIS 10.4 or higher (a dozen lines of Python, with extensive documentation). – Vince Nov 23 '16 at 22:52
  • please share Vince – D_C Nov 23 '16 at 23:40
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.