From f2fb25b0c804d66ce4dce3cdc1ee3bbd138bb2bf Mon Sep 17 00:00:00 2001 From: Qiusheng Wu Date: Tue, 22 Oct 2024 22:52:23 -0400 Subject: [PATCH] Add lab 7 (#123) * Add lab 7 * Add to TOC --- _toc.yml | 1 + book/geospatial/leafmap.ipynb | 4 +- book/geospatial/leafmap.md | 169 +++++++++++++--- book/labs/lab_07.ipynb | 371 ++++++++++++++++++++++++++++++++++ book/labs/lab_07.md | 211 +++++++++++++++++++ 5 files changed, 731 insertions(+), 25 deletions(-) create mode 100644 book/labs/lab_07.ipynb create mode 100644 book/labs/lab_07.md diff --git a/_toc.yml b/_toc.yml index d402c2b..07d6f0b 100644 --- a/_toc.yml +++ b/_toc.yml @@ -60,3 +60,4 @@ parts: - file: book/labs/lab_04 - file: book/labs/lab_05 - file: book/labs/lab_06 + - file: book/labs/lab_07 diff --git a/book/geospatial/leafmap.ipynb b/book/geospatial/leafmap.ipynb index 588f026..3a69dae 100644 --- a/book/geospatial/leafmap.ipynb +++ b/book/geospatial/leafmap.ipynb @@ -1364,7 +1364,7 @@ " \"90\": \"#b8d9eb\",\n", " \"95\": \"#6c9fb8\",\n", "}\n", - "m = leafmap.Map(center=[40, -100], zoom=4, height=\"700px\")\n", + "m = leafmap.Map(center=[40, -100], zoom=4, height=\"650px\")\n", "m.add_basemap(\"Satellite\")\n", "m.add_cog_layer(url, colormap=colormap, name=\"NLCD Land Cover\", nodata=0)\n", "m.add_legend(title=\"NLCD Land Cover Type\", builtin_legend=\"NLCD\")\n", @@ -1818,7 +1818,7 @@ ], "metadata": { "kernelspec": { - "display_name": "geo", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, diff --git a/book/geospatial/leafmap.md b/book/geospatial/leafmap.md index f04520c..8e40823 100644 --- a/book/geospatial/leafmap.md +++ b/book/geospatial/leafmap.md @@ -6,7 +6,7 @@ jupytext: format_version: 0.13 jupytext_version: 1.16.4 kernelspec: - display_name: geo + display_name: Python 3 (ipykernel) language: python name: python3 --- @@ -606,20 +606,49 @@ m ## Visualizing Raster Data -Leafmap makes it easy to visualize Cloud Optimized GeoTIFFs (COGs) and local GeoTIFF files. In this section, we’ll explore how to add, compare, and style raster data from both online and local sources. +Leafmap supports various raster data formats, including GeoTIFF, Cloud Optimized GeoTIFF (COG), SpatioTemporal Asset Catalog (STAC), and others. This section demonstrates how to visualize raster data using Leafmap. -### Adding a Cloud Optimized GeoTIFF (COG) +### Visualizing Cloud Optimized GeoTIFFs (COGs) + +A Cloud Optimized GeoTIFF ([COG](https://cogeo.org/)) is a regular GeoTIFF file, aimed at being hosted on a HTTP file server, with an internal organization that enables more efficient workflows on the cloud. It does this by leveraging the ability of clients issuing ​HTTP GET range requests to ask for just the parts of a file they need. + +#### Adding a Cloud Optimized GeoTIFF (COG) You can load remote COGs directly from a URL. In this example, we load pre-event imagery of the 2020 California fire: ```{code-cell} ipython3 -m = leafmap.Map() +m = leafmap.Map(center=[39.494897, -108.507278], zoom=10) url = "https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2018-02-16/pine-gulch-fire20/1030010076004E00.tif" m.add_cog_layer(url, name="Fire (pre-event)") m ``` -### Adding Multiple COGs +Under the hood, the `add_cog_layer()` method uses the [TiTiler](https://developmentseed.org/titiler) demo endpoint (https://titiler.xyz) to serve COGs as map tiles. The method also supports custom styling and visualization parameters. Please refer to the [TiTiler documentation](https://developmentseed.org/titiler/endpoints/cog/#api) for more information. + +Please note that the `add_cog_layer()` method requires an internet connection to fetch the COG tiles from the TiTiler endpoint. If you need to work offline, you can download the COG and load it as a local GeoTIFF file using the `add_raster()` method covered in the next section. + ++++ + +To show the image metadata, use the `cog_info()` method: + +```{code-cell} ipython3 +leafmap.cog_info(url) +``` + +To check the available bands, use the `cog_bands()` method: + +```{code-cell} ipython3 +leafmap.cog_bands(url) +``` + +To get the band statistics, use the `cog_stats()` method: + +```{code-cell} ipython3 +stats = leafmap.cog_stats(url) +# stats +``` + +#### Adding Multiple COGs You can visualize and compare multiple COGs by adding them to the same map. Below, we add post-event imagery to the map: @@ -629,29 +658,82 @@ m.add_cog_layer(url2, name="Fire (post-event)") m ``` -### Creating a Split Map for Comparison +#### Creating a Split Map for Comparison Leafmap also provides a convenient way to compare two COGs side by side using a split map. In this example, we create a map comparing pre-event and post-event fire imagery: ```{code-cell} ipython3 -m = leafmap.Map() +m = leafmap.Map(center=[39.494897, -108.507278], zoom=10) m.split_map( left_layer=url, right_layer=url2, left_label="Pre-event", right_label="Post-event" ) m ``` -### Visualizing Local Raster Files +Here is another example comparing two COGs using a split map: -Leafmap supports visualization of local GeoTIFF files as well. In this example, we download a sample Digital Elevation Model (DEM) dataset and visualize it. +```{code-cell} ipython3 +m = leafmap.Map(center=[47.653149, -117.59825], zoom=16) +m.add_basemap("Satellite") +image1 = "https://github.com/opengeos/datasets/releases/download/places/wa_building_image.tif" +image2 = "https://github.com/opengeos/datasets/releases/download/places/wa_building_masks.tif" +m.split_map( + image2, + image1, + left_label="Building Masks", + right_label="Aerial Imagery", + left_args={"colormap_name": "tab20", "nodata": 0, "opacity": 0.7}, +) +m +``` + +#### Using a Custom Colormap + +You can apply a custom colormap to raster data for better visualization. The example below shows how to visualize the [US National Land Cover Database (NLCD)](https://www.mrlc.gov/data/nlcd-2021-land-cover-conus) data with a custom colormap: + +```{code-cell} ipython3 +url = "https://github.com/opengeos/datasets/releases/download/raster/nlcd_2021_land_cover_30m.tif" +colormap = { + "11": "#466b9f", + "12": "#d1def8", + "21": "#dec5c5", + "22": "#d99282", + "23": "#eb0000", + "24": "#ab0000", + "31": "#b3ac9f", + "41": "#68ab5f", + "42": "#1c5f2c", + "43": "#b5c58f", + "51": "#af963c", + "52": "#ccb879", + "71": "#dfdfc2", + "72": "#d1d182", + "73": "#a3cc51", + "74": "#82ba9e", + "81": "#dcd939", + "82": "#ab6c28", + "90": "#b8d9eb", + "95": "#6c9fb8", +} +m = leafmap.Map(center=[40, -100], zoom=4, height="650px") +m.add_basemap("Satellite") +m.add_cog_layer(url, colormap=colormap, name="NLCD Land Cover", nodata=0) +m.add_legend(title="NLCD Land Cover Type", builtin_legend="NLCD") +m.add_layer_manager() +m +``` + +### Visualizing Local Raster Datasets + +Leafmap supports visualizing local GeoTIFF datasets as well. In this example, we download a sample Digital Elevation Model (DEM) dataset and visualize it. #### Downloading and Visualizing a Local Raster Start by downloading a sample DEM GeoTIFF file: ```{code-cell} ipython3 -dem_url = "https://open.gishub.org/data/raster/srtm90.tif" -filename = "srtm90.tif" +dem_url = "https://github.com/opengeos/datasets/releases/download/raster/dem_90m.tif" +filename = "dem_90m.tif" leafmap.download_file(dem_url, filename, quiet=True) ``` @@ -675,27 +757,50 @@ Optionally, add a colormap legend to indicate the range of elevation values: m.add_colormap(cmap="terrain", vmin=15, vmax=4338, label="Elevation (m)") ``` +Keep in mind that the `add_raster()` method works for both local and remote GeoTIFF files. You can use it to visualize COGs available online or local GeoTIFF files stored on your computer. The example below demonstrates how to visualize the same COG from the previous section as a remote file: + ```{code-cell} ipython3 -landsat_url = "https://open.gishub.org/data/raster/cog.tif" -filename = "cog.tif" -leafmap.download_file(landsat_url, filename, quiet=True) +m = leafmap.Map() +m.add_raster(dem_url, colormap="terrain", layer_name="DEM") +m ``` -### Visualizing a Multi-Band Raster +You can also use a custom colormap to visualize the COG by providing a list of colors as the `colormap` parameter: + +```{code-cell} ipython3 +m = leafmap.Map() +m.add_raster(dem_url, colormap=["blue", "green", "white"], layer_name="DEM") +m +``` + +#### Visualizing a Multi-Band Raster Multi-band rasters, such as satellite images, can also be visualized. The following example loads a multi-band Landsat image and displays it as an RGB composite: +```{code-cell} ipython3 +landsat_url = "https://github.com/opengeos/datasets/releases/download/raster/cog.tif" +filename = "cog.tif" +leafmap.download_file(landsat_url, filename, quiet=True) +``` + ```{code-cell} ipython3 m = leafmap.Map() -m.add_raster(filename, bands=[4, 3, 2], layer_name="RGB") +m.add_raster(filename, indexes=[4, 3, 2], layer_name="RGB") m ``` -![](https://i.imgur.com/euhkajs.png) +#### Inspecting Pixel Values -+++ +To inspect pixel values interactively, use the `m.add("inspector")` method to add an inspector control to the map. The inspector control displays pixel values when you click on the map: + +```{code-cell} ipython3 +m = leafmap.Map(center=[53.407089, 6.875480], zoom=13) +m.add_raster(filename, indexes=[4, 3, 2], layer_name="RGB") +m.add("inspector") +m +``` -### isualizing SpatioTemporal Asset Catalog (STAC) Data +### Visualizing SpatioTemporal Asset Catalog (STAC) Data The [STAC specification](https://stacspec.org) provides a standardized way to describe geospatial information, enabling easier discovery and use. In this section, we will visualize STAC data using Leafmap. @@ -710,16 +815,16 @@ leafmap.stac_bands(url) #### Adding STAC Layers to the Map -Once you have explored the available bands, you can add them to your map. In this example, we visualize both panchromatic and false-color imagery: +Once you have explored the available bands, you can add them to your map. In this example, we visualize both the panchromatic band and false-color composite of the SPOT Orthoimage: ```{code-cell} ipython3 -m = leafmap.Map() +m = leafmap.Map(center=[60.95410, -110.90184], zoom=10) m.add_stac_layer(url, bands=["pan"], name="Panchromatic") m.add_stac_layer(url, bands=["B3", "B2", "B1"], name="False color") m ``` -### Custom STAC Catalog +#### Using a Custom STAC Catalog You can integrate custom STAC API endpoints into your map. Simply provide a dictionary of STAC endpoints, where the keys are names and the values are the API URLs. This example demonstrates how to add custom STAC catalogs: @@ -788,9 +893,27 @@ items[:10] Finally, visualize a raster dataset from the S3 bucket by adding it to the map: ```{code-cell} ipython3 -m = leafmap.Map() +m = leafmap.Map(center=[37.045802, 35.333319], zoom=14) m.add_cog_layer(items[2], name="Maxar") m ``` ![](https://i.imgur.com/NkTZ6Lj.png) + ++++ + +Some S3 buckets may require additional permissions to access the data. For example, for requester pays buckets, you need to set the environment variable `AWS_REQUEST_PAYER` to `requester`: + +```{code-cell} ipython3 +os.environ["AWS_REQUEST_PAYER"] = "requester" +``` + +The example below shows how to visualize a [NAIP imagery](https://registry.opendata.aws/naip/) from a requester pays bucket: + +```{code-cell} ipython3 +m = leafmap.Map(center=[34.979166, -84.920496], zoom=14) +m.add_basemap("Satellite") +src = "s3://naip-analytic/tn/2021/60cm/rgbir_cog/34084/m_3408401_ne_16_060_20210404.tif" +m.add_raster(src, layer_name="NAIP") +m +``` diff --git a/book/labs/lab_07.ipynb b/book/labs/lab_07.ipynb new file mode 100644 index 0000000..12cf9af --- /dev/null +++ b/book/labs/lab_07.ipynb @@ -0,0 +1,371 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lab 7\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/giswqs/geog-312/blob/main/book/labs/lab_07.ipynb)\n", + "\n", + "## Overview\n", + "\n", + "In this lab, you will delve deeper into Leafmap, a powerful Python package for interactive geospatial mapping and analysis. You will explore how to work with various types of raster and vector data, customize basemaps and map layers, and visualize data using Cloud Optimized GeoTIFFs (COGs), PMTiles, GeoParquet, and other formats. You will also learn how to enhance your maps with advanced features like legends, colorbars, marker clusters, and split maps for comparison.\n", + "\n", + "## Objective\n", + "\n", + "* Create and customize interactive maps using Leafmap.\n", + "* Add and configure basemaps, XYZ tile layers, and WMS layers.\n", + "* Visualize various raster formats such as GeoTIFF, Cloud Optimized GeoTIFF (COG), and multi-band imagery.\n", + "* Explore vector data visualization, including marker clusters and choropleth maps.\n", + "* Implement advanced mapping features like legends, colorbars, and split map comparisons." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 1: Creating an Interactive Map\n", + "\n", + "1. Create an interactive map with search functionality that allows users to search for places and zoom to them. Disable the draw control on the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/b930fb63-3bd1-4d7e-9bf8-87e6d398e5c3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 2: Adding XYZ and WMS Tile Layers\n", + "\n", + "1. Add a custom XYZ tile layer ([USGS Topographic basemap](https://apps.nationalmap.gov/services)) using the following URL:\n", + " - URL: `https://basemap.nationalmap.gov/arcgis/rest/services/USGSTopo/MapServer/tile/{z}/{y}/{x}`\n", + "2. Add two WMS layers to visualize NAIP imagery and NDVI using a USGS WMS service.\n", + " - URL: `https://imagery.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer?`\n", + " - Layer names: `USGSNAIPImagery:FalseColorComposite`, `USGSNAIPImagery:NDVI_Color`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/28dd8511-a0ac-4b44-ab02-9ed791817043)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/719c1e07-f955-42d6-985c-b5842c9eac4c)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 3: Adding Map Legends\n", + "\n", + "1. Add the [ESA World Cover](https://esa-worldcover.org/en) WMS tile layer to the map.\n", + " - URL: `https://services.terrascope.be/wms/v2?`\n", + " - Layer name: `WORLDCOVER_2021_MAP`\n", + "2. Add a legend to the map using the leafmap built-in `ESA_WorldCover` legend." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/be5a9b07-4f6c-4245-9737-31db2df14f7f)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 4: Creating Marker Clusters\n", + "\n", + "1. Create a marker cluster visualization from a GeoJSON file of building centroids:\n", + " - URL: https://github.com/opengeos/datasets/releases/download/places/wa_building_centroids.geojson\n", + " - Hint: Read the GeoJSON file using GeoPandas and add \"latitude\" and \"longitude\" columns to the GeoDataFrame.\n", + "2. Create circle markers for each building centroid using the `Map.add_circle_markers_from_xy()` method with the following styling:\n", + " * Radius: 5\n", + " * Outline color: \"red\"\n", + " * Fill color: \"yellow\"\n", + " * Fill opacity: 0.8" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/d60cbfc7-b8c9-4cab-8852-bc34e82fd665)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/637e00ae-89af-495e-84b4-e668e16cce88)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 5: Visualizing Vector Data\n", + "\n", + "1. Visualize the building polygons GeoJSON file and style it with:\n", + " * Outline color: \"red\"\n", + " * No fill color\n", + " * URL: https://github.com/opengeos/datasets/releases/download/places/wa_overture_buildings.geojson\n", + " \n", + "2. Visualize the road polylines GeoJSON file and style it with:\n", + " * Line color: \"red\"\n", + " * Line width: 2\n", + " * URL: https://github.com/opengeos/datasets/releases/download/places/las_vegas_roads.geojson\n", + "\n", + "3. Create a choropleth map of county areas in the US:\n", + " * URL: https://github.com/opengeos/datasets/releases/download/us/us_counties.geojson\n", + " * Column: `CENSUSAREA`\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/069eb704-c409-44ee-af9e-7582d1ab23a5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/f28d19a6-4f60-484c-b2f7-c1ecce7ecb26)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/3aa9f54f-64d7-4788-89f1-f3ab88c1aa2e)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 6: Visualizing GeoParquet Data\n", + "\n", + "1. Visualize GeoParquet data of US states:\n", + "\n", + " - URL: https://github.com/opengeos/datasets/releases/download/us/us_states.parquet\n", + " - Style: Outline color of \"red\" with no fill." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/b6d9ec09-035b-4df7-8ab7-a7b4588f1e71)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 7: Visualizing PMTiles\n", + "\n", + "1. Visualize the Overture Maps building dataset using PMTiles:\n", + " * URL: https://overturemaps-tiles-us-west-2-beta.s3.amazonaws.com/2024-09-18/buildings.pmtiles\n", + " * Style: Blue fill with 0.4 opacity, red outline." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/4ee08461-c962-4c37-8979-7105f588842a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 8: Visualizing Cloud Optimized GeoTIFFs (COGs)\n", + "\n", + "1. Visualize Digital Elevation Model (DEM) data using the following COG file:\n", + " - URL: https://github.com/opengeos/datasets/releases/download/raster/dem.tif\n", + " - Apply a terrain colormap to show elevation values." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/21e1f7dd-466e-4880-94a5-0365bf1495dc)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 9: Visualizing Local Raster Data\n", + "\n", + "1. Visualize the following raster datasets using the Map.add_raster() method:\n", + "\n", + " * Aerial Imagery: https://github.com/opengeos/datasets/releases/download/places/wa_building_image.tif\n", + " * Building Footprints: https://github.com/opengeos/datasets/releases/download/places/wa_building_masks.tif (use the \"tab20\" colormap and opacity of 0.7)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/faf7c345-6a3f-4ca0-8eca-7417e6568867)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 10: Creating a Split Map\n", + "\n", + "1. Create a split map to compare imagery of Libya before and after the 2023 flood event:\n", + "\n", + " * Pre-event imagery: https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-07-01.tif\n", + " * Post-event imagery: https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-09-13.tif" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/8cab4f1c-2dba-4652-a644-3ce6be4bbbd2)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "geo", + "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.12.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/book/labs/lab_07.md b/book/labs/lab_07.md new file mode 100644 index 0000000..19b458d --- /dev/null +++ b/book/labs/lab_07.md @@ -0,0 +1,211 @@ +--- +jupytext: + text_representation: + extension: .md + format_name: myst + format_version: 0.13 + jupytext_version: 1.16.4 +kernelspec: + display_name: geo + language: python + name: python3 +--- + +# Lab 7 + +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/giswqs/geog-312/blob/main/book/labs/lab_07.ipynb) + +## Overview + +In this lab, you will delve deeper into Leafmap, a powerful Python package for interactive geospatial mapping and analysis. You will explore how to work with various types of raster and vector data, customize basemaps and map layers, and visualize data using Cloud Optimized GeoTIFFs (COGs), PMTiles, GeoParquet, and other formats. You will also learn how to enhance your maps with advanced features like legends, colorbars, marker clusters, and split maps for comparison. + +## Objective + +* Create and customize interactive maps using Leafmap. +* Add and configure basemaps, XYZ tile layers, and WMS layers. +* Visualize various raster formats such as GeoTIFF, Cloud Optimized GeoTIFF (COG), and multi-band imagery. +* Explore vector data visualization, including marker clusters and choropleth maps. +* Implement advanced mapping features like legends, colorbars, and split map comparisons. + ++++ + +## Exercise 1: Creating an Interactive Map + +1. Create an interactive map with search functionality that allows users to search for places and zoom to them. Disable the draw control on the map. + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/b930fb63-3bd1-4d7e-9bf8-87e6d398e5c3) + ++++ + +## Exercise 2: Adding XYZ and WMS Tile Layers + +1. Add a custom XYZ tile layer ([USGS Topographic basemap](https://apps.nationalmap.gov/services)) using the following URL: + - URL: `https://basemap.nationalmap.gov/arcgis/rest/services/USGSTopo/MapServer/tile/{z}/{y}/{x}` +2. Add two WMS layers to visualize NAIP imagery and NDVI using a USGS WMS service. + - URL: `https://imagery.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer?` + - Layer names: `USGSNAIPImagery:FalseColorComposite`, `USGSNAIPImagery:NDVI_Color` + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/28dd8511-a0ac-4b44-ab02-9ed791817043) + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/719c1e07-f955-42d6-985c-b5842c9eac4c) + ++++ + +## Exercise 3: Adding Map Legends + +1. Add the [ESA World Cover](https://esa-worldcover.org/en) WMS tile layer to the map. + - URL: `https://services.terrascope.be/wms/v2?` + - Layer name: `WORLDCOVER_2021_MAP` +2. Add a legend to the map using the leafmap built-in `ESA_WorldCover` legend. + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/be5a9b07-4f6c-4245-9737-31db2df14f7f) + ++++ + +## Exercise 4: Creating Marker Clusters + +1. Create a marker cluster visualization from a GeoJSON file of building centroids: + - URL: https://github.com/opengeos/datasets/releases/download/places/wa_building_centroids.geojson + - Hint: Read the GeoJSON file using GeoPandas and add "latitude" and "longitude" columns to the GeoDataFrame. +2. Create circle markers for each building centroid using the `Map.add_circle_markers_from_xy()` method with the following styling: + * Radius: 5 + * Outline color: "red" + * Fill color: "yellow" + * Fill opacity: 0.8 + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/d60cbfc7-b8c9-4cab-8852-bc34e82fd665) + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/637e00ae-89af-495e-84b4-e668e16cce88) + ++++ + +## Exercise 5: Visualizing Vector Data + +1. Visualize the building polygons GeoJSON file and style it with: + * Outline color: "red" + * No fill color + * URL: https://github.com/opengeos/datasets/releases/download/places/wa_overture_buildings.geojson + +2. Visualize the road polylines GeoJSON file and style it with: + * Line color: "red" + * Line width: 2 + * URL: https://github.com/opengeos/datasets/releases/download/places/las_vegas_roads.geojson + +3. Create a choropleth map of county areas in the US: + * URL: https://github.com/opengeos/datasets/releases/download/us/us_counties.geojson + * Column: `CENSUSAREA` + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/069eb704-c409-44ee-af9e-7582d1ab23a5) + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/f28d19a6-4f60-484c-b2f7-c1ecce7ecb26) + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/3aa9f54f-64d7-4788-89f1-f3ab88c1aa2e) + ++++ + +## Exercise 6: Visualizing GeoParquet Data + +1. Visualize GeoParquet data of US states: + + - URL: https://github.com/opengeos/datasets/releases/download/us/us_states.parquet + - Style: Outline color of "red" with no fill. + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/b6d9ec09-035b-4df7-8ab7-a7b4588f1e71) + ++++ + +## Exercise 7: Visualizing PMTiles + +1. Visualize the Overture Maps building dataset using PMTiles: + * URL: https://overturemaps-tiles-us-west-2-beta.s3.amazonaws.com/2024-09-18/buildings.pmtiles + * Style: Blue fill with 0.4 opacity, red outline. + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/4ee08461-c962-4c37-8979-7105f588842a) + ++++ + +## Exercise 8: Visualizing Cloud Optimized GeoTIFFs (COGs) + +1. Visualize Digital Elevation Model (DEM) data using the following COG file: + - URL: https://github.com/opengeos/datasets/releases/download/raster/dem.tif + - Apply a terrain colormap to show elevation values. + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/21e1f7dd-466e-4880-94a5-0365bf1495dc) + ++++ + +## Exercise 9: Visualizing Local Raster Data + +1. Visualize the following raster datasets using the Map.add_raster() method: + + * Aerial Imagery: https://github.com/opengeos/datasets/releases/download/places/wa_building_image.tif + * Building Footprints: https://github.com/opengeos/datasets/releases/download/places/wa_building_masks.tif (use the "tab20" colormap and opacity of 0.7) + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/faf7c345-6a3f-4ca0-8eca-7417e6568867) + ++++ + +## Exercise 10: Creating a Split Map + +1. Create a split map to compare imagery of Libya before and after the 2023 flood event: + + * Pre-event imagery: https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-07-01.tif + * Post-event imagery: https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-09-13.tif + +```{code-cell} ipython3 + +``` + +![image](https://github.com/user-attachments/assets/8cab4f1c-2dba-4652-a644-3ce6be4bbbd2)