Skip to content
Closed
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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/

Expand Down
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 10 additions & 1 deletion geocoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from shapely.geometry.base import BaseGeometry
from shapely.ops import unary_union

from config import settings

WAB_LAYER = "Layer1"


Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading