4

I have a Tiff file that has no data value = -3.40282347e+38. I want to filter no data and get raw values with gdal. I tried with below code:

import numpy as np
from osgeo import gdal, gdal_array

dataset = gdal.Open("path/to/file.tif")
array = dataset.ReadAsArray()

lst = []
for v in array:
    if v < 0:
        lst.append(v)

print lst

but I received this error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/tmp/tmp3B0d5H.py", line 10, in <module>
    if v < 0:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

How can I do it?

  • 1
    Your code doesn't work because it needs a double loop. – xunilk Oct 29 '17 at 17:07
  • 1
    Alternatively, you could strip the nodata cells from your raster prior to going into the array for analysis - it's not clear if you want to keep the nodata cells for analysis, or just remove them. Gdalwarp could do this easily.A simple os.system command could accomplish this: os.system('gdalwarp -srcnodata "value [-3.40282347e+38]" -dstnodata "value []" input.tif output.tif') – AlecZ Oct 29 '17 at 19:34
4

If you want to filter no data and get raw values you need following code:

import numpy as np
from osgeo import gdal, gdal_array

dataset = gdal.Open("path/to/file.tif")
array = dataset.ReadAsArray()

lst = [ element for v in array for element in v if element > 0 ]

print lst

I tried it out with a raster with no data values equal to -999 and it works adequately.

Editing Note:

Based in your commentary, you don't need to filter these values. It's necessary to change no data values to NaN values for calculating directly np.nanpercentiles. Following code calculates this (percentile 50) for my raster with -999 no data values.

import numpy as np
from osgeo import gdal, gdal_array

dataset = gdal.Open("/home/zeito/pyqgis_data/test_raster_nodata.tif")
array = dataset.ReadAsArray()

nan_array = array

nan_array[array == -999] = np.nan

print np.nanpercentile(nan_array, 50)

Result is 6.

  • Hi @xunilk, Is it possible to use block in your code for faster run in large raster files? – nickan Oct 29 '17 at 17:15
  • What do you want to do with data array? – xunilk Oct 29 '17 at 17:19
  • I want to compute quantile (percentile) without negative values (i.e. nodata values). – nickan Oct 29 '17 at 17:21
  • I posted new question for my work. (gis.stackexchange.com/questions/260019/…) – nickan Oct 29 '17 at 17:41
  • 1
    See my Editing Note. You have the answer. – xunilk Oct 29 '17 at 17:44

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.