This page shows practical ways to fetch organisation unit geojson from the DHIS2 Web API using dhis2-python-client
library.
In the climate-tools environment, dhis2-python-client
library is already installed. You only need to come up with your DHIS2 credentials and server address to use this library. Below is a basic usage:
import geopandas as gpd
import io
import json
from dhis2_client import DHIS2Client
from dhis2_client.settings import ClientSettings
# Client configuration
cfg = ClientSettings(
base_url="http://localhost:8080",
username="admin",
password="district")
client = DHIS2Client(settings=cfg)
info = client.get_system_info()
# Check if everything is working.
# You should see your current DHIS2 version info.
print("▶ Current DHIS2 version:", info["version"])
▶ Current DHIS2 version: 2.42.2-SNAPSHOT
Once you are sure to be able to make connection with your DHIS2 instance, you can proceed to fetching geojson info. But first you need to decide which orgunit(s) or level you want to fetch. Below is an example for fetching geojson info for all orgunits from `level=2
geojson = client.get("/api/organisationUnits.geojson", params={"level": 2})
# Convert dict -> string -> file-like object
geojson_str = json.dumps(geojson)
geojson_io = io.StringIO(geojson_str)
# Read directly into a GeoDataFrame
gdf = gpd.read_file(geojson_io)
# Show a compact summary
gdf[["name", "level", "geometry"]].head()
Loading...
# Do a quick plot
gdf.plot()
<Axes: >
