Reputation: 220
I am attempting to read a multiband tif file (4 bands - [Blue, Green, Red, Infrared]) into an xarray, then display as RGB using HoloViews within a Jupyter notebook. For reference, I'm loosely following the RGB png example here: http://holoviews.org/reference/elements/matplotlib/RGB.html
The final RGB image does display, however, I lose the x/y coordinate dimensions by combining the DataArrays using np.dstack. The x/y coordinates in the final image default to ~ -0.5 - +0.5.
I'm at a loss how to carry through the coordinate dimensions through the process, or potentially how to apply the original coordinate dimensions to the final image.
# read .tif
ximg = xarray.open_rasterio('path/to/tif')
print('1.', type(ximg), ximg.coords['x'].values)
# convert to hv.Dataset
r_ds = hv.Dataset(ximg[2,:,:], kdims=['x','y'], vdims='Value')
g_ds = hv.Dataset(ximg[1,:,:], kdims=['x','y'], vdims='Value')
b_ds = hv.Dataset(ximg[0,:,:], kdims=['x','y'], vdims='Value')
print('2.', type(r_ds), r_ds.dimension_values('x'))
# scale to uint8
r = np.squeeze((r_ds.data.to_array().astype(np.float64)/8190)*255).astype('uint8')
g = np.squeeze((g_ds.data.to_array().astype(np.float64)/8190)*255).astype('uint8')
b = np.squeeze((b_ds.data.to_array().astype(np.float64)/8190)*255).astype('uint8')
print('3.', type(r), r.coords['x'].values)
# combine to RGB
dstack = np.dstack([r, g, b]) # lose coordinate dimensions here
print('4.', type(dstack), 'NO COORDS')
rgb = hv.RGB(dstack, kdims=['x','y'])
print('5.', type(rgb), rgb.dimension_values('x'))
1. <class 'xarray.core.dataarray.DataArray'> [557989.5 557992.5 557995.5 ... 563194.5 563197.5 563200.5]
2. <class 'holoviews.core.data.Dataset'> [557989.5 557989.5 557989.5 ... 563200.5 563200.5 563200.5]
3. <class 'xarray.core.dataarray.DataArray'> [557989.5 557992.5 557995.5 ... 563194.5 563197.5 563200.5]
4. <class 'numpy.ndarray'> NO COORDS
5. <class 'holoviews.element.raster.RGB'> [-0.49971231 -0.49971231 -0.49971231 ... 0.49971231 0.49971231
0.49971231]
Example showing desired coordinates, using HoloViews images created from r_ds
, g_ds
, and b_ds
above:
Example showing undesired coordinates, using HoloViews RGB, named rgb
above:
Upvotes: 1
Views: 942
Reputation: 220
The Landsat example mentioned in comments uses a data
parameter of the form (xdim, ydim, R, G, B, A)
, which applies the desired x/y coordinates to the image.
Landsat example: http://datashader.org/topics/landsat.html
rgb = hv.RGB(
(
ximg['x'],
ximg['y'],
r.data[::-1],
g.data[::-1],
b.data[::-1]
),
vdims=list('RGB')
)
Upvotes: 1