Location>code7788 >text

Python method for cropping multiple remote sensing images with shp files

Popularity:704 ℃/2024-07-24 13:08:24

  This paper describes a system based onPythoncenterArcPymodule, based on thevector datarange, for a large number ofraster remote sensing image(before polysyllabic verb) handle itBatch cut maskThe methodology.

  First, without further ado, the code needed for this article is shown below.

# -*- coding: utf-8 -*-
"""
Created on Tue Dec 13 20:07:48 2022

@author: fkxxgis
"""

import arcpy
from  import *

tif_file_path = "E:/AllYear/Original/"
clip_file_path = "E:/AllYear/Clip/"
shp_file_name = "E:/AllYear/"
 = tif_file_path

tif_file_name = ("*", "tif")

for tif_file in tif_file_name:
    key_name = tif_file.split(".tif")[0] + "_C.tif"
    clip_file_name = clip_file_path + key_name
    clip_file = ExtractByMask(tif_file, shp_file_name)
    clip_file.save(clip_file_name)

  Among them.tif_file_pathIndicates the save path of the raster file to be cropped.clip_file_pathIndicates the save path of the raster file after cropping.shp_file_nameIndicates the spatial extent vector file to be based on when cropping.

  The overall idea of the code is also simple: first, we base the code on the()function to get thetif_file_pathAll the existing ones under the path.tifformat and stored as a list of image files in thetif_file_namein it; then, one by one, remove thetif_file_nameThe raster files in the list are cropped. Among them, because it is a batch operation, it is necessary to name each output cropped raster file separately; we will first intercept the original raster file name by means of string interception of the.tifThe entire content before the suffix is retained and a field is added after it_C, indicating that it is a cropped raster file, and as the respective name of the cropped raster file. Subsequently, the raster files are displayed by theExtractByMask()function, based on the vector data, to crop the raster file and finally to pass the.save()function is saved.

  With the above code, we can add theclip_file_pathpath to see the batch cropped raster remote sensing image files.

  It's important to note here that since we're using theArcPymodule, so if everyone'sPythonThe version is3.0and above, it is necessary toArcMapsoftwarePython runtime boxor its equivalentIDLE(shown below) to run the above code.

  At this point, the job is done.