Risk indicators — Global Forest Watch (live, needs a key)¶
The third risk source is the Global Forest Watch (GFW) Data API. It serves forest indicators — here annual tree-cover loss by country — and the GADM admin geometry those indicators are computed over.
Unlike ThinkHazard! and INFORM, GFW needs a free API key:
- Create one by following the GFW guide: https://www.globalforestwatch.org/help/developers/guides/create-and-use-an-api-key/.
- Make it available to the notebook via the
GFW_API_KEYenvironment variable (e.g.export GFW_API_KEY=...before launching Jupyter, or set it in your shell profile).
The backend reads GFW_API_KEY from the environment automatically — the
key is never passed in code or printed in this notebook. If the key is
missing, constructing a gfw:* request raises an AuthenticationError
naming GFW_API_KEY.
import tempfile
import matplotlib.pyplot as plt
from earthlens.core import EarthLens
OUT = tempfile.mkdtemp() # the tabular download also writes a CSV here
Annual tree-cover loss by country¶
variables=["gfw:tree_cover_loss"] runs GFW's SQL query for the chosen
country (canopy density ≥ 30%) and returns one row per year. The
data_source="gfw" alias reaches the same risk-indicators backend.
loss = EarthLens(
data_source="gfw",
variables=["gfw:tree_cover_loss"],
country="KEN",
path=OUT,
).download()
print(loss.shape)
loss.head()
umd_tree_cover_loss__year is the year and umd_tree_cover_loss__ha the
hectares of tree-cover loss in that year (UMD/Hansen). Plain pandas from
here — plot the annual series:
fig, ax = plt.subplots(figsize=(11, 4))
ax.bar(
loss["umd_tree_cover_loss__year"],
loss["umd_tree_cover_loss__ha"],
color="tab:green",
alpha=0.8,
)
ax.set_title("Kenya — annual tree-cover loss (canopy >= 30%)")
ax.set_xlabel("year")
ax.set_ylabel("tree-cover loss (ha)")
fig.tight_layout()
The admin boundary as a FeatureCollection¶
gfw:admin_boundary is a vector dataset: it returns the GADM country
geometry the indicators are computed over as a pyramids
FeatureCollection (a GeoDataFrame subclass), not a table.
boundary = EarthLens(
data_source="gfw",
variables=["gfw:admin_boundary"],
country="KEN",
path=OUT,
).download()
print("type:", type(boundary).__name__)
print("features:", len(boundary))
print("crs:", boundary.crs)
print("bounds:", boundary.total_bounds)
It carries a CRS (EPSG:4326) and a geometry column, so it plots directly:
ax = boundary.plot(facecolor="none", edgecolor="tab:green", linewidth=1.2)
ax.set_title("Kenya — GADM admin boundary (GFW geostore)")
ax.set_xlabel("longitude")
ax.set_ylabel("latitude")
Takeaway¶
- GFW needs a free key in
GFW_API_KEY; the backend reads it from the environment — never hard-code or print it. gfw:tree_cover_lossis tabular (a per-yearDataFrame);gfw:admin_boundaryis vector (aFeatureCollectionthat plots directly) — the per-instanceOUTPUT_KINDin action.- The same
EarthLens(...).download()call shape serves all three risk sources; see the quickstart for the public ThinkHazard! / INFORM sources and catalog & behaviour for the full dataset list.