2015-09-27 129 views
-3

我有一個有趣的項目。經歷一個國家的經緯度

所以我有一個WEB API,它接收兩個參數,經度和緯度,並且響應true或false的信息,以圓圈爲中心(緯度,長度)和無線電X(比如說10英里) 。

如果它響應爲真,我必須再次調用它直到它響應False。

如果響應假我不必再調用它

當我假我必須改變(緯度,經度),所以我尋找其他地區的資源比前一個不同,直到我涵蓋一個國家的所有領土。 我想用python自動化它來覆蓋例如美國的所有領土。我該怎麼做?

我正在考慮從聖地亞哥(美國左下角)開始一直到西雅圖或類似的東西。但是,我怎樣才能知道美國領土的分隔符(經緯度)。

我不知道我是否確實解釋了我想要做的事情。如果沒有,請告訴我,我會嘗試更好。

謝謝

回答

2

您可以使用geopy第三方模塊提供的vincenty距離函數。您必須使用pip install geopypypi安裝geopy。

這裏是你如何會那麼寫代碼的一個例子:

from geopy.distance import vincenty 
this_country = (latitude, longitude) 
radius = 10 # 10 miles 

while radius >= 0: 
    other_country_within_circle_found = False 
    # other_countries is a list of tuples which are lat & long 
    # positions of other country eg. (-12.3456, 78.91011) 
    for other_country in other_countries: 
     # note: other_country = (latitude, longitude) 
     if other_country == this_country: 
      continue # skip if other country is the same as this country. 
     distance = vincenty(this_country, other_country).miles 
     if distance <= radius: 
      other_country_within_circle_found = True 
      break 
    if not other_country_within_circle_found: 
     # the circle of this radius, have no other countries inside it. 
     break 
    radius -= 1 # reduce the circle radius by 1 mile. 

參考geopy文檔的詳細信息:https://geopy.readthedocs.org/en/1.10.0/#module-geopy.distance

相關問題