This paper describes a system based onPythonhit the nail on the headgdal
module for a large number oflong time series (stats.)of the raster remote sensing image file, plotting a number of randomly assigned image elements in each of its bands in theTime series plotThe methodology.
In a previous post, we've covered the use of a new system based on thegdal
module for a large number ofmulti-temporalRaster images, batch drawing of pixelsTime Series Line Chartof the method. At the time, though, the article had a need for every1
Each of the time phases corresponds to3
different remote sensing image files, and each of the1
All of the remote sensing image files are only1
bands; whereas in this paper, we each1
The landscape remote sensing images all correspond to2
bands, and the multiple graphs we end up plotting come from this per1
Different bands of remote sensing images of the landscape.
Let's clarify the requirements of this article a bit more. Now there is a folder that holds a large number ofRemote sensing image filesThe following figure shows that. Among them, all remote sensing images are images of the same region and different imaging times, and their respective spatial reference information, the number of pixel rows and columns, etc., are the same, and the file names haveIndicates date of imagingspecific field; and each1
The landscape remote sensing images all have2
bands. Now we wish, within the area covered by the remote sensing image, to randomly select a number of image elements, based on which we plot their changes over time. Since each of our remote sensing images has2
bands and all want to be plotted, so the final graph will have a total of2
Strip Curve.
Once the requirements are clear, we can start writing the code. The code used in this article is as follows.
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 25 23:04:41 2023
@author: fkxxgis
"""
import os
import random
import as plt
from osgeo import gdal
def load_image(image_path):
dataset = (image_path)
band1 = (1).ReadAsArray()
band2 = (2).ReadAsArray()
del dataset
return band1, band2
def plot_time_series(image_folder, pic_folder, num_pixels):
image_files = [file for file in (image_folder) if (".tif")]
band1_merge, band2_merge = [], []
i = 0
for image_file in image_files:
image_path = (image_folder, image_file)
band1, band2 = load_image(image_path)
band1_merge.append(band1)
band2_merge.append(band2)
i += 1
x_size, y_size =
pixel_indices = (range(x_size * y_size), num_pixels)
for pixel_index in pixel_indices:
x, y = divmod(pixel_index, y_size)
band_list_1, band_list_2 = [], []
for i in range(len(band1_merge)):
band_data_1 = band1_merge[i]
band_list_1.append(band_data_1[x, y])
band_data_2 = band2_merge[i]
band_list_2.append(band_data_2[x, y])
()
(range(len(band1_merge)), band_list_1, label="Band 1")
(range(len(band1_merge)), band_list_2, label="Band 2")
("Time")
("NDVI")
(0, 1000)
(f"Time Series for Pixel at ({x}, {y})")
()
((pic_folder, str(x) + "_" + str(y)))
()
image_folder_path = "E:/02_Project/Result/test"
pic_folder_path = "E:/02_Project/TIFF/Plot"
num_pixels = 50
plot_time_series(image_folder_path, pic_folder_path, num_pixels)
The exact meaning of the above code is as follows.
First, we imported the libraries we needed to use; where theos
for handling file path and directory operations.random
for randomly selecting pixels.is used to draw the image.
Subsequently, we define the functionload_image(image_path)
This function receives an image file pathimage_path
as an input parameter. Subsequently, within the function, use thegdal
The library opens the image file and then extracts the data in its first and second bands and stores them separately in theband1
cap (a poem)band2
in. Finally, the function returns the data for both bands.
Next, we define the functionplot_time_series(image_folder, pic_folder, num_pixels)
This function takes three input parameters, which areimage_folder
、pic_folder
cap (a poem)num_pixels
. Among other things.image_folder
for a file that contains more than one.tif
The folder path of the image file in the formatpic_folder
is the path to the folder where the generated time series images will be saved, while thenum_pixels
then specifies the number of randomly selected pixels to plot the time series plot - set this parameter to a few, and we'll end up with a few resultant images.
Inside this function, we pass thefunction to obtain
image_folder
All of the items in the.tif
ending image files, and store those filenames in theimage_files
lists. Then we create two empty listsband1_merge
cap (a poem)band2_merge
The following is a list of all the image files that are used to store the2
bands of data. Next, we iterate through all the image files, load all the band data of each image file one by one, and add them to the corresponding list. Secondly, we load all the band data of each image file one by one and add them to the corresponding list using thefunction randomly selects from a range of pixel indices
num_pixels
The indexes of the pixels are saved in thepixel_indices
list. Next, we traverse and recover thepixel_indices
index of each pixel in the image, calculates the time series data for that pixel for each band in each image, and stores it in theband_list_1
、band_list_2
List.
Subsequently, we can plot two time series graphs, each representing2
values for the individual bands on different image dates. Finally, we save the images to the specified folderpic_folder
in which the naming convention isx_y
whichx
together withy
represent the horizontal and vertical coordinates of the pixel, respectively.
Execute the above code, we can see the multiple graphs we generated in the specified folder path; as shown below.
Of these, each1
The images all represent our2
The numerical trend of the individual bands over this period of time; as shown below.
At this point, the job is done.