从多个光栅输入裁剪同一地理多边形

2024-10-01 09:29:48 发布

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

我试图定位geotif光栅,以便我可以提取相同区域的光栅数据,从而产生相同大小的输出图像。你知道吗

这个想法是能够创建一个重叠卫星照片的时间点,有些照片只包含重叠的部分,所以我需要对齐和裁剪/不管他们。你知道吗

我正在使用光栅目前,但我没有锁定使用它。 我正在尝试将两个光栅放置在下面:

因此它们将在同一坐标系中对齐。 这样做的目的是获得固定大小的输出,其中只获取输入光栅中位于固定大小区域内的像素。你知道吗

顺便说一句,坐标系是WGS 84/UTM 32N区(电子邮政编码:32632). 你知道吗

安装依赖项:

pip install rasterio, numpy, pillow

示例:

import rasterio
from shapely.geometry.polygon import Polygon
from rasterio.mask import mask
from rasterio.plot import reshape_as_image
from PIL import Image


coords = [(331500,6171471),(480444,6171471), (480444,6320415),(331500,6320415)]
copenhagen_poly = Polygon(coords)

img_1 = rasterio.open('1_out.tif')
cropped_img_1, out_transform = mask(img_1, shapes=[copenhagen_poly], crop=True)
img_1 = reshape_as_image(img_1)
img_1 = Image.fromarray(img_1)

img_2 = rasterio.open('1_out.tif')
cropped_img_2, out_transform = mask(img_2, shapes=[copenhagen_poly], crop=True)
img_2 = reshape_as_image(img_2)
img_2 = Image.fromarray(img_2)

assert img_1.size, img_2.size == ((1803, 1997), (1948, 2000))

正如你所看到的大小不匹配,这意味着我无法提取完全相同的多边形,无论它是否包含数据,到一个固定大小的结果图像。你知道吗


Tags: fromimageimport区域imgas光栅mask