Skip to content

Commit

Permalink
Add support for visualizing in-memory raster (opengeos#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs authored Dec 24, 2023
1 parent 6852968 commit c8996a8
Show file tree
Hide file tree
Showing 9 changed files with 573 additions and 42 deletions.
1 change: 0 additions & 1 deletion docs/notebooks/88_nasa_earth_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@
{
"cell_type": "code",
"execution_count": null,
"id": "9e232fb9",
"metadata": {},
"outputs": [],
"source": [
Expand Down
201 changes: 201 additions & 0 deletions docs/notebooks/89_image_array_viz.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![image](https://jupyterlite.rtfd.io/en/latest/_static/badge.svg)](https://demo.leafmap.org/lab/index.html?path=notebooks/89_image_array_viz.ipynb)\n",
"[![image](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/opengeos/leafmap/blob/master/examples/notebooks/89_image_array_viz.ipynb)\n",
"[![image](https://img.shields.io/badge/Open-Planetary%20Computer-black?style=flat&logo=microsoft)](https://pccompute.westeurope.cloudapp.azure.com/compute/hub/user-redirect/git-pull?repo=https://github.com/opengeos/leafmap&urlpath=lab/tree/leafmap/examples/notebooks/89_image_array_viz.ipynb&branch=master)\n",
"[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/leafmap/blob/master/examples/notebooks/89_image_array_viz.ipynb)\n",
"[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD)\n",
"\n",
"**Visualizing in-memory raster datasets and image arrays**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %pip install leafmap rasterio rioxarray"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import leafmap\n",
"import rasterio\n",
"import rioxarray\n",
"import xarray as xr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Download two sample raster datasets."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"url1 = \"https://open.gishub.org/data/raster/landsat7.tif\"\n",
"url2 = \"https://open.gishub.org/data/raster/srtm90.tif\"\n",
"satellite = leafmap.download_file(url1, \"landsat7.tif\")\n",
"dem = leafmap.download_file(url2, \"srtm90.tif\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Landsat image contains 3 bands: nir, red, and green. Let's calculate NDVI using the nir and red bands."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dataset = rasterio.open(satellite)\n",
"nir = dataset.read(1).astype(float)\n",
"red = dataset.read(2).astype(float)\n",
"ndvi = (nir - red) / (nir + red)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create an in-memory raster dataset from the NDVI array and use the projection and extent of the Landsat image."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ndvi_image = leafmap.array_to_image(ndvi, source=satellite)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Visualize the Landsat image and the NDVI image on the same map."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = leafmap.Map()\n",
"m.add_raster(satellite, band=[1, 2, 3], nodata=-1, layer_name=\"Landsat 7\")\n",
"m.add_raster(ndvi_image, cmap=\"Greens\", layer_name=\"NDVI\")\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use rioxarray to read raster datasets into xarray DataArrays."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds = rioxarray.open_rasterio(dem)\n",
"ds"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Classify the DEM into 2 elevation classes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"array = ds.sel(band=1)\n",
"masked_array = xr.where(array < 2000, 0, 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create an in-memory raster dataset from the elevation class array and use the projection and extent of the DEM."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"image = leafmap.array_to_image(masked_array, source=dem)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Visualize the DEM and the elevation class image on the same map."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = leafmap.Map()\n",
"m.add_raster(dem, cmap=\"terrain\", layer_name=\"DEM\")\n",
"m.add_raster(image, cmap=\"coolwarm\", layer_name=\"Classified DEM\")\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.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
1 change: 1 addition & 0 deletions docs/tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
86. Adding markers to the map ([notebook](https://leafmap.org/notebooks/86_add_markers))
87. Cloud-based geoprocessing with Actinia ([notebook](https://leafmap.org/notebooks/87_actinia))
88. Searching and downloading NASA Earth science data products ([notebook](https://leafmap.org/notebooks/88_nasa_earth_data))
89. Visualizing in-memory raster datasets and image arrays ([notebook](https://leafmap.org/notebooks/89_image_array_viz))

## Demo

Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
86. Adding markers to the map ([notebook](https://leafmap.org/notebooks/86_add_markers))
87. Cloud-based geoprocessing with Actinia ([notebook](https://leafmap.org/notebooks/87_actinia))
88. Searching and downloading NASA Earth science data products ([notebook](https://leafmap.org/notebooks/88_nasa_earth_data))
89. Visualizing in-memory raster datasets and image arrays ([notebook](https://leafmap.org/notebooks/89_image_array_viz))

## Demo

Expand Down
1 change: 0 additions & 1 deletion examples/notebooks/88_nasa_earth_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@
{
"cell_type": "code",
"execution_count": null,
"id": "9e232fb9",
"metadata": {},
"outputs": [],
"source": [
Expand Down
Loading

0 comments on commit c8996a8

Please sign in to comment.