Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.