GIS and RS processing - Notes on fate

Posted by rinteractive on Mon, 27 Dec 2021 04:21:34 +0100

#Write 16 bit data into 8 bit data
#2021-08-07
#tif2-jpg
#version1/0

# from osgeo import gdal
import os
import imageio

#initialize 
Working_dir = './move_dir'
New_dir = './png_files'
files = os.listdir(Working_dir)

def Tif2Png(Working_dir,New_dir):
    files = os.listdir(Working_dir)
    #loop
    for i in range(len(files)):
        #get the filename from list
        file_name = files.pop()
        #Raster = gdal.Open(True_file_name)
        #Array = Raster.ReadAsArray(xsize=512,ysize=512)
        True_file_name = os.path.join(Working_dir,file_name)
        image = imageio.imread(True_file_name)
        image = image.astype('uint8')
        #Write the files
        Out_filename = os.path.join(New_dir,file_name)
        imageio.imwrite(Out_filename,image)
    print('Finish the processing')
            

if __name__ == "__main__":
    #Main_pro
    Tif2Png(Working_dir,New_dir)
    

Batch convert the tif file from 16 bit to 8 bit, and write it out

   since the vector of remote sensing image or OSM is mostly 16 bit data after being transferred out, png or jpg images need to be used during DL training, and since the coordinate system is lost during training, the projection coordinate system can be automatically added to the image later, so you can directly use the imageio module to read tif, write png, and then use imageio Astype ('uint8 ') is forcibly transformed. Of course, this will lead to the loss of multispectral data or hyperspectral information of some remote sensing images in DL. The original tif diagram has a coordinate system, but the coordinate system is lost after writing. Of course, you can use the gdal module to write tif. It's not necessary. DL only needs spectral information for information.

   in the early stage, when processing data, it is mainly vector to grid. However, in the process of vector to grid, the background processing in the geographic processing option needs to be turned off, otherwise the rotation is particularly slow. At the same time, the sampling size needs to be customized. If the resolution is too high, it will lead to a large amount of data, which needs to be cut in frames before exporting tif, Then the png diagram is generated. The vector is converted to grid and cropped. You can use ArcGIS, ENVI and envi. If the label needs mask, you can use envi's band math for calculation, that is, the so-called logical calculation. You can see a black in an ordinary photo viewer, but you can see the above options only by viewing tif in GIS or envi.
Plus: when envi processes data, set nodata to 0 in band math. If ArcGIS clipping is used, nodata will appear. Of course, there are many methods of ArcGIS clipping. For example, there are 23 G data processed this time. If you use python's gdal library to read the clipping, you can't load it. You can only select a small part first, and then load it. After loading, you can set the repetition rate clipping with the clipping method of a great God. Then you need to use the code to check whether it becomes all 0 due to too few building samples. If you use ArcGIS to check, Or ordinary photo viewer will be very troublesome, so you can view it with gdal library through NP Sum (array = = 1) method to check the number of 1 values, and then move it to a new folder. It mainly uses OS, shutil and gdal modules. The code is posted along with it. A lot of things

#Move the image element larger than 4000 to a new folder
#author:Neverland!
#data:2021-08-06
#version1.0
from osgeo  import gdal
import os
import numpy as np 
import shutil 

# initialize
list_dir = './samples/'
dir = os.listdir(list_dir)
new_dir = './move_dir/'
#rename_index
index = 0 


# def read_raster():
#     try:
for i in range(len(dir)):
    files = dir.pop()
    filename = os.path.join(list_dir,files)
    Raster = gdal.Open(filename)
    #Open shadow image correctly
    Array = Raster.ReadAsArray(xsize=512,ysize=512)
    #Judge the number of buildings
    if (np.sum(Array ==1))>4000:
            index += 1 
            new_aftname = str(index) + '.tif'
        #Memory needs to be destroyed, otherwise an error of occupying memory will be reported
            Raster = None
        #The path to the new folder and move it
            new_path = os.path.join(new_dir,files)
            shutil.move(filename,new_path) 
            print(new_path,'Successfully moved to the new folder')
            old_name = os.path.join(new_dir,files)
            print('The path of the new file is' + new_path)
            new_name = os.path.join(new_dir,new_aftname)
            os.rename(old_name,new_name)
            print('The file name was successfully by' + files + 'Change to' + new_aftname )

  original list Pop () is to pop out the elements from the end to the end. The file names are out of order, and now the number of pixels > 4000 has been in order. Check that all TIFS have been moved out and named sequential TIFS by the original