Getting X and Y from a single point object in arcpy

5456
6
Jump to solution
09-11-2018 01:56 PM
BenjaminSperry1
Occasional Contributor

I am trying to get the X and Y values from the output of the positionAlongLine method.

As far as I can tell it should be a point object so how do I get the XY from that point?

I have found examples of how to do this in a search cursor but that only works with tables, I only have a single point.

new_point = line.positionAlongLine(cur_length, is_percentage)
point_x = new_point.X
point_y = new_point.Y

returns the error: AttributeError: 'PointGeometry' object has no attribute 'X'

Any help would be greatly appreciated.

1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus
6 Replies
DanPatterson_Retired
MVP Emeritus

Try

new_point.centroid.X‍‍

from here

PointGeometry—ArcPy classes | ArcGIS Desktop 

BenjaminSperry1
Occasional Contributor

Thank you very much Dan. That worked perfectly.

0 Kudos
DanPatterson_Retired
MVP Emeritus

No problem... it is one of those quirky things, but think of it as a property of the object... it helped me get over it a long time ago

0 Kudos
XanderBakker
Esri Esteemed Contributor

To add to the explanation by Dan Patterson here a little more about the difference between Point and PointGeometry objects. The Point object is used to store the X, Y, and optionally the Z and M values of a point. The Point object iis used to construct geometries, like PointGeometry, Polyline, Polygon and Multipoint. See example below:

import arcpy

# create a Point with just X and Y (Long and Lat)
pnt = arcpy.Point(-75.569461, 6.216609)
print("X (pnt) : {}".format(pnt.X))

# create PointGeometry using WGS1984
sr = arcpy.SpatialReference(4326)
pntg = arcpy.PointGeometry(pnt, sr)

# retrieve the X property of the PointGeometry (I normally use firstPoint)
print("X (pntg): {}".format(pntg.firstPoint.X))

This will print:

X (pnt) : -75.569461
X (pntg): -75.569461

To construct the Polyline, Polygon and Multipoint geometries you will have to provide an array of Points. See example below:

import arcpy

# simple example, create two Points with just X and Y (Long and Lat)
pnt1 = arcpy.Point(-75.569461, 6.216609)
pnt2 = arcpy.Point(-74.051171, 4.673431)

# create Polyline using WGS1984
sr = arcpy.SpatialReference(4326)
polyline = arcpy.Polyline(arcpy.Array([pnt1, pnt2]), sr)

# retrieve some coordinate values
print("X (firstPoint): {}".format(polyline.firstPoint.X))
print("Y (lastPoint) : {}".format(polyline.lastPoint.Y))

# loop through pnts in Polyline
for part in polyline:
    for pnt in part:
        print(" - {}".format(pnt))

When working with 3D and M aware geometries you will have to enable those properties when you construct the geometry. More on this in my blog here: https://community.esri.com/people/xander_bakker/blog/2016/07/08/working-with-3d-and-m-aware-geometri... 

by Anonymous User
Not applicable

Can you use arcpy.da.searchcursor to get the x,y? 

XanderBakker
Esri Esteemed Contributor

Hi Liran Sun , 

Yes you can. The easiest way would be using the SHAPE@XY token. See:SearchCursor—Data Access module | Documentation