【问题标题】:Checking if a geocoordinate point is land or ocean with cartopy?检查地理坐标点是陆地还是海洋?
【发布时间】:2018-06-02 08:00:24
【问题描述】:

如果坐标是陆地还是海洋,我想知道纬度和经度

根据https://gis.stackexchange.com/questions/235133/checking-if-a-geocoordinate-point-is-land-or-ocean

from mpl_toolkits.basemap import Basemap
bm = Basemap()   # default: projection='cyl'
print bm.is_land(99.675, 13.104)  #True
print bm.is_land(100.539, 13.104)  #False

问题是底图已被弃用。如何使用 cartopy 执行此操作?

【问题讨论】:

    标签: cartopy


    【解决方案1】:

    可以在Polygon containment test in matplotlib artist 找到有关使用 cartopy 对国家几何图形进行点包含测试的问题。

    Cartopy 有实现这一点的工具,但没有“is_land”等内置方法。相反,您需要获取适当的几何数据,并使用标准的 shapely 谓词进行查询。

    import cartopy.io.shapereader as shpreader
    import shapely.geometry as sgeom
    from shapely.ops import unary_union
    from shapely.prepared import prep
    
    land_shp_fname = shpreader.natural_earth(resolution='50m',
                                           category='physical', name='land')
    
    land_geom = unary_union(list(shpreader.Reader(land_shp_fname).geometries()))
    land = prep(land_geom)
    
    def is_land(x, y):
        return land.contains(sgeom.Point(x, y))
    

    这给出了两个样本点的预期结果:

    >>> print(is_land(0, 0))
    False
    >>> print(is_land(0, 10))
    True
    

    如果您可以访问它,fiona 将使这更容易(而且更快捷):

    import fiona
    import cartopy.io.shapereader as shpreader
    import shapely.geometry as sgeom
    from shapely.prepared import prep
    
    geoms = fiona.open(
                shpreader.natural_earth(resolution='50m',
                                        category='physical', name='land'))
    
    land_geom = sgeom.MultiPolygon([sgeom.shape(geom['geometry'])
                                    for geom in geoms])
    
    land = prep(land_geom)
    

    最后,我(早在 2011 年)制作了 shapely.vectorized 功能,以在同时测试 许多 点时加速这种操作。该代码可在https://gist.github.com/pelson/9785576 上作为要点获得,并为英国测试土地遏制产生以下概念验证:

    您可能有兴趣阅读的另一个工具是 geopandas,因为这种遏制测试是它的核心功能之一。

    【讨论】:

    • 我花了很长时间安装所有 cartopy 依赖项。我可能会在您的答案中添加一些模糊警告用户,这不仅仅是直接的 pip 安装。
    • 现在我发现这些依赖项的设置非常重要,即使在遵循 GEOS 和 cartopy 网页上的稀疏指示之后也是如此。然后在安装所有要求后,导入错误,除非你安装更多的依赖项,这些依赖项除了这个 github 问题之外没有在任何地方列出:github.com/SciTools/cartopy/issues/1239
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 2018-04-03
    • 2019-09-26
    • 2014-06-21
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多