Skip to content

Commit

Permalink
Updated notebooks 2023-12-24 UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Dec 24, 2023
1 parent 5a67e5b commit 865bf2d
Showing 1 changed file with 190 additions and 0 deletions.
190 changes: 190 additions & 0 deletions content/notebooks/89_image_array_viz.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
{
"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://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://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 (Pyodide)",
"language": "python",
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 865bf2d

Please sign in to comment.