QGIS按照矢量范围批量裁剪遥感影像

以下是使用Python代码在QGIS中按照矢量范围批量裁剪遥感影像的详细步骤:


# Import required libraries
import os
from qgis.core import (QgsVectorLayer, QgsProject, QgsRasterLayer,
                       QgsRectangle, QgsRasterFileWriter)

# Define the path to the vector layer containing the clipping polygons
vector_path = "/path/to/vector/layer.shp"

# Define the path to the directory containing the raster images to clip
image_dir = "/path/to/image/directory"

# Load the vector layer
vector_layer = QgsVectorLayer(vector_path, "Clipping Polygons", "ogr")

# Get the extent of the vector layer
extent = vector_layer.extent()

# Loop through all files in the image directory
for file_name in os.listdir(image_dir):
    # Check if file is a raster image
    if file_name.endswith(".tif"):
        # Define the path to the input raster image
        input_path = os.path.join(image_dir, file_name)

        # Load the raster layer
        raster_layer = QgsRasterLayer(input_path, file_name)

        # Set the extent of the raster layer to match the vector layer
        raster_layer.setExtent(extent)

        # Clip the raster layer using the vector layer
        output_path = os.path.join(image_dir, f"{file_name[:-4]}_clipped.tif")
        writer = QgsRasterFileWriter(output_path)
        writer.clipRasterByExtent(raster_layer, extent, True)

        # Refresh the map canvas
        iface.mapCanvas().refresh()
 

这段代码将加载矢量图层,获取其范围,并遍历指定目录中的所有遥感影像文件。对于每个遥感影像文件,代码将加载该文件作为栅格图层,并将其范围设置为与矢量图层相同。然后,代码将使用矢量图层裁剪栅格图层,并将输出保存到与输入文件相同的目录中,但文件名以“_clipped”结尾。最后,代码将刷新QGIS地图画布以显示新的裁剪栅格图层。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
【资源说明】 基于Qgis二次开发的遥感图像云检测及地物恢复系统python源码.zip基于Qgis二次开发的遥感图像云检测及地物恢复系统python源码.zip基于Qgis二次开发的遥感图像云检测及地物恢复系统python源码.zip基于Qgis二次开发的遥感图像云检测及地物恢复系统python源码.zip基于Qgis二次开发的遥感图像云检测及地物恢复系统python源码.zip基于Qgis二次开发的遥感图像云检测及地物恢复系统python源码.zip基于Qgis二次开发的遥感图像云检测及地物恢复系统python源码.zip基于Qgis二次开发的遥感图像云检测及地物恢复系统python源码.zip基于Qgis二次开发的遥感图像云检测及地物恢复系统python源码.zip基于Qgis二次开发的遥感图像云检测及地物恢复系统python源码.zip基于Qgis二次开发的遥感图像云检测及地物恢复系统python源码.zip基于Qgis二次开发的遥感图像云检测及地物恢复系统python源码.zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

认真学GIS

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

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

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

打赏作者

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

抵扣说明:

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

余额充值