In [1]:
Copied!
#mamba install -c conda-forge hvplot datashader geoviews xarray-spatial
#mamba install -c conda-forge hvplot datashader geoviews xarray-spatial
In [24]:
Copied!
import time
import datashader.colors as dscolors
import rioxarray as rxr
import hvplot.xarray
import xarray as xr
import xrspatial
import time
import datashader.colors as dscolors
import rioxarray as rxr
import hvplot.xarray
import xarray as xr
import xrspatial
Mounting data¶
In [28]:
Copied!
%%time
# Define the URL of the DEM file
dem_url = (
"/vsizip/vsicurl/"
"https://data.hydrosheds.org/file/hydrosheds-v1-dem/hyd_na_dem_15s.zip/"
"hyd_na_dem_15s.tif")
# Open the DEM file with rioxarray
dem_da = rxr.open_rasterio(dem_url, masked=True).squeeze()
# Print out details of the raster object
print("Raster details:")
print("Shape:", dem_da.shape)
print("CRS:", dem_da.rio.crs)
dem_da
%%time
# Define the URL of the DEM file
dem_url = (
"/vsizip/vsicurl/"
"https://data.hydrosheds.org/file/hydrosheds-v1-dem/hyd_na_dem_15s.zip/"
"hyd_na_dem_15s.tif")
# Open the DEM file with rioxarray
dem_da = rxr.open_rasterio(dem_url, masked=True).squeeze()
# Print out details of the raster object
print("Raster details:")
print("Shape:", dem_da.shape)
print("CRS:", dem_da.rio.crs)
dem_da
Raster details: Shape: (13920, 20640) CRS: EPSG:4326 CPU times: user 22.7 ms, sys: 146 µs, total: 22.8 ms Wall time: 19.4 ms
Out[28]:
<xarray.DataArray (y: 13920, x: 20640)> [287308800 values with dtype=float32] Coordinates: band int64 1 * x (x) float64 -138.0 -138.0 -138.0 -138.0 ... -52.01 -52.01 -52.0 * y (y) float64 63.0 62.99 62.99 62.99 ... 5.015 5.01 5.006 5.002 spatial_ref int64 0 Attributes: AREA_OR_POINT: Area RepresentationType: THEMATIC scale_factor: 1.0 add_offset: 0.0 long_name: Band_1
In [26]:
Copied!
start_time = time.time()
dem_da.hvplot(x='x', y='y', geo=True, rasterize=True, cmap=dscolors.Elevation)
start_time = time.time()
dem_da.hvplot(x='x', y='y', geo=True, rasterize=True, cmap=dscolors.Elevation)
Out[26]:
In [17]:
Copied!
f'Elapsed time: {time.time() - start_time} s'
f'Elapsed time: {time.time() - start_time} s'
Out[17]:
'Elapsed time: 55.768351793289185 s'
In [25]:
Copied!
%%time
# THIS SLOPE IS INCORRECT BECAUSE THE DATA ARE NOT PROJECTED (units issue with xrspatial)
slope_da = xrspatial.slope(dem_da)
slope_da.hvplot(x='x', y='y', geo=True, rasterize=True, )
%%time
# THIS SLOPE IS INCORRECT BECAUSE THE DATA ARE NOT PROJECTED (units issue with xrspatial)
slope_da = xrspatial.slope(dem_da)
slope_da.hvplot(x='x', y='y', geo=True, rasterize=True, )
CPU times: user 11.3 s, sys: 564 ms, total: 11.8 s Wall time: 11.8 s
Out[25]:
Calculate aspect from data¶
In [19]:
Copied!
%%time
aspect_da = xrspatial.aspect(dem_da)
aspect_da.hvplot(x='x', y='y', geo=True, rasterize=True, cmap='colorwheel')
%%time
aspect_da = xrspatial.aspect(dem_da)
aspect_da.hvplot(x='x', y='y', geo=True, rasterize=True, cmap='colorwheel')
CPU times: user 9.87 s, sys: 421 ms, total: 10.3 s Wall time: 10.3 s
Out[19]:
Create a cube from those layers!¶
In [22]:
Copied!
data_cube = xr.Dataset(dict(
elevation=dem_da,
slope=slope_da,
aspect=aspect_da
))
data_cube
data_cube = xr.Dataset(dict(
elevation=dem_da,
slope=slope_da,
aspect=aspect_da
))
data_cube
Out[22]:
<xarray.Dataset> Dimensions: (x: 20640, y: 13920) Coordinates: band int64 1 * x (x) float64 -138.0 -138.0 -138.0 -138.0 ... -52.01 -52.01 -52.0 * y (y) float64 63.0 62.99 62.99 62.99 ... 5.015 5.01 5.006 5.002 spatial_ref int64 0 Data variables: elevation (y, x) float32 nan nan nan nan nan nan ... nan nan nan nan nan slope (y, x) float32 nan nan nan nan nan nan ... nan nan nan nan nan aspect (y, x) float32 nan nan nan nan nan nan ... nan nan nan nan nan
In [23]:
Copied!
data_cube.mean()
data_cube.mean()
Out[23]:
<xarray.Dataset> Dimensions: () Coordinates: band int64 1 spatial_ref int64 0 Data variables: elevation float32 664.2 slope float32 88.27 aspect float32 171.0
Last update:
2023-11-16