From c336d0be99b30005a17bd75881ce38ca115b3358 Mon Sep 17 00:00:00 2001 From: Ranjan Shrestha Date: Mon, 28 Jul 2025 16:38:17 +0545 Subject: [PATCH 1/2] Add env vars that conditionally reduce the geo size --- config.py | 3 +++ docker-compose.yml | 3 +++ geocoding.py | 11 ++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/config.py b/config.py index 7442139..9c72ea1 100644 --- a/config.py +++ b/config.py @@ -11,6 +11,9 @@ class Settings(BaseSettings): GAUL_FILE_PATH: str = "./geodata-prep/geodata/gaul.gpkg" GAUL_DOWNLOAD_URL: str = "https://github.com/IFRCGo/geocoding-service/releases/download/v1.0.0/gaul.gpkg" + TOLERANCE: float = 0.1 + GEO_SIMPLIFY: bool = True + settings = Settings() diff --git a/docker-compose.yml b/docker-compose.yml index b4d0c31..0a6068d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,4 +11,7 @@ services: environment: WAB_FILE_PATH: /geodata/simple.wab.fgb GAUL_FILE_PATH: /geodata/simple.gaul.gpkg + TOLERANCE: 0.1 + GEO_SIMPLIFY: True + command: /bin/sh -c "uvicorn service:app --host 0.0.0.0 --port 8001 --reload --workers 1" diff --git a/geocoding.py b/geocoding.py index 3524dc2..05940bf 100644 --- a/geocoding.py +++ b/geocoding.py @@ -6,6 +6,8 @@ from shapely.geometry.base import BaseGeometry from shapely.ops import unary_union +from config import settings + WAB_LAYER = "Layer1" @@ -51,7 +53,10 @@ def _init_adm_mapping(self): properties = feature["properties"] geometry = feature["geometry"] adm1 = properties["ADM1_CODE"] - self._adm1_to_geometry_mapping[adm1] = shape(geometry) + geom = shape(geometry) + if settings.GEO_SIMPLIFY: + geom = geom.simplify(tolerance=settings.TOLERANCE, preserve_topology=True) + self._adm1_to_geometry_mapping[adm1] = geom def get_iso3_from_geometry(self, lng: float, lat: float) -> Country | None: point = Point(lng, lat) @@ -76,6 +81,8 @@ def get_geometry_from_country_name(self, country_name: str) -> AdminGeometry | N for feature in src: if feature["properties"]["name"].lower().strip() == country_name: geom = shape(feature["geometry"]) + if settings.GEO_SIMPLIFY: + geom = geom.simplify(tolerance=settings.TOLERANCE, preserve_topology=True) val = AdminGeometry( geometry=mapping(geom), bbox=geom.bounds, @@ -125,6 +132,8 @@ def get_geometry_from_iso3(self, iso3: str) -> AdminGeometry | None: continue if iso3_from_feature.lower().strip() == iso3: geom = shape(geometry) + if settings.GEO_SIMPLIFY: + geom = geom.simplify(tolerance=settings.TOLERANCE, preserve_topology=True) val = AdminGeometry( geometry=mapping(geom), bbox=geom.bounds, From 5afd5c2424fb109e69c2afd0fbbd24feac53d6f1 Mon Sep 17 00:00:00 2001 From: Ranjan Shrestha Date: Mon, 28 Jul 2025 16:49:58 +0545 Subject: [PATCH 2/2] chore(docker image): Update the base image --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 7a90951..172c9a8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.10-slim-buster +FROM python:3.10-slim-bookworm COPY --from=ghcr.io/astral-sh/uv:0.5.29 /uv /uvx /bin/