{ "cells": [ { "cell_type": "markdown", "id": "d2583da9-3f1e-40e6-b81d-700f26e89daa", "metadata": { "tags": [] }, "source": [ "## Accessing NOAA C-CAP Regional Land Cover with the Planetary Computer STAC API\n", "\n", "The [National Oceanic and Atmospheric Administration (NOAA) Coastal Change Analysis Program (C-CAP) Regional Land Cover](https://coast.noaa.gov/digitalcoast/data/ccapregional.html) dataset features standardized raster based land cover for coastal areas of the contiguous United States (CONUS) and Hawaii at a 30-meter resolution available from 1975 through 2016. The data are derived from Landsat's Thematic Mapper (TM) satellite imagery and ancillary information.\n", "\n", "The use of standardized data and procedures assures consistency through time and across geographies. C-CAP data forms the coastal expression of the National Land Cover Database (NLCD) and the A-16 land cover theme of the National Spatial Data Infrastructure (NSDI). \n", "\n", "In this notebook, we'll demonstrate how to access and work with this data through the Planetary Computer. Documentation for this dataset is available at the [Planetary Computer Data Catalog](https://planetarycomputer.microsoft.com/dataset/noaa-c-cap)." ] }, { "cell_type": "markdown", "id": "6c7da081-b792-45f2-a428-ea8f9ecbda08", "metadata": {}, "source": [ "### Environment setup\n", "\n", "This notebook works with or without an API key, but you will be given more permissive access to the data with an API key.\n", "The [Planetary Computer Hub](https://planetarycomputer.microsoft.com/compute) is pre-configured to use your API key." ] }, { "cell_type": "code", "execution_count": 1, "id": "f3f030fa-8639-4b4e-b0a1-f3226375c28e", "metadata": {}, "outputs": [], "source": [ "from matplotlib.colors import ListedColormap\n", "from pystac.extensions.item_assets import ItemAssetsExtension\n", "\n", "import numpy as np\n", "import odc.stac\n", "import planetary_computer\n", "import pystac_client\n", "import rasterio\n", "import rasterio.features\n", "import rich.table" ] }, { "cell_type": "markdown", "id": "980b87ca-9fcb-4146-94ef-420542debf7c", "metadata": {}, "source": [ "### Data access\n", "\n", "The datasets hosted by the Planetary Computer are available from [Azure Blob Storage](https://docs.microsoft.com/en-us/azure/storage/blobs/). We'll use [pystac-client](https://pystac-client.readthedocs.io/) to search the Planetary Computer's [STAC API](https://planetarycomputer.microsoft.com/api/stac/v1/docs) for the subset of the data that we care about, and then we'll load the data directly from Azure Blob Storage. We'll specify a `modifier` so that we can access the data stored in the Planetary Computer's private Blob Storage Containers. See [Reading from the STAC API](https://planetarycomputer.microsoft.com/docs/quickstarts/reading-stac/) and [Using tokens for data access](https://planetarycomputer.microsoft.com/docs/concepts/sas/) for more." ] }, { "cell_type": "code", "execution_count": 2, "id": "66b5faa1-abbe-4c3f-8b86-90b83722b9b0", "metadata": {}, "outputs": [], "source": [ "catalog = pystac_client.Client.open(\n", " \"https://planetarycomputer.microsoft.com/api/stac/v1\",\n", " modifier=planetary_computer.sign_inplace,\n", ")" ] }, { "cell_type": "markdown", "id": "527d242e-32b4-49fb-a513-a74ee38495da", "metadata": { "tags": [] }, "source": [ "### Query for available data\n", "\n", "The NOAA C-CAP dataset reaches back to 1975 in select areas. We selected the coordinates for Lansing, MI to showcase data for 1975 and use the STAC API to find what data items are available." ] }, { "cell_type": "code", "execution_count": 3, "id": "63ddd01e-2ae3-4b5d-af8f-cdcbb56f0423", "metadata": {}, "outputs": [], "source": [ "# Select area and time of interest\n", "latitude = 42.7325\n", "longitude = -84.5555\n", "location = [longitude, latitude]\n", "geometry = {\n", " \"type\": \"Point\",\n", " \"coordinates\": location,\n", "}\n", "datetimes = [\n", " \"1975\",\n", "]\n", "\n", "buffer = 2\n", "bbox = [longitude - buffer, latitude - buffer, longitude + buffer, latitude + buffer]\n", "items = dict()\n", "search = catalog.search(collections=\"noaa-c-cap\", intersects=geometry, datetime=\"1975\")\n", "item = next(search.items())" ] }, { "cell_type": "markdown", "id": "69f3564c-bd52-452d-875d-34d9309a64c9", "metadata": {}, "source": [ "### Available Assets" ] }, { "cell_type": "markdown", "id": "cfd0f626-55bb-4bcf-a2b6-d276df55a0d4", "metadata": {}, "source": [ "Let's display the available assets for the NOAA C-CAP item. " ] }, { "cell_type": "code", "execution_count": 4, "id": "a3f2152a-6925-4a01-b24f-485cca0b9f55", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┏━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", "┃ Key ┃ Title ┃\n", "┡━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", "│ data │ │\n", "│ tilejson │ TileJSON with default rendering │\n", "│ rendered_preview │ Rendered preview │\n", "└──────────────────┴─────────────────────────────────┘\n", "\n" ], "text/plain": [ "┏━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", "┃\u001b[1m \u001b[0m\u001b[1mKey \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mTitle \u001b[0m\u001b[1m \u001b[0m┃\n", "┡━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", "│ data │ │\n", "│ tilejson │ TileJSON with default rendering │\n", "│ rendered_preview │ Rendered preview │\n", "└──────────────────┴─────────────────────────────────┘\n" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = rich.table.Table(\"Key\", \"Title\")\n", "for key, asset in item.assets.items():\n", " t.add_row(key, asset.title)\n", "t" ] }, { "cell_type": "markdown", "id": "fcd49bc9-32ac-4b10-83f8-e9fbd2a870ac", "metadata": {}, "source": [ "### Loading the land cover data\n", "\n", "For this example, we'll visualize the land cover classifications surrounding Lansing, MI. Let's grab the data COG and load them into an xarray using [odc-stac](https://github.com/opendatacube/odc-stac). \n" ] }, { "cell_type": "code", "execution_count": 5, "id": "a8fc2966-2787-4f20-bb2d-e47d4829cec5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n", "Dimensions: (y: 16091, x: 12565, time: 1)\n", "Coordinates:\n", " * y (y) float64 2.491e+06 2.491e+06 ... 2.008e+06 2.008e+06\n", " * x (x) float64 7.454e+05 7.455e+05 ... 1.122e+06 1.122e+06\n", " spatial_ref int32 5070\n", " * time (time) datetime64[ns] 1975-01-01\n", "Data variables:\n", " data (time, y, x) uint8 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0