Measuring geographic distance with scipy

ⅰ亾dé卋堺 提交于 2019-12-04 14:14:27

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

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!