I am struggling to get the arcpy point class method distanceTo to function with dynamically generated geometries with a Python addin.

I have used the default tool addin function and set self.shape (in __init__) to shape. Selecting a polyline feature class in a drop down enables it.

class DrawLine(object):
    """Implementation for testaddin_addin.drawline (Tool)"""
    def __init__(self):
        self.enabled = False
        self.shape = "Line"

The user is able to draw a line, with a spatial reference automatically set to the data frame SR. I use the line to perform a SelectLayerByLocation query and extract the intersecting geometries to a feature layer (MakeFeatureLayer).

For the tool, the order of intersection is important, so I grab the starting point of the drawn line and create a new point geometry:

start = arcpy.PointGeometry(arcpy.Point(line_geometry.firstPoint.X, line_geometry.firstPoint.Y))

Finally I iterate over the intersecting features and attempt to call distanceTo:

    while row:
        geom = row.getValue(shpfieldname)
        print type(start)
        print start.distanceTo(geom)
        row = rows.Next()

start is type <class 'arcpy.arcobjects.geometries.PointGeometry'>, so I know that I am working with a point geometry.

I am returning:

 File "c:\arcgis\desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py", line 809, in distanceTo
    return convertArcObjectToPythonObject(self._arc_object.DistanceTo(*gp_fixargs((other,))))
ValueError: <geoprocessing describe geometry object object at 0x2868B4A0>

Has anyone gotten distanceTo to work? How about with a dynamic geometry?

Note: The input shapefile is simple polylines, single part, composed of pairs of vertices, i.e. as simple as possible.


Try to include a Spatial Reference when creating a new geometry. Most Geometry classes accept a SR as the second parameter. This worked for me.

| improve this answer | |

Figured this one out. @klewis, great suggestion, but in 10.1 the spatial reference of the dynamic geometry is automatically set to the spatial reference of the data frame. This is good to know since, possibly, we could be working with a mismatch and need to manually set the SR.

The answer is in the classes used to instantiate point objects. The firstPoint, lastPoint properties of a polyline can not be used to access the distanceTo method. The error is pretty cryptic... Additionally, using the coordinates of the firstPoint to create an arcpy.Point object will not work as valid input. It is necessary to:

x = firstPoint.X
y = firstPoint.Y
pnt = arcpy.Point(x,y)
pntgeom = arcpy.Pointgeometry(pnt)

distance = pntgeom.distanceTo(other_geometry)

Code completion in pyscripter was acting up on this one and the error was pretty cryptic. I resorted top using print dir(object) to actually figure out what was going on...

| improve this answer | |

This code, where feat is Polygon geometry


is working. It is funny that distanceTo will fail, when you try it other way around, i.e. from point to line


It will end with geoprocessing describe geometry object object at 0x1750FA60 message

| improve this answer | |
  • 1
    Welcome to GIS SE! It may just be formatting but I am unable to follow what you are trying to say in your answer. Would you be able to edit it, and experiment with the Code button {} to see if you can make it clearer, please? – PolyGeo Jul 14 '14 at 22:23

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.