Geometry object stuff

1867
3
02-13-2014 10:22 AM
NeilAyres
MVP Alum
I have used this stuff before but this afternoon (because of my fading brain) I got stuck again.
This page says that using point geometry objects, you can access the X,Y,Z like this:

http://resources.arcgis.com/en/help/main/10.2/index.html#//002z0000001t000000

for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@"]):
    # Print the current multipoint's ID
    #
    print("Feature {0}:".format(row[0]))

    # For each point in the multipoint feature,
    #  print the x,y coordinates
    for pnt in row[1]:
        print("{0}, {1}".format(pnt.X, pnt.Y))


If you do this, all you get is this :
>>> geom1
<PointGeometry object at 0x128e50d0[0x128e51c0]>
>>> geom1.type
u'point'
>>> geom1.X

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    geom1.X
AttributeError: 'PointGeometry' object has no attribute 'X'
>>> 


This had me stuck for about an hour until, from somewhere in my aging cortex, I remembered you had to do this :

>>> geom1.type
u'point'
>>> geom1.getPart().X
37.85577369550394
>>> geom1.getPart().Y
-5.721226269422574
>>> geom1.getPart().Z
633.8880000000063
>>> 


Oh, and if you try to iterate over a geometry object like in the help, that fails as well.

>>> for p in geom1:
 print p.getPart().X

 

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    for p in geom1:
TypeError: 'PointGeometry' object is not iterable
>>> 


Dear esri, when is either the attributes of the geometry going to get fixed, or the help files when you want to actually do this stuff?

Thanks,
Neil
Tags (2)
0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Neil,

Looks like you are trying to iterate through the shape field, which won't be possible, unless you are working with a multipoint feature class:

for pnt in row[1]:
        print("{0}, {1}".format(pnt.X, pnt.Y))



However, you can return the XY coordinates a couple ways:

for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@"]):
    print("Feature {0}:".format(row[0]))
    print row[1].centroid.X, row[1].centroid.Y


or:

for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@XY"]):
    print("Feature {0}:".format(row[0]))
    print row[1]
0 Kudos
NeilAyres
MVP Alum
Thanks Jake,

I realize that .centroid would also work. But I still think that the help could be a little less misleading.
Got my script to work by using .getPart()

Cheers,
Neil
0 Kudos
FredSpataro
Occasional Contributor III
Adding my 2 cents here, since i went looking for this today: 

The PointGeometry class should have either .X .Y .Z properties or a .Point property.  It doesn't make sense to use .getPart() or .centroid() or .firstPoint().  Isn't the point (no pun intended) of python to be "easy to read/write"?

I added a item to the ideas system: http://shar.es/RHjsR