TransWikia.com

Give non-overlapping parts of 2 rasters a value 0 (QGIS)

Geographic Information Systems Asked by Yoni Verhaegen on January 10, 2021

Is there a way in QGIS to give non-overlapping parts of 2 rasters a value of 0 in the new raster file?

I would like the data gaps in the velocity file (so where Ice mask is displayed on the chart) to be zero in a new raster file, so a new raster with the original value of velocity for pixels that have ice mask = 1 and velocity > 0, else 0.

enter image description here

One Answer

in pure python this code does what you are asking for:

from osgeo import gdal    

ds = gdal.Open(yourotherraster)
band = ds.GetRasterBand(1)
arrA = band.ReadAsArray()
ds = gdal.Open('yourraster')
band = ds.GetRasterBand(1)
arrB = band.ReadAsArray()
arrB = np.where((arrA == 1), 2, arrB) #your condition
[cols, rows] = array.shape
driver = gdal.GetDriverByName("GTiff")
outdata = driver.Create('youroutputraster.tif', rows, cols, 1,
                                    gdal.GDT_UInt16)
outdata.SetGeoTransform(ds.GetGeoTransform())  ##sets same geotransform as input
outdata.SetProjection(ds.GetProjection())  ##sets same projection as input
outdata.GetRasterBand(1).WriteArray(array)
outdata.GetRasterBand(1).SetNoDataValue(0)  ##if you want these values transparent
outdata.FlushCache()  ##saves to disk!!
outdata = None
band = None
ds = None
i=0

Answered by Leo on January 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP