Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measuring geographic distance with scipy

I fail to use the outcome of scipy's pdist function. I am interested in the real geographic distance (preferred unit: km). Take the following coordinates:

from scipy.spatial.distance import pdist

coordinates = [ (42.057, -71.08), (39.132, -84.5155) ]
distance = pdist(coordinates)
print distance
# [ 13.75021037]

But what's the unit? Google says the distance between these two points is 1179 km. How do I get there from 13.75021037?

like image 690
MERose Avatar asked Jul 25 '15 23:07

MERose


People also ask

How to calculate distance between two geo-locations in Python?

Calculating distance between two geo-locations in Python. Step 1: Installing “haversine”. Step 2: Importing library After installing the library import it import haversine as hs. Step 3: Calculating distance between two locations loc1= (28.426846,77.088834) loc2= (28.394231,77.050308) hs.haversine ...

How to calculate the distance between two points in a map?

It is a great package to work with map projections, but in there you have also the Geod class which offers various geodesic computations. To calculate the distance between two points we use the inv function, which calculates an inverse transformation and returns forward and back azimuths and distance.

How do you measure the distance of the Earth?

You can also use geopy to measure distances. This package has many different methods for calculating distances, but it uses the Vincenty’s formulae as default, which is a more exact way to calculate distances on earth since it takes into account that the earth is, as previously mentioned, an oblate spheroid.

What are the different types of SciPy functions?

Orthogonal distance regression ( scipy.odr ) Optimization and root finding ( scipy.optimize ) Cython optimize zeros API Signal processing ( scipy.signal ) Sparse matrices ( scipy.sparse ) Sparse linear algebra ( scipy.sparse.linalg )


Video Answer


2 Answers

The pdist method from scipy does not support distance for lon, lat coordinates, as mentioned at the comments.

However, if you like to get the kind of distance matrix that pdist returns, you may use the pdist method and the distance methods provided at the geopy package. To do so, pdist allows to calculate distances with a custom function with two arguments (a lambda function).

Here is an example:

from scipy.spatial.distance import pdist
from geopy.distance import vincenty
import numpy as np

coordinates = np.array([[19.41133431, -99.17822823],
                        [19.434514  , -99.180934],
                        [19.380412  , -99.178789])

# Using the vincenty distance function.

m_dist = pdist(coordinates, # Coordinates matrix or tuples list
               # Vicenty distance in lambda function
               lambda u, v: vincenty(u, v).kilometers)
like image 181
RZRKAL Avatar answered Oct 18 '22 17:10

RZRKAL


Using the latest Python 3, this now gives a deprecation warning. I actually found this answer by @cffk much easier to understand:

(pasting here for convenience)

>>> from geopy.distance import great_circle
>>> from geopy.distance import geodesic
>>> p1 = (31.8300167,35.0662833) # (lat, lon) - https://goo.gl/maps/TQwDd
>>> p2 = (31.8300000,35.0708167) # (lat, lon) - https://goo.gl/maps/lHrrg
>>> geodesic(p1, p2).meters
429.1676644986777
>>> great_circle(p1, p2).meters
428.28877358686776
like image 29
Ryan Hunt Avatar answered Oct 18 '22 19:10

Ryan Hunt