Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layers as xarray.DataArray in split_map (ipyleaflet Map) #681

Merged
merged 11 commits into from
Feb 14, 2024
83 changes: 83 additions & 0 deletions docs/notebooks/49_split_control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"outputs": [],
"source": [
"import folium\n",
"import rioxarray\n",
"import xarray as xr\n",
"import leafmap.foliumap as leafmap"
]
},
Expand Down Expand Up @@ -130,13 +132,94 @@
"m.split_map(left_layer, right_layer)\n",
"m"
]
},
{
"cell_type": "markdown",
"id": "30e269ce",
"metadata": {},
"source": [
"**Using xarrays**"
]
},
{
"cell_type": "markdown",
"id": "5d01d145",
"metadata": {},
"source": [
"Download a sample dataset."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11de8d33",
"metadata": {},
"outputs": [],
"source": [
"url = \"https://open.gishub.org/data/raster/srtm90.tif\"\n",
"dem = leafmap.download_file(url, \"srtm90.tif\")"
]
},
{
"cell_type": "markdown",
"id": "c95ed0f5",
"metadata": {},
"source": [
"Use rioxarray to read the raster as a xarray DataArray and then classify the DEM into 2 elevation classes. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0022a80",
"metadata": {},
"outputs": [],
"source": [
"dem_ds = rioxarray.open_rasterio(dem)\n",
"dem_class = xr.where(dem_ds<2000, 0, 1)"
]
},
{
"cell_type": "markdown",
"id": "4718a9f1",
"metadata": {},
"source": [
"Visualize the DEM and the elevation class image as a split map."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "40018a15",
"metadata": {},
"outputs": [],
"source": [
"m = leafmap.Map(center=[37.6,-119], zoom=9)\n",
"m.split_map(dem_ds, dem_class,\n",
" left_args={\"layer_name\":\"DEM\", \"colormap\": \"terrain\"},\n",
" right_args={\"layer_name\":\"Classified DEM\",\"colormap\": \"coolwarm\"}\n",
" )\n",
"m"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down
Loading
Loading