了解纬度/经度坐标是否属于美国国家

时间:2014-07-29 04:19:35

标签: python geopy

使用Python,是否有函数,库或其他方法来检查某个纬度/经度坐标对是否属于定义的地理边界,例如美国州?

示例:

32.781065,-96.797117是否属于德克萨斯州?

4 个答案:

答案 0 :(得分:2)

您可以使用reverse geocode库,然后检查“admin1”区域是否为特定的美国州。

它将lat / lon坐标转换为字典,如:

{
  'name': 'Mountain View', 
  'cc': 'US', 
  'lat': '37.38605',
  'lon': '-122.08385', 
  'admin1': 'California', 
  'admin2': 'Santa Clara County'
}

答案 1 :(得分:2)

您可以使用可以使用import geocoder results = geocoder.google('32.781065, -96.797117', reverse = True) print(results.current_result.state_long) 安装的geocoder library

{{1}}

enter image description here

答案 2 :(得分:1)

我会使用requests libraryGoogle geocoding API发送请求。

答案 3 :(得分:0)

仅以 mathisonian 的答案为基础。

import reverse_geocoder

#create a list of US states (and the District of Columbia)
state_names = ["Alaska", "Alabama", "Arkansas", "Arizona", "California", "Colorado", "Connecticut", "District of Columbia","Delaware", "Florida","Georgia", "Hawaii", "Iowa", "Idaho", "Illinois", "Indiana", "Kansas", "Kentucky", "Louisiana", "Massachusetts", "Maryland", "Maine", "Michigan", "Minnesota", "Missouri", "Mississippi", "Montana", "North Carolina", "North Dakota", "Nebraska", "New Hampshire", "New Jersey", "New Mexico", "Nevada", "New York", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina","South Dakota", "Tennessee", "Texas", "Utah", "Virginia", "Vermont", "Washington", "Wisconsin", "West Virginia", "Wyoming"]

#get the metadata for the latitude and longitude coordinates
results = reverse_geocoder.search((32.781065, -96.797117))

#check if the location is a US state (the 'admin1' variable is where US states are listed)
if results[0]['admin1'] in state_names:
  print(results[0]['admin1'])

那应该打印“Texas”。