0

I'm trying to convert WKT from an API to a shapefile via arcpy (there is a requirement to use the ArcGIS GUI) and am unable to get arcpy to create the shapefile.

My code:

def create_shapefile(self, input_file, data, template):
    try:
        arcpy.CreateFeatureclass_management(os.path.dirname(input_file), os.path.basename(input_file), 'POLYLINE', template, has_m='ENABLED', has_z='ENABLED')
        insertCursor = arcpy.da.InsertCursor(input_file, ['SHAPE@', 'OPNUM'])
        for f in data:
            # Manually overriding the geom for purposes of this example
            # Data is valid, I can access all attributes if I print to the GUI
            f['geom'] = 'LINESTRING ZM(-93.8477 45.5571 3340 0,-93.8413 45.5521 3308 6,-93.8356 45.5477 3287 11,-93.8295 45.5430 3271 16,-93.8234 45.5383 3246 21)'
            insertCursor.insertRow((f['geom'], f['opnum']))

    except Exception, e:
        self._catch_error(sys._getframe().f_code.co_name, e)

The error in the ArcGIS script dialog is:

Error in function: create_shapefile
cannot alter multipart geometry type

ArcGIS screenshot

Edit: Updated to new error after reply about using SHAPE@ instead of SHAPE@WKT

  • 1
    Your WKT is invalid. It should be LINESTRING ZM ... if it contains Z and M values. I would use the geometry constructor and shape@ in the cursor. Don't forget to apply the correct SpatialReference – Vince Apr 30 '18 at 15:09
  • Thanks @Vince - I did not know about shape@ vs shape@wkt and can't believe I missed the ZM! I have updated the question with the new error. Thanks again for your help. – Matt Apr 30 '18 at 15:11
  • 1
    Please Edit the question to have a complete code sample with proper indentation, and an error message that specifies the line number. – Vince Apr 30 '18 at 15:13
  • 1
    Data Access cursors take a row, which should be an array. I believe Shape@ requires a Geometry object, not a string, but I can't find a WKT constructor for Geometry. – Vince Apr 30 '18 at 15:36
  • The missing ZM was the root cause. SHAPE@WKT is in the documentation (desktop.arcgis.com/en/arcmap/10.5/analyze/arcpy-data-access/…) but only works with a valid WKT. Thanks so much for your help! – Matt Apr 30 '18 at 17:37
1

Use the 'SHAPE@WKT' instead of 'SHAPE@'. SHAPE@ requires a geometry object as an input.

def create_shapefile(self, input_file, data, template):
    try:
        arcpy.CreateFeatureclass_management(os.path.dirname(input_file), os.path.basename(input_file), 'POLYLINE', template, has_m='ENABLED', has_z='ENABLED')
        insertCursor = arcpy.da.InsertCursor(input_file, ['SHAPE@WKT', 'OPNUM'])
        for f in data:
            # Manually overriding the geom for purposes of this example
            # Data is valid, I can access all attributes if I print to the GUI
            f['geom'] = 'LINESTRING ZM(-93.8477 45.5571 3340 0,-93.8413 45.5521 3308 6,-93.8356 45.5477 3287 11,-93.8295 45.5430 3271 16,-93.8234 45.5383 3246 21)'
            insertCursor.insertRow((f['geom'], f['opnum']))

    except Exception, e:
        self._catch_error(sys._getframe().f_code.co_name, e)
  • Converting back to SHAPE@WKT and fixing the missing ZM did it. Thanks to you and @vince for your help! – Matt Apr 30 '18 at 17:34

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.