0

I work with geographic data and have large raster files I import into python with gdal. To process them, I convert them to numpy arrays.

I create an empty array that gets filled with data in a loop. However, I need the NaN values to be at the same location as in my imported rater file.

data = ds.GetRasterBand(1).GetNoDataValue()
data = np.array(ds.GetRasterBand(1).ReadAsArray())
data[data==-9999] = np.nan # set all -9999 as no data value

cols, rows = data.shape
dataOut = np.zeros([ds.RasterYSize, ds.RasterXSize]) # create empty array with zeros
for c in range(cols):
    for r in range(rows):
        dataOut[c][r] = np.nan if isinstance(data[c][r], np.ndarray) else dataOut[c][r]

So I want my NaN values from my data array to be transferred to my dataOut array. None of those solutions suggested here wored for me...the dataOut array still holds only zeros.

Community
  • 1
  • 1
GeoEki
  • 437
  • 1
  • 7
  • 20
  • Why are you checking `if isinstance(data[c][r], no.ndarray)`? That is never going to be true for a 2 dimensional array... You probably just want `dataOut[np.isnan(data))` – juanpa.arrivillaga Mar 16 '17 at 16:20
  • That's not working unfortunately (without error message, just no change at all). dataOut still holds only 0's instead of the NaN's in those positions as in data. – GeoEki Mar 21 '17 at 15:34

1 Answers1

0

Figured it out myself...np.isnan is the solution:

for c in range(cols):
    for r in range(rows):
        dataOut[c][r] = np.nan if np.isnan(data[c][r]) else dataOut[c][r]
GeoEki
  • 437
  • 1
  • 7
  • 20