I would like to know how to measure the distance from two Points in ArcPy?

I know only Geometry has the distanceTo method but how do I could make a typecast in ArcPy?

I got the following error message:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 10, in <module>
AttributeError: 'Point' object has no attribute 'distanceTo'

Here is my code:


... ptIni = {}
... ptEnd = {}
... for row in arcpy.SearchCursor("polilinha"):
...     ptIni = row.Shape.firstPoint
...     ptEnd = row.Shape.lastPoint
... print ptIni.distanceTo(ptFim)
| improve this question | | | | |
  • 1
    You have a polyline and what to know the distance from first to last Point measured as a straight line (not along the line)? – BERA Feb 1 '19 at 12:27
  • 2
    The last version of ArcGIS that didn't support Data Access cursors is now long retired, so it's time to start actively discouraging the use of the less-than-Pythonic arcpy.SearchCursor. Instead, use arcpy.da.SearchCursor, which is both faster and easier to integrate into Python list operators. If you read the doc on firstPoint/lastPoint, you'll see the result type is Point, but that distanceTo is an operation of PointGeometry. Be careful to retrieve the SpatialReference of the source layer and apply it to the PointGeometry constructor or the precision could be lost. – Vince Feb 1 '19 at 12:35
  • 1
    Point doesn't have distanceTo. pointGeometry does. – nickves Feb 1 '19 at 12:38
  • @BERA actually I need to find the walfway point by measuring distances between polyline parts (path). – Ezequias Feb 1 '19 at 12:56
  • 3
    You've changed this question in comments enough to break the answer, and now the answer doesn't address the question. Please Edit the Question to ask the question you want answered. If you have a new question, you can ask it as a separate Question. – Vince Feb 1 '19 at 13:56

Use the da.SearchCursor instead of the old cursor.

To find mid point use positionAlongLine with use_percentage=True:

import arcpy
fc = 'Roads'
geom = [i[0] for i in arcpy.da.SearchCursor(fc,'SHAPE@')][0]
midpoint = geom.positionAlongLine(value=0.5, use_percentage=True)

enter image description here

| improve this answer | | | | |
  • I know this method but I would like to make the same without using the built-in feature. – Ezequias Feb 1 '19 at 18:39

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.