正如手册中所建议的那样,您需要以某种格式“导出”距离/ vincenty。例如。像这样:
> from geopy.distance import vincenty
> newport_ri = (41.49008, -71.312796)
> cleveland_oh = (41.499498, -81.695391)
> print(vincenty(newport_ri, cleveland_oh).miles)
538.3904451566326
您不能处理vincenty
iteself,因为(你已经提到),它是一个对象的是不支持的数学运算数geopy的。您需要提取数据对象中的值,例如与.miles
。查看完整的文档,其他可能的值:GeoPy documentation
见差异类型:
> type(vincenty(newport_ri, cleveland_oh))
geopy.distance.vincenty
> type(vincenty(newport_ri, cleveland_oh).miles)
float
现在你可以用这个计算:
> vincenty(newport_ri, cleveland_oh).miles
538.3904451566326
> vincenty(newport_ri, cleveland_oh).miles * 2
1076.7808903132652
或者,如果你真的需要一个numpy的阵列出于此:
> np.array(vincenty(newport_ri, cleveland_oh).miles)
array(538.3904451566326)
> type(np.array(vincenty(newport_ri, cleveland_oh).miles))
numpy.ndarray
编辑:请注意,你甚至可以强制执行它的数据类型与NumPy的内置的dtype
参数:
> np.array(vincenty(newport_ri, cleveland_oh).miles, dtype=np.float32)
array(538.3904418945312, dtype=float32)
> np.array(vincenty(newport_ri, cleveland_oh).miles, dtype=np.float64)
array(538.3904451566326) # dtype=float64, default type here
> np.array(vincenty(newport_ri, cleveland_oh).miles, dtype=np.int32)
array(538, dtype=int32)
这可能是有帮助的,如果你存储/加载大量的数据,但始终只是需要一定的精度。
FWIW,如果你想使用更稳定和更精确的算法比Vincenty,看看CF˚FKarney的优秀[GeographicLib(HTTP://geographiclib.sourceforge 。净/)。维基百科的[椭圆体上的测地线]也提供了一些信息(https://en.wikipedia.org/wiki/Geodesics_on_an_ellipsoid#Solution_of_the_inverse_problem);卡尔尼是该文章的主要撰稿人,以及大多数关于椭球测地线的维基百科文章。 –