确定纬度/经度坐标是否位于美国的一个州内

6
使用Python,有没有一种函数、库或其他方式可以检查一个特定的纬度/经度坐标对是否落在一个定义好的地理边界内,比如美国的某个州?
例如:
32.781065, -96.797117 是否在德克萨斯州内?

1
我在谷歌搜索中找到了一些链接,关于“Python反向地理编码”的内容。链接 - Robᵩ
关于关闭投票,我认为OP并没有特别期望或要求需要使用"工具、[第三方]库或离线资源"来解决问题。但事实上确实需要这些东西。 - Karl Knechtel
1
我很困惑。是否有投票关闭这个问题?如果是,为什么?我提出了一个诚实的问题,我相信对其他人也会有用。 - Matt Keller
4个回答

5
你可以使用reverse geocode库,然后检查“admin1”区域是否为特定的美国州。它将纬度/经度坐标转换为字典格式,例如:
{
  'name': 'Mountain View', 
  'cc': 'US', 
  'lat': '37.38605',
  'lon': '-122.08385', 
  'admin1': 'California', 
  'admin2': 'Santa Clara County'
}

太棒了! - Lucas925

2
您可以使用地理编码库,您可以使用pip install geocoder进行安装。
import geocoder

results = geocoder.google('32.781065, -96.797117', reverse = True)

print(results.current_result.state_long)

enter image description here


1

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"。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接