1

I have a polygon shapefile made up of 2 shapes (independent of each other). I'm using the code below to print in the ArcMap python console, a list of each shapes coordinates in WKT format. I get an error though.

Feature 0:
Part 0:
Runtime error 
Traceback (most recent call last):
  File "<string>", line 26, in <module>
AttributeError: 'unicode' object has no attribute 'X'

What am I missing? 'Williamson_Tunnels_Alert_Zone' is the name of the feature in question by the way.

import arcpy

infc = arcpy.GetParameterAsText(0)

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

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

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

You're confusing the data access cursor "SHAPE@" token and the "SHAPE@WKT" token. The "SHAPE@" token returns a geometry object with parts, etc. The token "SHAPE@WKT"returns a string of the feature's geometry in WKT format.

From the help:

SHAPE@WKT —The well-known text (WKT) representation for OGC geometry. It provides a portable representation of a geometry value as a text string.

SHAPE@ —A geometry object for the feature.

So use:

for row in arcpy.da.SearchCursor("Williamson_Tunnels_Alert_Zone", ["OID@", "SHAPE@WKT"]):
    print("Feature {0}:".format(row[0]))
    print (row [1]) #wkt string
  • Thank you so much, that short 3 line code was all I needed! I'm a bit of a noob still with python so this will help me learn. – Theo F Dec 6 '17 at 11:19
1

Your problem is

print("{0}, {1}".format(pnt.X, pnt.Y))

pnt is a Unicode String and it doesn't have an attribute called X (or probably Y either).

0

Are you setting the environmental workspace somewhere else in this code not shown for the Williamson_Tunnels_Alert_Zone layer? If not your cursor statement needs to reference the infc layer:

for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@WKT"]):

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.