使用Cartopy检查地理坐标点是陆地还是海洋?

9
1个回答

14

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

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 找到,并生成以下用于测试英国地形包含性的概念证明:

uk_containment

你可能会对另一个工具 geopandas 感兴趣,因为这种包含性测试是它的核心功能之一。


我花了很长时间才成功安装所有的Cartopy依赖项。我可能会添加一些警告用户的小提示,告诉他们这不仅仅是一个简单的pip安装过程。 - rocksNwaves
现在我正在发现这些依赖项的设置并不容易,即使在遵循GEOS和cartopy网页上的简要说明之后仍然如此。然后,在安装所有要求之后,除非您安装更多依赖项(除了在此github问题https://github.com/SciTools/cartopy/issues/1239中未列出的),否则会导致导入错误。 - rocksNwaves

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