使用shapefile剪辑或遮罩多波段图像

时间:2020-09-04 18:25:19

标签: python-3.x image mask shapefile clip

下面的代码允许用户使用简单的多边形shapefile剪切图像(1个带)。但是,即使我尝试了几种变体,也无法将其应用于由多个波段组成的图像。我想做的是读取具有多个条带的Geotiff图像并将其全部裁剪,而无需将它们分成不同的图像。

from rasterio.mask import mask
import geopandas as gpd

img = rasterio.open("..\Image.tiff")
aoi = gpd.read_file("..\POLYGON.shp")
geometry = aoi["geometry"]
clipImg, clipImg_Transform = mask(img, geometry, crop=True, invert=False)
clipImg_Meta = img.meta.copy()
clipImg_Meta.update({"driver": "GTiff",
                     "height": clipImg.shape[1],
                     "width": clipImg.shape[2],
                     "transform": clipImg_Transform})

0 个答案:

没有答案