how to calculate the distance between two points using python

168934
8
05-05-2015 07:28 AM
mikebei
New Contributor

hello everyone, I am a newbie on python. I am stuck by the calculation of distance between two points along a given line. My primary task is to calculate the distance among different points respectively and make some comparison.  And I don't want to use network analyst. Any helps would be appreciated.Thanks

0 Kudos
8 Replies
DanPatterson_Retired
MVP Emeritus

Not to answer your question directly, but to throw you in the deep end..  you need to study how python interacts with arcmap, through the arcpy module.  Begin exploring the arcpy functions and classes section of the help files and the code snippets.  Of course you will have to determine whether you want the distances between just points that could form a line, the vertices of a polyline,  point(s) you wish to put along a polyline, etc

EDIT

And there were some suggestions on here as well how to calculate the distance between two points using python - Geographic Information Systems Stack...

DarrenWiens2
MVP Honored Contributor

Check out the Polyline object, specifically the measureOnLine method:http://resources.arcgis.com/en/help/main/10.2/index.html#/Polyline/018z00000008000000/

This will give you the measure of the point on the line, from the start of the line. Repeat for both points and find the difference.

CarlSunderman
Occasional Contributor

To calculate distance between two points, you could just do.

import math
def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist
print calculateDistance(x1, y1, x2, y2)

if it is along a line, you could just split the line with the points and use the length of the line for that

mikebei
New Contributor

Hi Carl, many thanks for your hint. your kindness will be well rewarded

0 Kudos
ChrisSmith7
Frequent Contributor

Here's a GitHub for finding the distance between two points using great circle distance:

great circle distance in python

There's also geopy, which has built-in methods:

geopy 1.10.0 : Python Package Index

You wouldn't even need to use ArcGIS to figure this out if you simply have a csv of point pairs to conduct the calculation.

DarrenWiens2
MVP Honored Contributor

Mike,

Since you mention Network Analyst, would you please clarify whether you require the distance between points along a line (e.g. along a winding road), or the straight-line distance between pairs of points?

ChrisSmith7
Frequent Contributor

That's a good and important clarification... At least for the examples I gave, they are only valid for calculating distances "as the crow flies" between two given points.