February** Dynamic Incentive Program This article describes the basis of:pythonarcpy
module, based on oneLarge foldersand go through each of themSubfoldersin all of themRemote sensing image raster fileand will be the originalEvery sceneRemote sensing image filesQuatilecut, or cropped into otherSpecify the number of small piecesmethod.
First, let's clarify our needs. There is an existing oneLarge folders, which has more than oneSubfolders, as shown in the figure below.
of these, each and every oneSubfolders, are contained in large quantitiesRaster remote sensing image file(We'll take it here.).tif
format of raster image files for example); For example, feel free to open any of the above diagramsSubfolders, all as shown in the figure below.
What we hope to achieve is that each oneSubfoldersRemote sensing imagery per scene, all of which are cropped and cut to divide the original remote sensing image into new onesportions, equivalent to yesQuatile。Here we want to take the original imageIt's okay to divide it into several parts, that is, the original image can be divided into
n * m
sections.
With the requirements understood, we can begin to write. The ** used in this paper is actually similar to the idea mentioned in our previous article Python batch cutting remote sensing images into multiple small rectangles, and Arcpy divides raster remote sensing images into multiple blocks according to vector layers, but it is different in terms of file reading and cropping parameter settings. If you need to, you can check out the above two articles first. The ** used in this article is as follows.
# -*coding: utf-8 -*
3created on mon aug 28 22:39:52 2023
5@author: fkxxgis
8import os
9import arcpy
11tif_file_path = r"e:/02_project/202307_ndviproduce/beijing_preprocessing/original"
12result_file_path = r"e:/02_project/202307_ndviproduce/beijing_preprocessing/four_result"
13arcpy.env.parallelprocessingfactor = 0
15for root, dirs, files in os.walk(tif_file_path):
16 for dir_name in dirs:
17 dir_path = os.path.join(root, dir_name)
18 arcpy.env.workspace = dir_path
19 tif_file_list = arcpy.listrasters("*", "tif")
21 for tif_file in tif_file_list:
22 arcpy.splitraster_management(tif_file,23 result_file_path,24 tif_file.split(".tif")[0] +"_",25 "number_of_tiles",26 "tiff",27 "bilinear",28 "2 2",29 "##",31 "pixels",32 "#####
38 print(dir_path)
First, we need to set the input and output folder paths; Among them,tif_file_path
Indicates the storage of the original remote sensing imageryLarge folderspath, whileresult_file_path
indicates the path of the folder where the split result is stored. Subsequently, here you need to set it uparcpy
environmental parameters, passedarcpy.env.parallelprocessingfactor = 0
This sentence disables parallel processing to ensure that there are no problems during processing - for the principle of this setting, please refer to the article ArcGIS ArcMap Split Raster Tool does not have a result solution (that is.
Subsequently, we use:os.walk
to iterate through all of the source foldersSubfolders;For eachSubfolders, Settingsarcpy
The working environment is thatSubfoldersso that we can use itarcpy.listrasters
Get this oneSubfoldersin all.tif
format of the remote sensing imagery file.
Next, for each remote sensing image, usearcpy.splitraster_management
function to slice it. Among them, the first parametertif_file
is the path of the remote sensing image to be split, the second parameterresult_file_path
is the path to the folder where the result is saved after splitting, and the third parametertif_file.split(".tif")[0] +"_"
is the prefix of the output file, which is removed using the original file name here.tif
suffix, and add an underscore at the end; The next parameters are used to set the splitting method, output format, interpolation method, and so on. Here we choose to split the remote sensing imagery intoblock (if you need to split the remote sensing image into other quantities, you can modify it here), and the interpolation method is
bilinear
, and the output format istiff
。One last parameterRepresents the original remote sensing imagery
value as for the small image after cuttingnodataValue.
Running the above **, we can see that each remote sensing image has been divided into sections in the results folderand each document here is followed by a numeric suffix (the numeric suffix is from
Start the math, and that's what it is in this article
with
);, as shown in the figure below.
Next, if we wish to get theseSmall remote sensing imageryAccording to the characteristics of the file name, copy and cut to other folders, you can refer to the article python to copy a large number of different files to multiple specified folders according to the characteristics of the file name, and copy each file to the corresponding folder in combination with the remote sensing image file name: the ** mentioned in python is automatically implemented. That's it.