您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页使用rasterio库来对tif进行矢量裁剪操作

使用rasterio库来对tif进行矢量裁剪操作

来源:化拓教育网

经验证,比 gdal.Warp速度更快

import rasterio
from rasterio.mask import mask
import geopandas as gpd

# 输入数据集路径
input_path = dr
# 输出数据集路径
output_path = outraster1
# 矢量数据集路径
cutline_path = "TKXJFG2022shp"

# 打开输入数据集
with rasterio.open(input_path) as src:
    # 打开矢量数据集
    cutline_gdf = gpd.read_file(cutline_path)
    
    # 裁剪栅格数据
    out_image, out_transform = mask(src, cutline_gdf.geometry, crop=True)

    # 更新元数据信息
    out_meta = src.meta.copy()
    out_meta.update({
        "height": out_image.shape[1],
        "width": out_image.shape[2],
        "transform": out_transform
    })

    # 保存裁剪后的栅格数据
    with rasterio.open(output_path, "w", **out_meta) as dest:
        dest.write(out_image)

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务