# How to obtain the X, Y difference (RMSE) between two rasters in QGIS?

How to obtain the X, Y difference (RMSE) between two rasters in QGIS?

There are two rasters with the same pixel size and the same number of pixels in rows and columns.

Each of these two rasters has a red and blue band

When the two rasters were superimposed, the positions of each pixel did not match exactly as shown.

Is there a way to determine how different each pixel of the two rasters is by X and Y? I want to get the RMSE, the degree of dislocation when the two rasters are overlapped.

• You mention both vector and raster in your question, can you clarify which you are referring to? The answer provided assumes raster. Commented Apr 8, 2022 at 6:08

It seems like you want the difference in x and y coordinates?

The following will return that using the top left corner point as a reference, like your diagram shows.

You will need to load the raster to your QGIS session as RASTER_A and RASTER_B or modify the script here to reference their names as you have them stored. Run in the python console:

In the case shown below it is showing the shift between RASTER_A in colour and RASTER_B in grey, both in units as well as % of the respective pixel size. i.e. here RASTER_B is shifted east by ~2 units or ~112% and shifted north by ~1.3 units or ~74%.

``````rlayer_1 = QgsProject.instance().mapLayersByName('RASTER_A')[0]
rlayer_2 = QgsProject.instance().mapLayersByName('RASTER_B')[0]

#GET BBOX OF RASTER 1
ext_1 =rlayer_1.extent()
# get the origin points of the raster
origin_1 = [ext_1.xMinimum(), ext_1.yMaximum()]

#GET BBOX OF RASTER 2
ext_2 = rlayer_2.extent()
# get the origin points of the raster
origin_2 = [ext_2.xMinimum(), ext_2.yMaximum()]

#GET DIFFERENCE BETWEEN RASTER 1 AND RASTER 2
difshift= [origin_2 - origin_1 for origin_2, origin_1 in zip(origin_2, origin_1)]
#PIXEL SIZE:
pixelhightx = rlayer_1.rasterUnitsPerPixelX()
pixelhighty = rlayer_1.rasterUnitsPerPixelY()
#GET DIFFERENCE BETWEEN RASTER 1 AND RASTER 2 as % of pixelheight
difpercentage = [(difshift[0]/pixelhightx)*100, (difshift[1]/pixelhighty)*100]

#print(difunits)
print("X SHIFT units: %s units" %(difshift[0]))
print("Y SHIFT units %s units" %(difshift[1]))

#print(difpercentage)
print("X SHIFT %%: %s%%" %(difpercentage[0]))
print("Y SHIFT %%: %s%%" %(difpercentage[1]))
``````