Skip to content

Commit

Permalink
WIP: Run a query instead of canned response
Browse files Browse the repository at this point in the history
  • Loading branch information
dmannarino committed Dec 19, 2024
1 parent 9d2ae13 commit 44a253f
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions app/routes/thematic/geoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from fastapi import APIRouter, Query

# from app.routes.datasets.queries import _query_dataset_json
from app.routes.datasets.queries import _query_dataset_json


router = APIRouter()

Expand All @@ -14,7 +15,6 @@
)
async def geoencode(
*,
# country, region, subregion: Tuple[str, str, str] = Depends(geo_hierarchy_dependency),
admin_source: Optional[str] = Query(
"GADM",
description="The source of administrative boundaries to use."
Expand All @@ -23,47 +23,55 @@ async def geoencode(
None,
description="Version of the administrative boundaries dataset to use.",
),
country_name: str = Query(
country: str = Query(
description="Name of the country to match.",
),
region_name: Optional[str] = Query(
region: Optional[str] = Query(
None,
description="Name of the region to match.",
),
subregion_name: Optional[str] = Query(
subregion: Optional[str] = Query(
None,
description="Name of the subregion to match.",
),
):
""" Look-up administrative boundaries matching a specified country name
(and possibly region and subregion, if specified).
""" Look-up administrative boundary IDs matching a specified country name
(and region name and subregion names, if specified).
"""

return await lookup_admin_boundaries(
admin_source, admin_version, country_name, region_name, subregion_name
return await lookup_admin_boundary_ids(
admin_source, admin_version, country, region, subregion

)


async def lookup_admin_boundaries(
async def lookup_admin_boundary_ids(
admin_source: str,
admin_version: str,
country_name: str,
region_name: Optional[str],
subregion_name: Optional[str]
) -> Dict[str, Any]:
dataset = "gadm_administrative_boundaries"
source_to_datasets = {
"gadm": "gadm_administrative_boundaries"
}

dataset = source_to_datasets[admin_source.lower()]
version = f"v{admin_version}"

matches = [
{
"country": {"id": "HND", "name": "Honduras"},
"region": {"id": 1, "name": "Atlántida"},
"subregion": {"id": 4, "name": "Jutiapa"}
}
]
base_sql = f"SELECT gid_0, gid_1, gid_2, country, name_1, name_2 FROM {dataset}"
where_filter:str = f" AND WHERE country='{country_name}'"
if region_name is not None:
where_filter += f" AND WHERE region='{region_name}'"
if subregion_name is not None:
where_filter += f" AND WHERE subregion='{subregion_name}'"

json_data: List[Dict[str, Any]] = await _query_dataset_json(
dataset, version, base_sql + where_filter, None
)

return {
"adminSource": admin_source,
"adminVersion": admin_version,
"matches": matches
"matches": json_data
}

0 comments on commit 44a253f

Please sign in to comment.