Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GTC-3137 Fix extract_level_gid to deal with misformats in GADM 4.1 #634

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/routes/political/id_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,14 @@ def form_admin_id_lookup_response(

def extract_level_gid(gid_level: int, match):
gid_level_name = f"gid_{gid_level}"
return (match[gid_level_name].rsplit("_")[0]).split(".")[gid_level]
gid_str = match[gid_level_name]

# Exception because of bad formatting of GHA gids in gadm_administrative_boundaries/v4.1
if gid_str.startswith("GHA") and not gid_str.startswith("GHA."):
gid_str = "GHA." + gid_str[3:]
# Exception because bad ids IDN.35.4, IDN.35.8, IDN.35.9, IDN.35.13, IDN.35.14
# (they are missing final '_1') in gadm_administrative_boundaries/v4.1
if gid_str.startswith("IDN") and not gid_str.endswith("_1"):
gid_str += "_1"

return (gid_str.rsplit("_")[0]).split(".")[gid_level]
23 changes: 22 additions & 1 deletion tests_v2/unit/app/routes/political/id_lookup/test_id_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from app.models.pydantic.geostore import GeostoreCommon
from app.routes.political import id_lookup
from app.routes.political.id_lookup import _admin_boundary_lookup_sql, normalize_names
from app.routes.political.id_lookup import _admin_boundary_lookup_sql, normalize_names, extract_level_gid

ENDPOINT_UNDER_TEST = "/political/id-lookup"

Expand Down Expand Up @@ -279,6 +279,27 @@ async def test_id_lookup_matches_hide_extraneous(
assert resp.status_code == 200


@pytest.mark.asyncio
async def test_extract_level_gid() -> None:
# Normal gid values
match1 = {"gid_0": "USA", "gid_1": "USA.5_1", "gid_2": "USA.5.10_1"}
assert extract_level_gid(0, match1) == "USA"
assert extract_level_gid(1, match1) == "5"
assert extract_level_gid(2, match1) == "10"

# Ghana values with bad formatting (missing dot after GHA in gadm 4.1)
match2 = {"gid_0": "GHA", "gid_1": "GHA7_2", "gid_2": "GHA7.1_2"}
assert extract_level_gid(0, match2) == "GHA"
assert extract_level_gid(1, match2) == "7"
assert extract_level_gid(2, match2) == "1"

# Indonesia values with bad formatting (missing suffix _1 in gadm 4.1)
match3 = {"gid_0": "IDN", "gid_1": "IDN.35_1", "gid_2": "IDN.35.4"}
assert extract_level_gid(0, match3) == "IDN"
assert extract_level_gid(1, match3) == "35"
assert extract_level_gid(2, match3) == "4"


async def _query_dataset_json_mocked_no_results(
dataset: str,
version: str,
Expand Down