Trying to access coordinates of the vertices in polygons using Arcpy and getting “Error in getting object from array”

1644
2
Jump to solution
02-14-2021 08:32 AM
Labels (1)
OlesiaIgnateva
New Contributor
I want to get coordinates of vertices of polygons using arcpy. My code is:
with arcpy.da.SearchCursor(inFC,['OID@','SHAPE@']) as cursor:
    for row in cursor:
        array1=row[1].getPart() # get a single polygon 

        for vertice in range(row[1].pointCount): # a loop to access vertices in a polygon
            pnt=array1.getObject(0).getObject(vertice)
            #print(row[0],pnt.X,pnt.Y)

            try:
                vertices = (pnt.X,pnt.Y)
                print(vertices)
            except:
                print("No vertice")

What I got is a list of coordinates, but not all of them. The code's run breaks by an error:

File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\arcobjects.py", line 105, in getObject return convertArcObjectToPythonObject(self._arc_object.GetObject(*gp_fixargs(args))) RuntimeError: Array: Error in getting object from array

Why this can happen?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

There's an oven-ready solution here Read geometries—ArcGIS Pro | Documentation - section headed 'Reading polyline or polygon geometries'

import arcpy

infc = arcpy.GetParameterAsText(0)

# Enter for loop for each feature
for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@"]):
    # Print the current polygon or polyline's ID
    print("Feature {}:".format(row[0]))
    partnum = 0

    # Step through each part of the feature
    for part in row[1]:
        # Print the part number
        print("Part {}:".format(partnum))

        # Step through each vertex in the feature
        for pnt in part:
            if pnt:
                # Print x,y coordinates of current point
                print("{}, {}".format(pnt.X, pnt.Y))
            else:
                # If pnt is None, this represents an interior ring
                print("Interior Ring:")

        partnum += 1

View solution in original post

2 Replies
DavidPike
MVP Frequent Contributor

There's an oven-ready solution here Read geometries—ArcGIS Pro | Documentation - section headed 'Reading polyline or polygon geometries'

import arcpy

infc = arcpy.GetParameterAsText(0)

# Enter for loop for each feature
for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@"]):
    # Print the current polygon or polyline's ID
    print("Feature {}:".format(row[0]))
    partnum = 0

    # Step through each part of the feature
    for part in row[1]:
        # Print the part number
        print("Part {}:".format(partnum))

        # Step through each vertex in the feature
        for pnt in part:
            if pnt:
                # Print x,y coordinates of current point
                print("{}, {}".format(pnt.X, pnt.Y))
            else:
                # If pnt is None, this represents an interior ring
                print("Interior Ring:")

        partnum += 1
DanPatterson
MVP Esteemed Contributor
in_fc = r"C:\arcpro_npg\npg\Project_npg\tests.gdb\sq2"

with SearchCursor(in_fc, "SHAPE@") as cursor:
    a = [row[0] for row in cursor]
    

a
Out[16]: 
[<Polygon object at 0x1759a6c8348[0x1759a6b7ba0]>,
 <Polygon object at 0x1759a6c82c8[0x1759a6b7bd0]>,
 <Polygon object at 0x1759a6c8548[0x1759a6b7c30]>,
 <Polygon object at 0x1759a6c8a48[0x1759a6b7a80]>,
 <Polygon object at 0x1759a6c8148[0x1759a6b7cc0]>,
 <Polygon object at 0x1759a6c8cc8[0x1759a6b7ae0]>,
 <Polygon object at 0x1759a6c8848[0x1759a6b7b10]>]

a[0]
# image of shape

a[0][0]
<Array [<Point (300009.0, 5000001.0, #, #)>, <Point (300000.0, 5000000.0, #, #)>, <Point (300002.0, 5000008.0, #, #)>, <Point (300008.0, 5000010.0, #, #)>, <Point (300010.0, 5000010.0, #, #)>, <Point (300010.0, 5000008.0, #, #)>, <Point (300009.0, 5000001.0, #, #)>, None, <Point (300003.0, 5000003.0, #, #)>, <Point (300007.0, 5000003.0, #, #)>, <Point (300005.0, 5000007.0, #, #)>, <Point (300003.0, 5000003.0, #, #)>]>

a[0][0][0]
<Point (300009.0, 5000001.0, #, #)>

The simplest is to use methods on the cursor directly,

def _fc_as_narray_(in_fc, with_id=True):
    """Return geometry from a featureclass using `as_narray`."""
    flds = ["SHAPE@X", "SHAPE@Y"]
    if with_id:
        flds = ["OID@", "SHAPE@X", "SHAPE@Y"]
    with SearchCursor(in_fc, flds, explode_to_points=True) as cursor:
        a = cursor._as_narray()
    del cursor
    return a
    

a = _fc_as_narray_(in_fc, True)

a0 = list(a)  # --- convert to list

a0
[(1,  300009.00,  5000001.00),
 (1,  300000.00,  5000000.00),
 (1,  300002.00,  5000008.00),
 (1,  300008.00,  5000010.00),
 (1,  300010.00,  5000010.00),
 (1,  300010.00,  5000008.00),
 (1,  300009.00,  5000001.00),
 (1,  300003.00,  5000003.00),
 (1,  300007.00,  5000003.00),
 (1,  300005.00,  5000007.00),
 (1,  300003.00,  5000003.00),
 (2,  300010.00,  5000008.00),
 (2,  300010.00,  5000010.00),
 (2,  300008.00,  5000010.00),
 (2,  300008.00,  5000011.00),
 (2,  300008.00,  5000012.00),
 (2,  300012.00,  5000012.00),
 (2,  300012.00,  5000008.00),
 (2,  300010.00,  5000008.00),
 (3,  300008.00,  5000011.00),
 (3,  300005.00,  5000010.00),
 (3,  300005.00,  5000012.00),
 (3,  300006.00,  5000012.00),
 (3,  300008.00,  5000012.00),
 (3,  300008.00,  5000011.00),
 (4,  300006.00,  5000012.00),
 (4,  300005.00,  5000012.00),
 (4,  300005.00,  5000015.00),
 (4,  300007.00,  5000014.00),
 (4,  300006.00,  5000012.00),
 (5,  300002.50,  5000013.00),
 (5,  300001.00,  5000011.50),
 (5,  300000.00,  5000010.00),
 (5,  300001.00,  5000013.00),
 (5,  300003.00,  5000014.00),
 (5,  300002.50,  5000013.00),
 (5,  300001.00,  5000012.50),
 (5,  300001.50,  5000012.50),
 (5,  300001.50,  5000013.00),
 (5,  300001.00,  5000012.50),
 (6,  300004.00,  5000012.50),
 (6,  300003.00,  5000011.00),
 (6,  300002.00,  5000011.00),
 (6,  300001.00,  5000011.50),
 (6,  300002.50,  5000013.00),
 (6,  300004.00,  5000012.50),
 (6,  300002.50,  5000012.50),
 (6,  300001.50,  5000011.50),
 (6,  300002.50,  5000011.50),
 (6,  300002.50,  5000012.50),
 (9,  300002.00,  5000011.00),
 (9,  300003.00,  5000011.00),
 (9,  300004.00,  5000009.00),
 (9,  300002.00,  5000009.00),
 (9,  300002.00,  5000011.00)]

... sort of retired...