Assuming that borewells’s elevations (in my example a constant raster with value 2000) and elevation of the bottom of the first geological layer (in my example a DEM raster) are two rasters with the same resolution and extension (both projected in meters), next PyQGIS code was tested to get the volume in km3.
registry = QgsMapLayerRegistry.instance()
bore_elev = registry.mapLayersByName('borewells_elevations')
first_geol = registry.mapLayersByName('first geological layer')
extent = bore_elev[0].extent()
rows = bore_elev[0].height()
columns = bore_elev[0].width()
xsize = bore_elev[0].rasterUnitsPerPixelX()
ysize = bore_elev[0].rasterUnitsPerPixelY()
cell_area = xsize*ysize
block1 = bore_elev[0].dataProvider().block(1, extent, columns, rows)
block2 = first_geol[0].dataProvider().block(1, extent, columns, rows)
sum = 0
print "Processing..."
for i in range(rows):
for j in range(columns):
sum += (block1.value(i,j) - block2.value(i,j))
print "volume:{:.2f} km3".format(sum*cell_area/1e9)
After running it at the Python Console of QGIS, I got:
>>>execfile(u'/home/zeito/pyqgis_scripts/calculating_volume.py'.encode('UTF-8'))
Processing...
volume:1275.13 km3