2016-03-28 185 views
1

我的問題很簡單。從geopy.distance,我可以計算兩點之間的距離。但我無法轉換數據格式以供進一步計算。Vincenty距離轉換成陣列

這樣的代碼:

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),仍然不能直接支持計算。

+0

FWIW,如果你想使用更穩定和更精確的算法比Vincenty,看看CF˚FKarney的優秀[GeographicLib(HTTP://geographiclib.sourceforge 。淨/)。維基百科的[橢圓體上的測地線]也提供了一些信息(https://en.wikipedia.org/wiki/Geodesics_on_an_ellipsoid#Solution_of_the_inverse_problem);卡爾尼是該文章的主要撰稿人,以及大多數關於橢球測地線的維基百科文章。 –

回答

2

正如手冊中所建議的那樣,您需要以某種格式「導出」距離/ 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) 

可能是有幫助的,如果你存儲/加載大量的數據,但始終只是需要一定的精度。

+0

我曾經想到過。但我仍然無法找到解決方案來將'print'內容保存爲np.array。 –

+0

我已經爲此添加了一個示例。不過,我建議你閱讀一些NumPy和/或Python教程,因爲這是真正的*基礎知識。如果你用print(vincenty(newport_ri,cleveland_oh).miles)打印一個'float',它應該*非常明顯*你可以簡單地使用'someVariable = vincenty(newport_ri,cleveland_oh).miles'來獲得這個float將它保存在一個變量中用於進一步計算(或者變成一個numpy數組)。 – daniel451

+1

沒問題,不客氣。不要忘記查看一些教程;) – daniel451

1
vincenty((38.103414282108375, 114.51898800000002),\ 
        (38.07902986076924, 114.50882128404997)) 

它是對象,你正在嘗試在不同類型的對象上進行乘法運算。 我建議這樣做

from geopy.distance import vincenty 
length = vincenty((38.103414282108375, 114.51898800000002),\ 
        (38.07902986076924, 114.50882128404997)) 
length = length.miles 

ration = np.array(([2,2],[3,3]))*length