0

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

  • is python an option? – Leo Jul 17 at 9:29
  • QGIS raster calculator uses GDAL so I guess yes – Yoni Verhaegen Jul 17 at 9:35
  • do the rasters have the same extent? are the gaps nodata? – Leo Jul 17 at 9:38
  • Both yes. I would like to define a new raster for pixels that have ice mask = 1 and velocity > 0. – Yoni Verhaegen Jul 17 at 9:40
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.