首页 > 解决方案 > 根据 shapefile 裁剪多个 tiff 图像并将所有裁剪的图像保存在一个文件夹中

问题描述

大家好,我是python的初学者。我想根据 shapefile 裁剪我的所有 tiff 图像,并将它们保存在一个文件夹中。我已经尝试了下面的代码来做到这一点。这仅保存一个图像 output.tiff。我想我需要更改第 3 步中的代码。任何有专业知识的人请提出建议,以根据 shapefile 从文件夹中裁剪所有 tiff 图像并将所有图像保存到文件夹中。谢谢你的帮助。

import fiona
import rasterio
import rasterio.mask
from rasterio.plot import show
from rasterio.warp import calculate_default_transform, reproject, Resampling
import glob

# step 1 Reading Shape file
with fiona.open("BD_boundary.shp", "r") as shapefile:
    shapes = [feature["geometry"] for feature in shapefile]

# step 2 Reading all images    
image_list = []
for filename in glob.glob('c_gls_DMP_QL_*_GLOBE_VGT_V2.0.1.tiff'): 
     im=rasterio.open(filename)
     image_list.append(im)
     out_image, out_transform = rasterio.mask.mask(im, shapes, crop=True)
     out_meta = im.meta
     out_meta.update({"driver": "GTiff",
                     "height": out_image.shape[1],
                      "width": out_image.shape[2],
                       "transform": out_transform})
 # step 3 Saving cropped images
   with rasterio.open('output.tiff', "w", **out_meta) as dest:
        dest.write(out_image)

标签: imagesavecroptiffshapefile

解决方案


推荐阅读