When I open a raster stack with GDAL and call it as a numpy array, lines with 'no data values' also appear. Since I do not want to include these 'no data values' (mine is 128) in the calculations I will make, I am looking for a way to prevent.

Is there a way to prevent 'no data values' from getting into numpy arrays when opening the raster stack? Or what would you recommend?

My codes are here:

outvrt = ('result/raster_stack_vrt.tif')
outtif = ('result/raster_stack.tif')
tifs = glob.glob('data/*.tif')
outds = gdal.BuildVRT(outvrt, tifs, separate = True)
outds = gdal.Translate(outtif, outds)

1 Answer 1

> import rasterio 
> import numpy as np

You can create a mask with numpy:

  1. Open the raster like a numpy array then run this code and plot the raster.


raster = rasterio.open(inputpath_raster)
raster = raster.read(1)
value = 0

raster = raster.astype('float32') # You can change the format
raster_copy = copy.copy(raster)
raster_copy[raster == value] = np.nan  # Value equal 'nan value'
raster_copy[raster > value]  = 1
raster_nan = raster_copy * raster

The process is something like this, I think you have to repeat the process por each band in your stack

enter image description here

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.