I am using this script to copy some features from an input featureclass into an output featureclass. This works for points but surprisingly not for polygons. For polygons it is generating features with null geometry (Shape_Length and Shape_Area equal to 0), while I can confirm they are not null in the input dataset. What could be causing this issue?

out_gdb = arcpy.env.scratchGDB
desc = arcpy.Describe(input_shapefile)
geom_type = desc.shapeType
sr = desc.spatialReference
out_fc = arcpy.CreateFeatureclass_management(out_gdb, os.path.basename(input_shapefile) + "_Export", geom_type, input_shapefile, spatial_reference=sr)

id_field = desc.OIDFieldName
where_clause = """{0} IN ({1})""".format(arcpy.AddFieldDelimiters(input_shapefile, id_field), ','.join([str(id) for id in feat_ids]))
with arcpy.da.SearchCursor(input_shapefile, ['*'], where_clause) as sCur:
    with arcpy.da.InsertCursor(out_fc, ['*']) as iCur:
        for row in sCur:
  • Does the code that you have presented work if you write to a file geodatabase that is not specified as a scratchGDB? – PolyGeo Feb 12 at 20:40
  • I need to test it (tomorrow), thanks for the hint. Anyway, I am start thinking that the SearchCursor with "*" as field parameter is providing only the centroids of the polygons instead of their entire geometry, and this is probably why I am getting "empty" polygons as output. Just a guess, in line with @mikewatt answer. Tomorrow I'll dig into it again and will update you. – umbe1987 Feb 12 at 21:31
  • @PolyGeo not working neither in a non-scratch File GDB. Now moving on to implement mikewatt solution – umbe1987 Feb 13 at 11:15

Try manually specifying the list of input fields, and include the special "SHAPE@" token to return a geometry object for that field. I suspect the wildcard you're using isn't returning the full geometry, maybe the centroid if anything.

You could grab the field names with:

fields = ['SHAPE@'] + [f.name for f in arcpy.ListFields(input_shapefile)]

then feed that into the cursors in place of the ['*']

  • Thanks for the suggestion. I too was thinking about the maig "SHAPE@" field to solve this issue. I will test your suggestion as soon as I can and come back with my fresult. – umbe1987 Feb 12 at 17:09
  • 1
    That did the trrick, thanks a lot! I am assuming that when using ['*'] to retrieve all fields, ArcGIS uses SHAPE@XY (which is the centroid for polygons) instead of the whole geometry (SHAPE@). Probably for performance reason... – umbe1987 Feb 13 at 11:19

This all seems needlessly pointless and overkill why not simply use the Select tool, you can provide a where clause and export that way using a couple of lines of code?

  • Thanks for suggesting the Select tool. However, this is just the first draft of a growing script and I usually do a lot of stuff with cursors (e.g. using the "SHAPE@" to build arcpy.Geometry objects and make some spatial analyses with them). So, I hope to figure out why it is not working to go on. – umbe1987 Feb 12 at 17:08
  • @umbe1987 well it may be nothing to do with your code, you don't show us what is in feat_ids, maybe they don't match and the id's in your polygon dataset are not strings which you are converting into when id_field is an OID... – Hornbydd Feb 12 at 17:13
  • I guarantee feat_ids is correct (indeed, it's working with any point featureclass). Anyway, when I can I'll add this info as well (typing from mobile right now). – umbe1987 Feb 12 at 17:59

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.