Python 按照shp行政边界切割tif的两种方式

介绍两种按照shp行政边界切割tif文件的方式

1. gdal.Warp


from osgeo import gdal

shp_path="shp文件路径"
path="tif数据路径"

#读取shp
shp_dataset = gdal.OpenEx(shp_path, gdal.OF_VECTOR)
shp_layer = shp_dataset.GetLayer()
shp_srs = shp_layer.GetSpatialRef().ExportToWkt()
# 读取tif
driver = gdal.GetDriverByName("GTiff")
dataset = driver.Create(path, len(new_im_data[0]), len(new_im_data), 1, gdal.GDT_Float32)
# 我的shp文件中有多个城市  所以我这里是循环切割每个城市
for city_layer in shp_layer:
        print(city_layer.name)
        # 执行裁剪 output_dataset裁剪后的数据  获取数据第一个参数写'',需要保存到文件直接写保存路径format可不写
        output_dataset = gdal.Warp('',
        									# 原始数据
                                             dataset,
                                             # 保存到内存
                                             format='MEM',
                                             cropToCutline=True,
                                             # srs可以自定义或者使用shp文件中srs
                                             # dstSRS='EPSG:4326',
                                             dstSRS=shp_srs,
                                             #shp文件地址
                                             cutlineDSName=shp_path,
                                             # 查询条件 Name是shp文件中的字段  查询Name=XXX   shp中只有一个city可以不使用这个参数
                                             cutlineWhere=f'"Name"=\'{city_layer.name}\'',
                                             #指定重采样的方法 
                                             resampleAlg='bilinear',
                                             # 原始数据无效值
                                             srcNodata=-99,
                                             # 新数据设置无效值
                                             dstNodata=np.nan
                                             )
        output_data = output_dataset.ReadAsArray()

2. rasterio.mask

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

# 打开 shapefile 文件
shapefile_path = 'path/to/shapefile.shp'
shapefile = gpd.read_file(shapefile_path)

# 打开要裁剪的 GeoTIFF 文件
tiff_path = 'path/to/input.tif'
with rasterio.open(tiff_path) as src:
    # 使用 mask 函数进行裁剪
    out_image, out_transform = mask(src, shapefile.geometry, crop=True)
    out_meta = src.meta.copy()

# 更新裁剪后图像的元数据
out_meta.update({"driver": "GTiff",
                 "height": out_image.shape[1],
                 "width": out_image.shape[2],
                 "transform": out_transform})

# 将裁剪后的图像保存到新文件中
output_path = 'path/to/output.tif'
with rasterio.open(output_path, "w", **out_meta) as dest:
    dest.write(out_image)
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr . zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值