我的问题很简单。从geopy.distance
,我可以计算两点之间的距离。但我无法转换数据格式以进行进一步计算。
代码如下:
from geopy.distance import vincenty
length = vincenty((38.103414282108375, 114.51898800000002),\
(38.07902986076924, 114.50882128404997))
ration = np.array(([2,2],[3,3]))*length
错误:
unsupported operand type(s) for *: 'int' and 'vincenty'
我尝试将 Distance(xxx)
更改为 np.array: np.array(length)
,但失败了。类似array(Distance(388.659276576), dtype=object)
,仍然不支持直接计算。
最佳答案
按照手册中的建议,您需要以某种格式“导出”您的距离/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
,因为(正如您已经提到的)它是 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)
如果您要存储/加载大量数据但总是只需要一定的精度,这可能会很有帮助。
关于python - Vincenty 距离变换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36259004/