【问题标题】:Find out if lat/lon coordinates fall inside a US State查明纬度/经度坐标是否在美国某个州内
【发布时间】:2014-07-29 04:19:35
【问题描述】:

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

例子:

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

【问题讨论】:

  • 我在google搜索"python reverse geocoding"找到了一些链接。
  • 关于投票结束,我认为 OP 并没有特别期望或要求需要“工具、[第三方]库或场外资源”来解决问题。但确实如此。
  • 我很困惑。是否有投票结束这个问题?如果是这样,为什么?我问了一个诚实的问题,我确信它对其他人有用。

标签: python geopy


【解决方案1】:

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

它将纬度/经度坐标转换为字典,例如:

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

【讨论】:

  • 很好的发现!
【解决方案2】:

您可以使用geocoder library,您可以使用pip install geocoder 安装它。

import geocoder

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

print(results.current_result.state_long)

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      只是以数学家的回答为基础。

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

      【讨论】:

        猜你喜欢
        • 2017-07-18
        • 2011-07-22
        • 1970-01-01
        • 2013-01-09
        • 1970-01-01
        • 1970-01-01
        • 2012-02-03
        • 2012-07-15
        相关资源
        最近更新 更多