"TypeError: cannot alter multipart geometry type" when trying to create singlepart polygons

1074
4
07-21-2023 04:18 AM
IlkaIllers1
Occasional Contributor III

Hello all,

I have a table which has a field with coordinates in the format (x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5) which I want to convert into polygons. At first, I tried it manually by typing into the python window: 

coordinates = [(x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5)]
with arcpy.da.InsertCursor(output_fc,["SHAPE@"]) as cursor:
    cursor.insertRow([coordinates])

This worked fine but is for obvious reasons a lot of work to do separately for every single polygon. So I tried to get it done in one go: 

iCur = arcpy.da.InsertCursor(output_fc,["SHAPE@"])
with arcpy.da.SearchCursor(search_fc,"COL_A") as sCur:
    for row in sCur:
        iCur.insertRow([row[0]])

This results in "TypeError: cannot alter multipart geometry type".

I don't understand why it thinks this is supposed to be a multipart geometry. My thinking was by iterating through the sCur with the for loop, it would take only one row at a time (which is confirm when I print(row)). This works as an input manually, why does it not work within the cursor and loop?

4 Replies
DanPatterson
MVP Esteemed Contributor

polygons require input Point objects, check the associated syntax in the help files

Write geometries—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
IlkaIllers1
Occasional Contributor III

Yes, that page is where I got the idea from. At the very bottom the examples show how to construct a polyline from just pairs of coordinates. I tried that for polygons and as I mentioned it works as long as I manually input the coordinates for every polygon separately - without using arcpy.Point. I feel like I am missing something? Why does this work:

coordinates = [(x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5)]

but it doesn't when I try to retrieve the same input from a search Cursor? I checked with the print function, and

with arcpy.da.SearchCursor(search_fc,"COL_B") as Scur:
    for row in Scur:
        coordinates = row[0]
        print(coordinates)

 gives me exactly the same; namely: 

[(x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5)]

If that is the return from the search cursor, why does it not work as input  for the insert cursor?

0 Kudos
DanPatterson
MVP Esteemed Contributor
coords = [[0.0, 0.0], [2.0, 8.0], [8.0, 10.0], [10.0, 10.0],
          [10.0, 8.0], [9.0, 1.0], [0.0, 0.0]]

arr = arcpy.Array([arcpy.Point(*i) for i in coords])
type(arr)
<class 'arcpy.arcobjects.arcobjects.Array'>

poly = arcpy.Polyline(arr)
type(poly)
<class 'arcpy.arcobjects.geometries.Polyline'>

polyline.png


... sort of retired...
0 Kudos
IlkaIllers1
Occasional Contributor III

But that is again just for one polygon. That works without the array as well. If I try to do it for all polygons with the search Cursor

iCur = arcpy.da.InsertCursor(output_fc,["SHAPE@"])
with arcpy.da.SearchCursor(search_fc,"COL_A") as Scur:
    for row in Scur:
        coordinates = row[0]
        arr = arcpy.Array([arcpy.Point(*i) for i in coordinates])
        poly = arcpy.Polygon(arr)
        iCur.insertRow(poly)

I get the following traceback: 

Traceback (most recent call last):
File "<string>", line 5, in <module>
File "<string>", line 5, in <listcomp>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\mixins.py", line 1101, in __init__
setattr(self, attr, value)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 109, in _set
return setattr(self._arc_object, attr_name, cval(val))
RuntimeError: Point: Input value is not numeric