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:
from dhis2_client import DHIS2Client
from dhis2_client.settings import ClientSettings
# Client configuration
cfg = ClientSettings(
base_url="https://play.im.dhis2.org/stable-2-42-3-1",
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.3.1
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 org unit(s) or level you want to fetch. Below is an example for fetching GeoJSON info for all level 2 org units:
# Get org units GeoJSON from DHIS2
level = 2
org_units_geojson = client.get_org_units_geojson(level=level)To load this GeoJSON with geopandas, the easiest way currently is to convert the GeoJSON dictionary to a string, and then loading that string into geopandas:
# Convert GeoJSON to geopandas
import geopandas as gpd
import json
gdf = gpd.read_file(json.dumps(org_units_geojson))
# Show the contents of the loaded org units
gdfSkipping field groups: unsupported OGR type: 5
Loading...
# Do a quick plot
gdf.plot()<Axes: >