From 8751402b3ba1935fe87caa9807a17e70ed1eb9c6 Mon Sep 17 00:00:00 2001 From: Qiusheng Wu Date: Sun, 23 Jun 2024 11:12:16 -0400 Subject: [PATCH] Add COG and STAC examples for MapLibre (#783) * Add COG and STAC examples * Add local raster example * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add STAC notebook --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .gitignore | 1 + docs/maplibre/cloud_optimized_geotiff.ipynb | 148 ++++++++++++++++++ docs/maplibre/local_raster.ipynb | 163 ++++++++++++++++++++ docs/maplibre/mouse_position.ipynb | 2 +- docs/maplibre/overview.md | 18 +++ docs/maplibre/stac.ipynb | 150 ++++++++++++++++++ mkdocs.yml | 3 + 7 files changed, 484 insertions(+), 1 deletion(-) create mode 100644 docs/maplibre/cloud_optimized_geotiff.ipynb create mode 100644 docs/maplibre/local_raster.ipynb create mode 100644 docs/maplibre/stac.ipynb diff --git a/.gitignore b/.gitignore index 4b2fb58504..011b91dfad 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ __pycache__/ *.so # Distribution / packaging +docs/maplibre/*.tif examples/notebooks/cache/ examples/notebooks/*.tif examples/notebooks/*.gif diff --git a/docs/maplibre/cloud_optimized_geotiff.ipynb b/docs/maplibre/cloud_optimized_geotiff.ipynb new file mode 100644 index 0000000000..e38eb1bfbc --- /dev/null +++ b/docs/maplibre/cloud_optimized_geotiff.ipynb @@ -0,0 +1,148 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[![image](https://jupyterlite.rtfd.io/en/latest/_static/badge.svg)](https://demo.leafmap.org/lab/index.html?path=maplibre/cloud_optimized_geotiff.ipynb)\n", + "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/leafmap/blob/master/docs/maplibre/cloud_optimized_geotiff.ipynb)\n", + "[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD)\n", + "\n", + "**Visualize Cloud Optimized GeoTIFF (COG)**\n", + "\n", + "This notebook demonstrates how to visualize Cloud Optimized GeoTIFF (COG) files using the [TiTiler](https://github.com/developmentseed/titiler) demo endpoint ([titiler.xyz](https://titiler.xyz)). Please be gentle with the demo endpoint.\n", + "\n", + "Uncomment the following line to install [leafmap](https://leafmap.org) if needed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# %pip install \"leafmap[maplibre]\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import leafmap.maplibregl as leafmap" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To run this notebook, you will need an [API key](https://docs.maptiler.com/cloud/api/authentication-key/) from [MapTiler](https://www.maptiler.com/cloud/). Once you have the API key, you can set it as an environment variable in your notebook or script as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# os.environ[\"MAPTILER_KEY\"] = \"YOUR_API_KEY\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "MAPTILER_KEY = leafmap.get_api_key(\"MAPTILER_KEY\")\n", + "style = f\"https://api.maptiler.com/maps/satellite/style.json?key={MAPTILER_KEY}\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m = leafmap.Map(style=style)\n", + "before = (\n", + " \"https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-07-01.tif\"\n", + ")\n", + "after = (\n", + " \"https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-09-13.tif\"\n", + ")\n", + "m.add_cog_layer(before, name=\"Before\", attribution=\"Maxar\")\n", + "m.add_cog_layer(after, name=\"After\", attribution=\"Maxar\", fit_bounds=True)\n", + "m" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/lcmQd7G.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m.layer_interact()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m = leafmap.Map(style=style)\n", + "dem = \"https://github.com/opengeos/datasets/releases/download/raster/srtm90.tif\"\n", + "m.add_cog_layer(dem, name=\"DEM\", colormap_name=\"terrain\", fit_bounds=True)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m.layer_interact()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/t3nX3vj.png)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/maplibre/local_raster.ipynb b/docs/maplibre/local_raster.ipynb new file mode 100644 index 0000000000..b0f9aa9a3c --- /dev/null +++ b/docs/maplibre/local_raster.ipynb @@ -0,0 +1,163 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[![image](https://jupyterlite.rtfd.io/en/latest/_static/badge.svg)](https://demo.leafmap.org/lab/index.html?path=maplibre/local_raster.ipynb)\n", + "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/leafmap/blob/master/docs/maplibre/local_raster.ipynb)\n", + "[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD)\n", + "\n", + "**Visualize local raster datasets**\n", + "\n", + "This notebook demonstrates how to visualize local raster datasets with [localtileserver](https://localtileserver.banesullivan.com).\n", + "\n", + "\n", + "Uncomment the following line to install [leafmap](https://leafmap.org) if needed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# %pip install \"leafmap[maplibre]\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import leafmap.maplibregl as leafmap" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To run this notebook, you will need an [API key](https://docs.maptiler.com/cloud/api/authentication-key/) from [MapTiler](https://www.maptiler.com/cloud/). Once you have the API key, you can set it as an environment variable in your notebook or script as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# os.environ[\"MAPTILER_KEY\"] = \"YOUR_API_KEY\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "MAPTILER_KEY = leafmap.get_api_key(\"MAPTILER_KEY\")\n", + "style = f\"https://api.maptiler.com/maps/streets/style.json?key={MAPTILER_KEY}\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/opengeos/datasets/releases/download/raster/landsat.tif\"\n", + "filepath = \"landsat.tif\"\n", + "leafmap.download_file(url, filepath)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m = leafmap.Map(style=style)\n", + "m.add_raster(filepath, indexes=[3, 2, 1], vmin=0, vmax=100, name=\"Landsat-321\")\n", + "m.add_raster(filepath, indexes=[4, 3, 2], vmin=0, vmax=100, name=\"Landsat-432\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m.layer_interact()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/Q9sQLCP.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/opengeos/datasets/releases/download/raster/srtm90.tif\"\n", + "filepath = \"srtm90.tif\"\n", + "leafmap.download_file(url, filepath)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m = leafmap.Map(style=style)\n", + "m.add_raster(filepath, colormap=\"terrain\", name=\"DEM\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m.layer_interact()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/5nbGolD.png)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/maplibre/mouse_position.ipynb b/docs/maplibre/mouse_position.ipynb index c66597cddd..c5ba26ce46 100644 --- a/docs/maplibre/mouse_position.ipynb +++ b/docs/maplibre/mouse_position.ipynb @@ -95,7 +95,7 @@ " print(lng_lat.new)\n", "\n", "\n", - "m.observe(log_lng_lat, names=\"bounds\")\n", + "m.observe(log_lng_lat, names=\"clicked\")\n", "output" ] }, diff --git a/docs/maplibre/overview.md b/docs/maplibre/overview.md index 76322eee50..09a2e058d6 100644 --- a/docs/maplibre/overview.md +++ b/docs/maplibre/overview.md @@ -98,6 +98,12 @@ Use the upcase and downcase expressions to change the case of labels. [![](https://i.imgur.com/FzGOovv.png)](https://leafmap.org/maplibre/change_case_of_labels) +## Cloud Optimized GeoTIFF (COG) + +Visualize Cloud Optimized GeoTIFF (COG) files with TiTiler. + +[![](https://i.imgur.com/t3nX3vj.png)](https://leafmap.org/maplibre/cloud_optimized_geotiff) + ## Create and style clusters Use MapLibre GL JS' built-in functions to visualize points as clusters. @@ -242,6 +248,12 @@ View local GeoJSON without server upload. [![](https://i.imgur.com/w3mbV2O.png)](https://leafmap.org/maplibre/local_geojson) +## View local raster datasets + +View local raster datasets with localtileserver + +[![](https://i.imgur.com/Q9sQLCP.png)](https://leafmap.org/maplibre/local_raster) + ## Locate the user Geolocate the user and then track their current location on the map using the GeolocateControl. @@ -296,6 +308,12 @@ Initialize a map with pitch and bearing camera options. [![](https://i.imgur.com/onKRYXz.png)](https://leafmap.org/maplibre/set_pitch_bearing) +## Visualize SpatioTemporal Asset Catalog (STAC) + +Visualize SpatioTemporal Asset Catalog (STAC) items with TiTiler. + +[![](https://i.imgur.com/zWsNXSF.png)](https://leafmap.org/maplibre/stac) + ## Variable label placement Use text-variable-anchor to allow high priority labels to shift position to stay on the map. diff --git a/docs/maplibre/stac.ipynb b/docs/maplibre/stac.ipynb new file mode 100644 index 0000000000..8dd6b405db --- /dev/null +++ b/docs/maplibre/stac.ipynb @@ -0,0 +1,150 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[![image](https://jupyterlite.rtfd.io/en/latest/_static/badge.svg)](https://demo.leafmap.org/lab/index.html?path=maplibre/stac.ipynb)\n", + "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/leafmap/blob/master/docs/maplibre/stac.ipynb)\n", + "[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD)\n", + "\n", + "**Visualize SpatioTemporal Asset Catalog (STAC)**\n", + "\n", + "This notebook demonstrates how to visualize SpatioTemporal Asset Catalog (STAC) items using the [TiTiler](https://github.com/developmentseed/titiler) demo endpoint ([titiler.xyz](https://titiler.xyz)). Please be gentle with the demo endpoint.\n", + "\n", + "\n", + "Uncomment the following line to install [leafmap](https://leafmap.org) if needed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# %pip install \"leafmap[maplibre]\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import leafmap.maplibregl as leafmap" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To run this notebook, you will need an [API key](https://docs.maptiler.com/cloud/api/authentication-key/) from [MapTiler](https://www.maptiler.com/cloud/). Once you have the API key, you can set it as an environment variable in your notebook or script as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# os.environ[\"MAPTILER_KEY\"] = \"YOUR_API_KEY\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "MAPTILER_KEY = leafmap.get_api_key(\"MAPTILER_KEY\")\n", + "style = f\"https://api.maptiler.com/maps/streets/style.json?key={MAPTILER_KEY}\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m = leafmap.Map(style=style)\n", + "url = \"https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json\"\n", + "m.add_stac_layer(url, bands=[\"pan\"], name=\"Panchromatic\", vmin=0, vmax=150)\n", + "m.add_stac_layer(url, bands=[\"B4\", \"B3\", \"B2\"], name=\"RGB\", vmin=0, vmax=150)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m.layer_interact()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/zWsNXSF.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "collection = \"landsat-8-c2-l2\"\n", + "item = \"LC08_L2SP_047027_20201204_02_T1\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "leafmap.stac_assets(collection=collection, item=item, titiler_endpoint=\"pc\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m = leafmap.Map(style=style)\n", + "m.add_stac_layer(\n", + " collection=collection,\n", + " item=item,\n", + " assets=[\"SR_B5\", \"SR_B4\", \"SR_B3\"],\n", + " name=\"Color infrared\",\n", + ")\n", + "m" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/mkdocs.yml b/mkdocs.yml index 734d1c4563..752f8d0514 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -154,6 +154,7 @@ nav: - maplibre/center_on_symbol.ipynb - maplibre/change_building_color.ipynb - maplibre/change_case_of_labels.ipynb + - maplibre/cloud_optimized_geotiff.ipynb - maplibre/cluster.ipynb - maplibre/color_switcher.ipynb - maplibre/data_driven_lines.ipynb @@ -178,6 +179,7 @@ nav: - maplibre/live_geojson.ipynb - maplibre/live_update_feature.ipynb - maplibre/local_geojson.ipynb + - maplibre/local_raster.ipynb - maplibre/locate_user.ipynb - maplibre/map_tiles.ipynb - maplibre/mouse_position.ipynb @@ -187,6 +189,7 @@ nav: - maplibre/restrict_bounds.ipynb - maplibre/satellite_map.ipynb - maplibre/set_pitch_bearing.ipynb + - maplibre/stac.ipynb - maplibre/variable_label_placement.ipynb - maplibre/variable_offset_label_placement.ipynb - maplibre/vector_tile.ipynb