problems with distanceTo from a point geometry to a new polyline

4689
5
12-10-2012 12:45 PM
HarryBowman
New Contributor III
I'm trying to call distanceTo from a PointGeometry to a Polyine and am running into trouble whenever the polyline is one I just cooked up. Any pointer on what I'm doing incorrectly would be welcome.

#get the first point feature and the first line feature
poi = arcpy.da.SearchCursor("POIs","SHAPE@").next()[0]
line = arcpy.da.SearchCursor("Streets","SHAPE@").next()[0]

print poi.distanceTo(line)  #returns 0.017893575694200557

#get first part of line
part = line.getPart(0)

#get the first two points on that part
p0 = part[0]
p1 = part[1]

#make a new polyline out of those two points
seg = arcpy.Polyline(arcpy.Array([p0,p1]))


print seg.length #returns 0.0017140186696764268, so the segment is there as a polyline

#try to get the distance from the poi to the segment
poi.distanceTo(seg)



This is what comes back:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\program files (x86)\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 0x16068EE0>
Tags (2)
0 Kudos
5 Replies
ChrisSnyder
Regular Contributor III
I'm having the same issue... The .distanceTo method fails when comparing a geometry object to a "constructed" polyline geometry object.

It works great when you run it on one of the points that your "constructed" polyline is created from, but not for a Polyline geometry object that you "construct" from point A and point B.

Same error:
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py", line 779, in distanceTo
    return convertArcObjectToPythonObject(self._arc_object.DistanceTo(*gp_fixargs((other,))))
ValueError: <geoprocessing describe geometry object object at 0x0D04CCC0>
0 Kudos
HarryBowman
New Contributor III
I think I have figured this out - distanceTo requires both geometries to have the same projection / spatial reference. The newly created geometry has an unknown projection/spatial reference, so the call fails. The spatial reference needs to be set at creation time for the new geometry.
0 Kudos
ChrisSnyder
Regular Contributor III
Great observation! I had not been setting the SR env variable. Now I will!
0 Kudos
HarryBowman
New Contributor III
There must be something else I'm doing wrong. Even though I can create a geometry with a spatial reference and use it for distanceTo, it still shows as having None spatial reference for the purpose of projectAs.
0 Kudos
FelixPertziger
New Contributor III

polylineGeometry.distanceTo(justPoint) will work

pointGeometry.distanceTo(polylineGeometry) will not.

0 Kudos