[GIS] Error while Geoprocessing with Geometry Objects

arcgis-10.3arcpygeometrygeoprocessing

I'm trying to use the distanceTo() mehtod to return the distance between the centroid of a polygon and a line, but I keep getting this error: "AttributeError: 'Point' object has no attribute 'distanceTo'"

I'm very unfamiliar with working with geometry objects (and I feel like the online help is very unhelpful), but it seems like I'm at least successfully creating a point from the centroid of my study area, as it's printing out as a point with x and y coords, but no z or m coords.

In fact, I'm pretty sure that this is the only correct part of my code so far. I'm not sure if I should be able to run distanceTo() on my line feature layer, or if I need to create a geometry object from the line first, and then try to run the tool.

Anyway, below is my code.

sa = r"Z:\Workspace\GDB.gdb\StudyArea"

nrthLine = r"Z:\Workspace\GDB.gdb\NorthLine"
arcpy.MakeFeatureLayer_management(nrthLine, "nrthLine_lyr")

for row in arcpy.da.SearchCursor(sa, ["SHAPE@XY"]):
        #returns (x,y) of coordinates
        x, y = row[0]
        point = arcpy.Point(x, y)

point
<Point (6719923.30924, 1985670.0069, #, #)>

dis = point.distanceTo("nrthLine_lyr")

AttributeError: 'Point' object has no attribute 'distanceTo'

So, in short, can anyone tell me why I'm getting this error, and what needs to be done to fix it, and successfully incorporate my line feature into the distanceTo() method?

Best Answer

You are working with an arcpy.Point() object, you need to be using an arcpy.PointGeometry() object.

Try this:

sa = r"Z:\Workspace\GDB.gdb\StudyArea"

nrthLine = r"Z:\Workspace\GDB.gdb\NorthLine"
arcpy.MakeFeatureLayer_management(nrthLine, "nrthLine_lyr")

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

        point = row[0]

dis = point.distanceTo("nrthLine_lyr")

I am not sure if this line: dis = point.distanceTo("nrthLine_lyr") is legal either. I think the distanceTo() method requires another Geometry object...

Edit:

I just tested, and the distanceTo() requires a single Geometry object to search against. Also, if your study area is a polygon, you can easily get it to a PointGeometry() object of its centroid like this:

with arcpy.da.SearchCursor(sa, ["SHAPE@"]) as rows::
    for row in rows:
        point = arcpy.PointGeometry(row[0].centroid, row[0].spatialReference) 

How many features are in the "nrthLine_lyr"? If not too many, you can do a nested loop to find the closest feature to each centroid of your study area polygons.

Edit 2:

In order to get the "nrthLine_lyr" to a geometry object, you need to use a Search Cursor. Since there is only one feature, this will work:

line_geom = [r[0] for r in arcpy.da.SearchCursor("nrthLine_lyr")][0]

So to find the distance from all study area centroids to the one line feature, the complete code could look something like this (no need to make feature layer for nrthLine):

import arcpy

sa = r"Z:\Workspace\GDB.gdb\StudyArea"
nrthLine = r"Z:\Workspace\GDB.gdb\NorthLine" # no need to make a feature layer from this

# get single line geometry object
line_geom = [r[0] for r in arcpy.da.SearchCursor(nrthLine, ['SHAPE@'])][0]

# find distance from all study area centroids to line
with arcpy.da.SearchCursor(sa, ["SHAPE@"]) as rows:
    for row in rows:
        point = arcpy.PointGeometry(row[0].centroid, row[0].spatialReference)  

        dis = point.distanceTo(line_geom) 
        # do stuff

        print dis