{
"cells": [
{
"cell_type": "markdown",
"id": "25db29cc",
"metadata": {},
"source": [
"## Accessing High Resolution Electricity Access (HREA) data with the Planetary Computer STAC API\n",
"\n",
"The HREA project aims to provide open access to new indicators of electricity access and reliability across the world. Leveraging VIIRS satellite imagery with computational methods, these high-resolution data provide new tools to track progress towards reliable and sustainable energy access across the world.\n",
"\n",
"This notebook provides an example of accessing HREA data using the Planetary Computer STAC API."
]
},
{
"cell_type": "markdown",
"id": "202b9ace",
"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. The Planetary Computer Hub is pre-configured to use your API key."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "7c8c9a42",
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.colors as colors\n",
"import matplotlib.pyplot as plt\n",
"import planetary_computer\n",
"import rasterio\n",
"import rioxarray\n",
"\n",
"import pystac_client\n",
"from rasterio.plot import show"
]
},
{
"cell_type": "markdown",
"id": "94749e92",
"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": "e5198dec",
"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": "0eb0ea8f-6c10-48d9-b103-f20aa14ad3af",
"metadata": {},
"source": [
"The HREA dataset covers all of Africa as well as Ecuador. Let's pick up an area of interest that covers Djibouti and query the Planetary Computer API for data coverage for the year 2019."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0d76a979",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Returned 2 Items\n"
]
}
],
"source": [
"area_of_interest = {\"type\": \"Point\", \"coordinates\": (42.4841, 11.7101)}\n",
"\n",
"search = catalog.search(\n",
" collections=[\"hrea\"], intersects=area_of_interest, datetime=\"2019-12-31\"\n",
")\n",
"\n",
"# Check how many items were returned, there could be more pages of results as well\n",
"items = search.item_collection()\n",
"print(f\"Returned {len(items)} Items\")"
]
},
{
"cell_type": "markdown",
"id": "6796a3a8",
"metadata": {},
"source": [
"We found 3 items for our search. We'll grab jsut the one for Djibouti and see what data assets are available on it."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8daca8f4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"lightscore: Probability of electrification\n",
"light-composite: Nighttime light annual composite\n",
"night-proportion: Proportion of nights a settlement is brighter than uninhabited areas\n",
"estimated-brightness: Estimated brightness levels\n"
]
}
],
"source": [
"(item,) = [x for x in items if \"Djibouti\" in x.id]\n",
"data_assets = [\n",
" f\"{key}: {asset.title}\"\n",
" for key, asset in item.assets.items()\n",
" if \"data\" in asset.roles\n",
"]\n",
"\n",
"print(*data_assets, sep=\"\\n\")"
]
},
{
"cell_type": "markdown",
"id": "c8dd36b8",
"metadata": {},
"source": [
"### Plotting the data\n",
"\n",
"Let's pick the variable `light-composite`, and read in the entire GeoTIFF to plot."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "56f0f1f9",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
"