1

How do I insert a polygon object into a feature class?

I've written the following code based off the Writing Geometries (https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/writing-geometries.htm) guide, unfortunately all of those examples are starting from scratch and I extracted my polygon objects from the .getExtents().polygon function in the camera class. This creates the row and adds the name but there aren't any of the polygons.

for name, poly in polyDict.items():
 with arcpy.da.InsertCursor(gdb_fc, ['Name', 'SHAPE@XY']) as cursor:
 cursor.insertRow([name, poly])

I've also tried adding an index to the poly variable to try the array but that doesn't work either. I'm obviously doing something wrong but have no idea as to what. I've tried it with a single value which doesnt work, I've tried extracting the XMax, YMax, XMin, YMin from the extent, I've tried 20 other things and I just can't figure it out.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Nov 17, 2021 at 23:57
5
  • 3
    Assuming poly from your dictionary is a Polygon object then your insert cursor retrieved fields would be ['Name','SHAPE@']. Read the help file parameter section for the insert cursor, you will read that SHAPE@XY is for a centroid but you want to insert a polygon. Commented Nov 18, 2021 at 0:07
  • 2
    I don't think SHAPE@XY is applicable with polygon feature classes, only points. You might need to delare your cursor with ['Name', 'SHAPE@'], construct your polygon from an array object as a polygon geometry and store that.. but first check that your dict actually has X,Y pairs and isn't empty. Commented Nov 18, 2021 at 0:09
  • 1
    @MichaelStimson and Hornbydd, you both had it correct! Needed to be the SHAPE@. I could have sworn I tried that earlier, but perhaps something else was not working at that time, but I'm seeing polygons now. Thanks for the help! Commented Nov 18, 2021 at 0:30
  • 1
    To qualify as a code snippet I think the code that you present should include how you "extracted my polygon objects from the .getExtents().polygon function in the camera class" Commented Nov 18, 2021 at 1:01
  • @PolyGeo, OK will do in the future. Commented Nov 19, 2021 at 20:54

1 Answer 1

1

As commented by @Hornbydd:

Assuming poly from your dictionary is a Polygon object then your insert cursor retrieved fields would be ['Name','SHAPE@']. Read the help file parameter section for the insert cursor, you will read that SHAPE@XY is for a centroid but you want to insert a polygon.

answered Nov 18, 2021 at 3:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.