0

I have a collection of raster stored in a directory. They are rasters of glaciers in same region. When I remove no data using rioxarray.where method on individual rasters it works. But when I use this method on the merged product generated using rioxarray.merge.merge_arrays method, it does not remove the no data value (which is -9999). Below is the code. I apologize that I was not able to provide a reproducible example.

import rioxarray as rxr
from rioxarray import merge
import xarray as xr
import numpy as np
import glob
import matplotlib.pyplot as plt

path = r'ice_thickness_pandit/*.tif'
files = glob.glob(path)
files

elements = []
for item in files:
    elements.append(rxr.open_rasterio(item))

merged = merge.merge_arrays(elements, nodata=-9999)

merged = merged.where(merged != -9999, drop = False)
Sayantan4796
  • 169
  • 1
  • 10

1 Answers1

0

I was having this same problem and was able to fix it by using the where command on the input data arrays.

import numpy as np
import pandas as pd
import rioxarray as rxr
import xarray as xr
import matplotlib.pyplot as plt

path = '/path/to/files/'
file1 = 'file1.dat'
cube1 = xr.open_dataarray(path+file1,engine='rasterio')
cube1_masked = cube1.where(cube1 != 0)
cube1.close()
file2 = 'file2.dat'
cube2 = xr.open_dataarray(path+file2,engine='rasterio')
cube2_masked = cube2.where(cube2 != 0)
cube2.close()

#%%
from rioxarray import merge
ds = [
    cube1_masked,
    cube2_masked,
]
merged = merge.merge_arrays(ds, nodata=np.nan)


merged_arr1 = merged[0,:,:]
merged_arr2 = merged[1,:,:]
fig, axes = plt.subplots(ncols=2, figsize=(12,4))
merged_arr1.plot(ax=axes[0], add_colorbar=False)
merged_arr2.plot(ax=axes[1], add_colorbar=False)
plt.draw()
Jennifer
  • 21
  • 2