使用多边形裁剪/遮罩WMS geoTIFF

2024-09-27 22:33:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Django构建一个地理空间webapp。如果我有一个类为django.contrib.gis.geos.polygon.Polygon的多边形,我如何将矩形geoTIFF(通过WMS请求收集;dataset)裁剪/屏蔽为polygon的形状?我曾经使用rasterio将TIFF加载到内存中(基于answers to this question),但由于对Python中的GIS不熟悉,我不确定这是满足裁剪/掩码需求的最佳方法。我很乐意考虑这个问题的任何和所有的解决方案。

我对Corine landcover服务的呼叫,该服务返回一个矩形tiff:

from owslib.wms import WebMapService
from rasterio import MemoryFile

wms = WebMapService('https://copernicus.discomap.eea.europa.eu/arcgis/services/Corine/CLC2018_WM/MapServer/WMSServer?request=GetCapabilities&service=WMS',version='1.1.1')

bbox = (0.823974609375, 52.1081920974632, 1.1700439453125, 52.3202320760973)

img = wms.getmap(layers=['12'],
    srs='EPSG:4326',
    size=(600, 500),
    bbox=bbox,
    format='image/geotiff')

with MemoryFile(img) as memfile:
    with memfile.open() as dataset:
        print(dataset.profile)

以及用于裁剪/遮罩dataset的多边形(最初来自Django模型中的polyGoField()):

polygon = "SRID=4326;POLYGON ((0.9063720703125029 52.32023207609735, 0.8239746093749998 52.10819209746323, 1.170043945312496 52.14191683166823, 1.170043945312496 52.31351619974807, 0.9063720703125029 52.32023207609735)"

有趣的是——我不确定这是否会导致裁剪问题——当我调用dataset.profile时,我发现没有CRS信息,尽管在getmap中定义了srs并返回了一个geoTIFF:

{'driver': 'PNG', 'dtype': 'uint8', 'nodata': None, 'width': 600, 'height': 500, 'count': 3, 'crs': None, 'transform': Affine(1.0, 0.0, 0.0,
       0.0, 1.0, 0.0), 'tiled': False, 'interleave': 'pixel'}

Tags: djangofromimportservice多边形datasetgeotiff矩形

热门问题