我正在尝试平铺一个大图像(.img格式,但可能是geotiff),但是我已经使用rasterio mask裁剪了图像,它返回一个蒙版数组和一个单独的Affine对象。
from rasterio import mask
import fiona
image = rasterio.open(image_path)
with fiona.open(shapefile_path, 'r') as shapefile:
cropping_polygon = [polygon['geometry'] for polygon in shapefile]
smaller_image, smaller_image_affine = mask.mask(image, cropping_polygon, crop=True)
现在我想将smaller_image
拆分为固定大小的磁贴。我看过rasterio窗口的读写,但这似乎依赖于具有image.affine
属性的图像,以免丢失地理参考。
是否可以平铺蒙面数组,并为每个图块生成新的仿射?
答案 0 :(得分:2)
我认为您正在寻找In [3752]: s
Out[3752]:
0 A
1 a1
2 b1
3 c1
4 B
5 a2
6 b2
7 c2
dtype: object
In [3753]: L
Out[3753]: ['A', 'B']
。
rasterio.windows.transform
然后使用tile_window = rasterio.windows.Window(0, 0, 256, 256)
tile_affine = rasterio.windows.transform(tile_window, smaller_image_affine)
tile_image = smaller_image[(slice(None),) + tile_window.toslices()]
和tile_image
,您可以将所有内容写入新文件。