我正在尝试创建一个陆地遮罩,用于应用于卫星图像,将与陆地质量相交的栅格像素设置为0。
在尝试使用gdal、skimage、pyplot等工具后,我发现rasterio cookbook中提供的方法快速简便。但是,它将多边形外的像素设置为0,而我正试图做相反的操作。
如果可能,请继续使用rasterio——您无需计算地理空间坐标的像素位置或处理超出栅格范围并变为负数的剪切特征。这也很快速,对于我正在处理的原始影像文件大小非常重要。
我的代码如下:
import fiona
import rasterio
from rasterio.tools.mask import mask
with fiona.open("/Users/Cate/UK_Mainland.shp", "r") as shapefile:
geoms = [feature["geometry"] for feature in shapefile]
with rasterio.open("jan_clip.tif") as src:
out_image, out_transform = mask(src, geoms, crop=True)
out_meta = src.meta.copy()
out_meta.update({"driver": "GTiff",
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform})
with rasterio.open("masked2.tif", "w", **out_meta) as dest:
dest.write(out_image)
我该如何遮盖与多边形相交的区域而不是未相交的区域?答案: 我该如何遮盖与多边形相交的区域而不是未相交的区域?