Fetch any HDX dataset by id (escape hatch)¶
Beyond the curated catalogue, hdx_id= fetches any of the ~21k HDX datasets by its id, with an optional resource= filter. Live public query; wrapped for offline nbval-lax.
Setup¶
Import the unified EarthLens entry point and prepare an output directory for the downloaded files.
In [ ]:
Copied!
from pathlib import Path
from earthlens import EarthLens
OUT_DIR = Path('hdx_output')
OUT_DIR.mkdir(exist_ok=True)
from pathlib import Path
from earthlens import EarthLens
OUT_DIR = Path('hdx_output')
OUT_DIR.mkdir(exist_ok=True)
Build the request¶
Configure EarthLens for the hdx backend, targeting the wfp-topline-figures dataset by its id and filtering to its *.csv resource. Construction is kept on its own statement so the download step below reads cleanly.
In [ ]:
Copied!
dataset = EarthLens(
data_source='hdx',
variables={},
hdx_id='wfp-topline-figures',
resource='*.csv',
path=str(OUT_DIR),
)
dataset = EarthLens(
data_source='hdx',
variables={},
hdx_id='wfp-topline-figures',
resource='*.csv',
path=str(OUT_DIR),
)
Download the resource¶
Call download() to fetch the matching CSV into OUT_DIR. The whole call is wrapped in a try/except so the notebook degrades gracefully (skips rather than fails) when the live HDX query is unavailable offline.
In [ ]:
Copied!
paths = None
try:
paths = dataset.download(progress_bar=False)
print('downloaded:', [Path(p).name for p in paths])
except Exception as exc:
print(f'skipped live query: {type(exc).__name__}: {exc}')
paths = None
try:
paths = dataset.download(progress_bar=False)
print('downloaded:', [Path(p).name for p in paths])
except Exception as exc:
print(f'skipped live query: {type(exc).__name__}: {exc}')