[GIS] Calculating difference two rasters through gdal

gdalpyqgis

I'm writing a module for Qgis I'm creating my own bitmap calculator, I need to compute the pixel values of the two rasters, I want to calculate with the help of Gdal.I create two functions Where I read all the information (how many channels, projection, coordinates, etc.) . On the output there are two variables as can now find the difference of the pixel values?

from osgeo import gdal
def inputFile():
    filepath = r"C:\Users\user2\Desktop\1\rastr calc\2012.tif"

    # Open the file:
    raster = gdal.Open(filepath)

    # Check type of the variable 'raster'
    type(raster)
    # Projection
    raster.GetProjection()

    # Dimensions
    raster.RasterXSize
    raster.RasterYSize

    # Number of bands
    raster.RasterCount

    # Metadata for the raster dataset
    raster.GetMetadata()
    # Read the raster band as separate variable
    band = raster.GetRasterBand(1)

    # Check type of the variable 'band'
    type(band)

    # Data type of the values
    gdal.GetDataTypeName(band.DataType)
    # Compute statistics if needed
    if band.GetMinimum() is None or band.GetMaximum()is None:
        band.ComputeStatistics(0)
        print("Statistics computed.")

    # Fetch metadata for the band
    band.GetMetadata()

    # Print only selected metadata:
    print ("[ NO DATA VALUE ] = ", band.GetNoDataValue()) # none
    print ("[ MIN ] = ", band.GetMinimum())
    print ("[ MAX ] = ", band.GetMaximum())

def inputFile2():
    filepath = r"C:\Users\user2\Desktop\1\rastr calc\2011.tif"

    # Open the file:
    raster = gdal.Open(filepath)

    # Check type of the variable 'raster'
    type(raster)
    # Projection
    raster.GetProjection()

    # Dimensions
    raster.RasterXSize
    raster.RasterYSize

    # Number of bands
    raster.RasterCount

    # Metadata for the raster dataset
    raster.GetMetadata()
    # Read the raster band as separate variable
    band = raster.GetRasterBand(1)

    # Check type of the variable 'band'
    type(band)

    # Data type of the values
    gdal.GetDataTypeName(band.DataType)
    # Compute statistics if needed
    if band.GetMinimum() is None or band.GetMaximum()is None:
        band.ComputeStatistics(0)
        print("Statistics computed.")

    # Fetch metadata for the band
    band.GetMetadata()

    # Print only selected metadata:
    print ("[ NO DATA VALUE ] = ", band.GetNoDataValue()) # none
    print ("[ MIN ] = ", band.GetMinimum())
    print ("[ MAX ] = ", band.GetMaximum())

How can I now take deduct the values of the 2012.tif pixel from the pixel values of 2011.tif Example of calculations as in Qgis calculator rasters

In Qgis the raster calculator, it takes pixels from the coordinates, not from the beginning of the images My task is to make the same algorithm as in the Qgis raster calculator

Example Qgis

In Qgis it looks like this

2012.tif-2011.tif=outputfile.tif Only from gdal

Best Answer

I know this is a bit late but might help someone who stumbles upon this question:

https://www.gdal.org/gdalcompare.html

Alternatively, you could use gdal_calc.py with both rasters A and B, and --calc="A-B", then simply check if the min/max values are both 0. If they're not, you can use the result to find out where they differ.

Related Question