6

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

例子:

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

4

4 回答 4

4

You could use the reverse geocode library, and then check that the "admin1" area is a specific US state.

It converts lat/lon coordinates into dictionaries like:

{
  'name': 'Mountain View', 
  'cc': 'US', 
  'lat': '37.38605',
  'lon': '-122.08385', 
  'admin1': 'California', 
  'admin2': 'Santa Clara County'
}
于 2015-06-04T14:52:13.783 回答
2

您可以使用可以使用安装的地理编码器库pip install geocoder

import geocoder

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

print(results.current_result.state_long)

在此处输入图像描述

于 2018-05-17T23:15:11.800 回答
1

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

于 2014-07-29T04:25:38.573 回答
0

只是为了建立数学家的答案。

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'])

那应该打印“德克萨斯”。

于 2021-02-17T07:48:28.147 回答