【问题标题】:Given latitude/longitude say if the coordinate is within continental US or not给定纬度/经度,说明坐标是否在美国大陆内
【发布时间】:2017-07-18 23:41:28
【问题描述】:

我想检查特定的纬度/经度是否在美国大陆内。我不想使用在线 API,我正在使用 Python。

我下载了this shapefile

from shapely.geometry import MultiPoint, Point, Polygon
import shapefile    
sf = shapefile.Reader("cb_2015_us_nation_20m")
shapes = sf.shapes()
fields = sf.fields
records = sf.records()
points = shapes[0].points
poly = Polygon(points)
lon = -112
lat = 48
point = Point(-112, 48)
poly.contains(point)
#should return True because it is in continental US but returns False

样本 lon、lat 在美国边界内,但 poly.contains 返回 False。 我不确定问题是什么以及如何解决问题,以便我可以测试一个点是否在美国大陆内。

【问题讨论】:

  • gis.stackexchange.com/questions/84114/… 你确定你的形状是lon, lat 而不是lat, lon
  • 是的,它是 lon,lat。我检查了反向,也不起作用。我最终使用了状态形状文件,现在我正在使用相同的方法检查所有状态,如果其中一个返回 true,那么它就是 true,虽然它现在似乎可以工作。

标签: python shapefile esri matplotlib-basemap pyshp


【解决方案1】:

我最终检查了纬度/经度是否在每个州,而不是在美国大陆检查,如果一个点在其中一个州,那么它在美国大陆。

from shapely.geometry import MultiPoint, Point, Polygon
import shapefile
#return a polygon for each state in a dictionary
def get_us_border_polygon():

    sf = shapefile.Reader("./data/states/cb_2015_us_state_20m")
    shapes = sf.shapes()
    #shapes[i].points
    fields = sf.fields
    records = sf.records()
    state_polygons = {}
    for i, record in enumerate(records):
        state = record[5]
        points = shapes[i].points
        poly = Polygon(points)
        state_polygons[state] = poly

    return state_polygons

#us border
state_polygons = get_us_border_polygon()   
#check if in one of the states then True, else False
def in_us(lat, lon):
    p = Point(lon, lat)
    for state, poly in state_polygons.iteritems():
        if poly.contains(p):
            return state
    return None

【讨论】:

    【解决方案2】:

    我运行了您的代码并绘制了多边形。它看起来像这样:

    如果您使用此代码运行它:

    import geopandas as gpd
    import matplotlib.pyplot as plt
    
    shapefile = gpd.read_file("path/to/shapes.shp")
    shapefile.plot()
    plt.show()
    # credit to https://stackoverflow.com/a/59688817/1917407
    

    你会看到这个:

    所以,1,你没有看 CONUS,2,你的情节很糟糕。您的代码虽然有效,但会在 geopandas 图中返回 True。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 2011-09-03
      相关资源
      最近更新 更多