image - 根据 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)
解决方案
推荐阅读
- loops - 如何将前导零添加到宏变量?
- matlab - MATLAB如何确保在我的色标中不考虑颤动?
- reactjs - “提交”按钮需要点击两次才能提交 React Js
- sql - 如何在sql server的变量中包含2个单引号
- r - 如何在使用 Rshiny 输入可编辑数据表时冻结行?
- opengl-es - OpenGL ES 3.x 如何(高效地)使用 alpha 混合和早期拒绝遮挡片段从前到后渲染混合三角形?
- html - HTML/CSS - 如何使全屏 videoJS 播放器适合浏览器窗口?
- ansible - 将 Ansible stat 的 mtime 输出转换为 YYMMDD-HHMMSS
- javascript - Python“服务器”+ JS客户端websocket 1006错误
- python - 对字典中列表的某些值求和