正如手冊中所建議的那樣,您需要以某種格式「導出」距離/ 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);卡爾尼是該文章的主要撰稿人,以及大多數關於橢球測地線的維基百科文章。 –