基于python的遥感影像裁剪

根据shp图对遥感影像进行裁剪,shp图有多个面的话将每个面裁剪出来并根据属性命名

数据准备

假设我们有一张多光谱影像和一个shp图,shp是一些取样点或者小区的面信息

小区取样点

这个shp中的属性表信息如下:

shp图属性表

这里面其实只需要name信息即可

具体代码

# coding=utf-8
"""
Author: Shuaijie
Blog: https://blog.csdn.net/weixin_45452300
公众号:AgbioIT
date: 2023/12/8 16:30
desc: 根据画的shp图把每个shp的影像裁剪出小图
"""


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

# 读取Shapefile文件
shp_file = 'path/to/image.tif'
shape_data = gpd.read_file(shp_file)

# 读取多光谱影像
image_file = 'path/to/shapefile.shp'

image_data = rasterio.open(image_file)


# 获取影像的元数据
meta = image_data.meta.copy()

# 遍历每个区域
for index, region in shape_data.iterrows():
    # 获取区域的几何对象
    geometry = region['geometry']
    if geometry == None:
        continue
    name = region['name']
    # 使用几何对象创建遮罩
    mask_shape = geometry.__geo_interface__
    masked_image, masked_transform = mask(image_data, [mask_shape],  crop=True)

    # 更新元数据的影像尺寸
#     meta.update({'height': masked_image.shape[1],
#                  'width': masked_image.shape[2]})
# 更新元数据的影像尺寸及投影坐标信息
    meta.update({
   'transform': masked_transform,
                 'crs': image_data.crs, 'height': masked_image.shape[1],
                 'width': masked_image.shape[2]})

    # 创建输出影像文件
    output_file = 'path/to/imageflod/%s.tif'%name
    with rasterio.open(output_file, 'w', **meta) as dst:
        dst.write(masked_image)

# 关闭影像文件
image_data.close()

结果

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-10 04:08:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-10 04:08:04       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-10 04:08:04       82 阅读
  4. Python语言-面向对象

    2023-12-10 04:08:04       91 阅读

热门阅读

  1. 数据库基础学习03计算机二级-第三章 数据定义

    2023-12-10 04:08:04       53 阅读
  2. 85. 最大矩形

    2023-12-10 04:08:04       62 阅读
  3. 力扣labuladong一刷day31天二叉树

    2023-12-10 04:08:04       54 阅读
  4. 在Go中使用循环时使用Break和Continue语句

    2023-12-10 04:08:04       58 阅读
  5. 汽车网络安全--ISO\SAE 21434解析(一)

    2023-12-10 04:08:04       59 阅读
  6. Environment Variables Used by GPUDirect Storage

    2023-12-10 04:08:04       59 阅读
  7. 解释 Git 的基本概念和使用方式。

    2023-12-10 04:08:04       38 阅读
  8. 12.5每日一题(备战蓝桥杯小数运算、拆位练习)

    2023-12-10 04:08:04       37 阅读
  9. 【Spring篇】切点表达式语法规范

    2023-12-10 04:08:04       43 阅读