0

I am attempting to import a GeoTiff mask in which pixels of interest have the value 1, and off-target pixels are Nan using Rasterio. When viewed in QGIS and with plt.show(), off target pixels are shown correctly as "nodata" in QGIS and [] when viewed in plt.show(). However, when I read the band of the dataset, nan values within the boundaries of the image are converted to zeroes, whereas the added border pixels remain as nodata (shown as white in image below). Is there a way to retain the nan values when converted to an np.array?

file = r'[GeoTiff Path]'
# Convert file to raster
exg = rasterio.open(file, mode = 'r')

# Plotting raster and shapefile
fig, ax = plt.subplots(1, 1)
show(exg, ax = ax)
plots.plot(ax = ax, facecolor = 'None', edgecolor = 'red')
plt.show()

# Assign raster values to numpy nd array
exg_array = exg.read(1)
show(exg_array)

visualization of array

5
  • what is the dtype of your raster? Apr 15, 2022 at 18:20
  • @AmanBagrecha float32
    – nburner
    Apr 15, 2022 at 18:38
  • What makes you think the NaN values aren't being retained? I created a Float32 raster with np.nan, 1 and NoData areas, read it in using rasterio and inspected the array and can confirm that the NaNs are there.
    – user2856
    Apr 16, 2022 at 23:28
  • @user2856 When I use the zonal_stats function in rasterstats on the binary GeoTiff for example, instead of returning a mean pixel value of 1, it appears that it includes the nodata pixels in the calculation as zeroes and as a result I get a decimal of some sort.
    – nburner
    Apr 18, 2022 at 13:14
  • Yes, because your NaNs are not NoData, so looks like rasterstats counts them when generating the mean. Don't use NaNs, set those areas to NoData.
    – user2856
    Apr 18, 2022 at 21:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.