Home  >  Article  >  Backend Development  >  How Python ArcPy implements batch splicing of long-term series raster images

How Python ArcPy implements batch splicing of long-term series raster images

PHPz
PHPzforward
2023-04-28 20:16:051330browse

First, let’s clarify the specific needs of this article. There is a folder that stores a large number of remote sensing images in the .tif format. The file name of each remote sensing image contains the imaging time of the image, as shown in the figure below.

How Python ArcPy implements batch splicing of long-term series raster images

## We hope to splice the remote sensing images imaged on the same day - for example, the picture above has

2001 year 185 There are 10 remote sensing images imaged on day , each of which is an image of this day at a different spatial position; at the same time, there are 193 days of 2001 10 of remote sensing images. We hope to first splice the 10 remote sensing images imaged on day 185, and then stitch the 10 images imaged on day 193 Remote sensing images are stitched together, and so on. When the overall number of remote sensing images is small, we may be able to manually stitch them one by one; but when the number of images is large, we need to use code to achieve it.

After clarifying the requirements, we can start specific operations. First, the code required for this article is as follows.

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 15 13:21:55 2022

@author: fkxxgis
"""

import os
import arcpy

tif_file_path="E:/LST/Data/NDVI/02_TIFF/"
out_file_path="E:/LST/Data/NDVI/03_Mosaic/"
arcpy.env.workspace=tif_file_path

tif_file_name=arcpy.ListRasters("*","tif")
tif_file_date=tif_file_name[0][1:8]
one_day_tif_list=[]

tif_file_example_path=tif_file_path+tif_file_name[0]
cell_size_x=arcpy.GetRasterProperties_management(tif_file_example_path,"CELLSIZEX")
cell_size=cell_size_x.getOutput(0)
value_type=arcpy.GetRasterProperties_management(tif_file_example_path,"VALUETYPE")
describe=arcpy.Describe(tif_file_example_path)
spatial_reference=describe.spatialReference

for tif_file in tif_file_name:
    if tif_file[1:8]==tif_file_date:
        one_day_tif_list.append(tif_file)
        tif_file_temp=tif_file
        if tif_file==tif_file_name[len(tif_file_name)-1]:
            out_file_name=tif_file[1:8]+".tif"
            arcpy.CreateRasterDataset_management(out_file_path,out_file_name,
                                                 cell_size,"16_BIT_SIGNED",spatial_reference,"1")
            out_file=out_file_path+out_file_name
            for tif_file_new in one_day_tif_list:
                arcpy.Mosaic_management([tif_file_path+tif_file_new],out_file)
                
    else:
        out_file_name=tif_file_temp[1:8]+".tif"
        arcpy.CreateRasterDataset_management(out_file_path,out_file_name,
                                             cell_size,"16_BIT_SIGNED",spatial_reference,"1")
        out_file=out_file_path+out_file_name
        for tif_file_new in one_day_tif_list:
            arcpy.Mosaic_management([tif_file_path+tif_file_new],out_file)
        one_day_tif_list=[]
        one_day_tif_list.append(tif_file)
        tif_file_date=tif_file[1:8]

Among them,

tif_file_path is the saving path of the original remote sensing image before splicing, and out_file_path is the saving path of our newly generated remote sensing image after splicing.

Here, we need to first sort the files under the

tif_file_path path in the resource manager by "name"; then, use The arcpy.ListRasters() function obtains all the original image files in the .tif format under the path, and intercepts part of the file name of the first file to obtain its imaging time; Next, be prepared to create a new raster file. The meaning of this part of the code has been mentioned in the article Python arcpy creates rasters and batch splicing rasters mentioned at the beginning of this article, so I won’t do it here. Repeat.

Next, traverse all

.tif format image files under the tif_file_path path. Among them, we use a simple judgment statement to determine whether the remote sensing images at a certain imaging time have been read - if they have been read, for example, if the 10 remote sensing images imaged on day 185 The images have all been traversed, then the ten remote sensing images are spliced; if the reading has not been completed, for example, if the 10 remote sensing images imaged on day 185 are currently traversed to the 8th one, Then don’t splice and continue traversing down.

I believe everyone here has also seen why we have to sort the files in the folder according to "

name" in the early stage - to ensure that all files at the same imaging time are The remote sensing images are all arranged together. As long as a new imaging time is encountered during traversal, the program will know that all images of the previous imaging time have been traversed. All raster images from the last imaging time can be spliced.

Finally, use

tif_file==tif_file_name[len(tif_file_name)-1] to confirm whether the last image in the folder has been traversed document. If so, you need to stitch all the images at the current imaging time and complete the running of the code.

Run the code in

IDLE (Python GUI). After the code runs, let's take a look at the results folder. It can be seen that the images are already spliced ​​separately according to the imaging time.

How Python ArcPy implements batch splicing of long-term series raster images

The above is the detailed content of How Python ArcPy implements batch splicing of long-term series raster images. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete