Location>code7788 >text

Python's GDAL library for plotting multi-band, long time-series remote sensing image time profiles

Popularity:930 ℃/2024-08-03 09:35:30

  This paper describes a system based onPythonhit the nail on the headgdalmodule 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 thegdalmodule 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 every1Each of the time phases corresponds to3different remote sensing image files, and each of the1All of the remote sensing image files are only1bands; whereas in this paper, we each1The landscape remote sensing images all correspond to2bands, and the multiple graphs we end up plotting come from this per1Different 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 each1The landscape remote sensing images all have2bands. 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 has2bands and all want to be plotted, so the final graph will have a total of2Strip Curve.

image

  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 theosfor handling file path and directory operations.randomfor randomly selecting pixels.is used to draw the image.

  Subsequently, we define the functionload_image(image_path)This function receives an image file pathimage_pathas an input parameter. Subsequently, within the function, use thegdalThe library opens the image file and then extracts the data in its first and second bands and stores them separately in theband1cap (a poem)band2in. 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_folderpic_foldercap (a poem)num_pixels. Among other things.image_folderfor a file that contains more than one.tifThe folder path of the image file in the formatpic_folderis the path to the folder where the generated time series images will be saved, while thenum_pixelsthen 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 obtainimage_folderAll of the items in the.tifending image files, and store those filenames in theimage_fileslists. Then we create two empty listsband1_mergecap (a poem)band2_mergeThe following is a list of all the image files that are used to store the2bands 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 indicesnum_pixelsThe indexes of the pixels are saved in thepixel_indiceslist. Next, we traverse and recover thepixel_indicesindex 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_1band_list_2List.

  Subsequently, we can plot two time series graphs, each representing2values for the individual bands on different image dates. Finally, we save the images to the specified folderpic_folderin which the naming convention isx_ywhichxtogether withyrepresent 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, each1The images all represent our2The numerical trend of the individual bands over this period of time; as shown below.

  At this point, the job is done.