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 (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?


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...


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
| improve this answer | |
  • when I run your code, followed by print point.WKT i get a list of points. i want a single point representing the centroid of a polygon. – CSB Dec 14 '15 at 20:12
  • Right, see my edit. At first I thought you were working with a point feature class. You need to get the centroid then convert it to a Point Geometry object. – crmackey Dec 14 '15 at 20:13
  • there is only one feature in the line. i'm having a hard time figuring out how to get it into a geometry object though. – CSB Dec 14 '15 at 20:15
  • See my edit 2. To get geometry objects from existing features, you just need to run a search cursor with the 'SHAPE@' token. – crmackey Dec 14 '15 at 20:23
  • thanks, i'll let you know as soon as i'm able to test this out and get back to you. – CSB Dec 14 '15 at 20:44

@crmackey Thanks for setting me on the right path. This is what I ended up doing, and it worked. It seems like what I was missing was the intermediate step that you suggested for the point. The way you suggested I use the line threw me all kinds of errors though. I did some more research and finally figured out I basically needed to do the same for the line: pull the coords > make points > put in array > make a polyline

array = arcpy.Array()

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

for row in arcpy.da.SearchCursor(nrthLine, ["SHAPE@"]):
            for part in row[0]:
                    for pnt in part:
                            x = pnt.X
                            y = pnt.Y
                            point = arcpy.Point(x, y)

newNrthLine = arcpy.Polyline(array)

northDist = centroid.distanceTo(southExtNorthLine)

distance = 552876.3574719524
| improve this answer | |
  • 1
    Thanks for posting. I didn't test the code I posted, but I saw the problem was that I did not pass in the fields argument. So the line to get the line geometry should have been this: line_geom = [r[0] for r in arcpy.da.SearchCursor(nrthLine, ['SHAPE@'])][0]. I have edited my answer to include this update. – crmackey Dec 15 '15 at 2:27
  • @crmackey i caught the missing field, and added it in. still, i got this error: "ValueError: <geoprocessing describe geometry object object at 0x1105F0C0>" when trying to use it in the distanceTo() function. I also tried forcing line_geom into polyline via x = arcpy.Polyline(line_geom), but this popped up: RuntimeError: "Object: CreateObject cannot create geometry from inputs". i know you don't have time to run all this code. i'm just including the errors in the comments in case someone stumbles across this while having a similar issue. – CSB Dec 15 '15 at 22:48
  • 1
    This answer is very helpful, and makes it clear that the geometry of a feature is comprised of parts. I had been baffled by a misleading example in ArcGIS online documentation (desktop.arcgis.com/en/arcmap/latest/analyze/python/…). – Mike Finch Aug 30 '17 at 15:59

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.