Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class Settings(BaseSettings):
# EMDAT GAUL
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"
# World Administrative Boundaries
SUPER_SIMPLIFIED_WAB_FILE_PATH: str = "./geodata-prep/geodata/super_simple.wab.fgb"
SUPER_SIMPLIFIED_WAB_DOWNLOAD_URL: str = "https://github.com/IFRCGo/geocoding-service/releases/download/v1.0.0/wab.fgb"
# EMDAT GAUL
SUPER_SIMPLIFIED_GAUL_FILE_PATH: str = "./geodata-prep/geodata/super_simple.gaul.gpkg"
SUPER_SIMPLIFIED_GAUL_DOWNLOAD_URL: str = "https://github.com/IFRCGo/geocoding-service/releases/download/v1.0.0/gaul.gpkg"


settings = Settings()
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ services:
environment:
WAB_FILE_PATH: /geodata/simple.wab.fgb
GAUL_FILE_PATH: /geodata/simple.gaul.gpkg
SUPER_SIMPLIFIED_WAB_FILE_PATH: /geodata/super_simple.wab.fgb
SUPER_SIMPLIFIED_GAUL_DOWNLOAD_URL: /geodata/super_simple.gaul.gpkg
command: /bin/sh -c "uvicorn service:app --host 0.0.0.0 --port 8001 --reload --workers 1"
1 change: 1 addition & 0 deletions geodata-prep/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ services:
environment:
DATA_DIR: /geodata
TOLERANCE: 0.001
HIGH_TOLERANCE: 0.2
command: /bin/sh /code/prepare.sh
7 changes: 7 additions & 0 deletions geodata-prep/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -e
set -x

echo "Simplification tolerance is $TOLERANCE"
echo "Super Simplification tolerance is $HIGH_TOLERANCE"
echo "Data directory is at $DATA_DIR"

# PROCESS EMDAT GAUL
Expand All @@ -13,6 +14,7 @@ ZIP_NAME="gaul.zip"
ITEM_NAME="gaul2014_2015.gpkg"
FILE_NAME="gaul.gpkg"
SIMPLIFIED_FILE_NAME="simple.$FILE_NAME"
SUPER_SIMPLIFIED_FILE_NAME="super_simple.$FILE_NAME"

# Initialize
mkdir -p "$TMP_DIR"
Expand All @@ -25,11 +27,13 @@ unzip -u "$TMP_DIR/$ZIP_NAME" "$ITEM_NAME" -d "$TMP_DIR"

# Simplify GAUL file while preserving the topology
ogr2ogr "$TMP_DIR/$SIMPLIFIED_FILE_NAME" "$TMP_DIR/$ITEM_NAME" -simplify $TOLERANCE
ogr2ogr "$TMP_DIR/$SUPER_SIMPLIFIED_FILE_NAME" "$TMP_DIR/$ITEM_NAME" -simplify $HIGH_TOLERANCE
# QT_QPA_PLATFORM=offscreen qgis_process plugins enable grassprovider
# QT_QPA_PLATFORM=offscreen qgis_process run grass7:v.generalize --input="$TMP_DIR/$ITEM_NAME" --output="$TMP_DIR/$SIMPLIFIED_FILE_NAME" --threshold=0.01 --type=1 --method=0 --error="$TMP_DIR/errors.qgis.log"

# Cleanup
mv "$TMP_DIR/$SIMPLIFIED_FILE_NAME" "$DATA_DIR"
mv "$TMP_DIR/$SUPER_SIMPLIFIED_FILE_NAME" "$DATA_DIR"

# PROCESS WAL

Expand All @@ -39,6 +43,7 @@ DATA_DIR=/geodata
TMP_DIR="$DATA_DIR/tmp_wab"
FILE_NAME="wab.fgb"
SIMPLIFIED_FILE_NAME="simple.$FILE_NAME"
SUPER_SIMPLIFIED_FILE_NAME="super_simple.$FILE_NAME"

# Initialize
mkdir -p "$TMP_DIR"
Expand All @@ -48,6 +53,8 @@ curl --no-progress-meter --output "$TMP_DIR/$FILE_NAME" "https://public.opendata

# Simplify GAUL file while preserving the topology
ogr2ogr "$TMP_DIR/$SIMPLIFIED_FILE_NAME" "$TMP_DIR/$FILE_NAME" -simplify $TOLERANCE
ogr2ogr "$TMP_DIR/$SUPER_SIMPLIFIED_FILE_NAME" "$TMP_DIR/$FILE_NAME" -simplify $HIGH_TOLERANCE

# Cleanup
mv "$TMP_DIR/$SIMPLIFIED_FILE_NAME" "$DATA_DIR"
mv "$TMP_DIR/$SUPER_SIMPLIFIED_FILE_NAME" "$DATA_DIR"
19 changes: 18 additions & 1 deletion init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

class SharedMem(typing.TypedDict):
geocoder: FastGeocoder | None
super_simplified_geocoder: FastGeocoder | None


shared_mem: SharedMem = {"geocoder": None}
shared_mem: SharedMem = {"geocoder": None, "super_simplified_geocoder": None}

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -65,12 +67,27 @@ async def lifespan(app: FastAPI):
file_path=settings.GAUL_FILE_PATH,
)

_download_geodata(
name="SUPER_SIMPLIFIED_WAB",
url_path=settings.SUPER_SIMPLIFIED_WAB_DOWNLOAD_URL,
file_path=settings.SUPER_SIMPLIFIED_WAB_FILE_PATH,
)

_download_geodata(
name="SUPER_SIMPLIFIED_GAUL",
url_path=settings.SUPER_SIMPLIFIED_GAUL_DOWNLOAD_URL,
file_path=settings.SUPER_SIMPLIFIED_GAUL_FILE_PATH,
)

logger.info("Initializing geocoder")
geocoder = FastGeocoder(
settings.WAB_FILE_PATH,
settings.GAUL_FILE_PATH,
)
super_simplified_geocoder = FastGeocoder(settings.SUPER_SIMPLIFIED_WAB_FILE_PATH, settings.SUPER_SIMPLIFIED_GAUL_FILE_PATH)
shared_mem["geocoder"] = geocoder
shared_mem["super_simplified_geocoder"] = super_simplified_geocoder

logger.info("Initialization for geocoder complete.")

yield
Expand Down
14 changes: 6 additions & 8 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ async def home():


@app.get("/country/iso3")
async def get_iso3(lat: float, lng: float) -> geocoding.Country:
Comment thread
tnagorra marked this conversation as resolved.
async def get_iso3(lat: float, lng: float, simplified: bool = False) -> geocoding.Country:
"""Get the iso3 based on coordinate"""
try:
geocoder = shared_mem["geocoder"]
geocoder = shared_mem["geocoder"] if not simplified else shared_mem["super_simplified_geocoder"]
if not geocoder:
raise Exception("Geocoder is not initialized")
result = geocoder.get_iso3_from_geometry(lat, lng)
Expand All @@ -60,12 +60,11 @@ async def get_iso3(lat: float, lng: float) -> geocoding.Country:

@app.get("/country/geometry")
async def get_country_geometry(
country_name: str | None = None,
iso3: str | None = None,
country_name: str | None = None, iso3: str | None = None, simplified: bool = False
) -> geocoding.AdminGeometry:
"""Get the country geometry based on country name or iso3"""
try:
geocoder = shared_mem["geocoder"]
geocoder = shared_mem["geocoder"] if not simplified else shared_mem["super_simplified_geocoder"]
if not geocoder:
raise Exception("Geocoder is not initialized")
if iso3:
Expand All @@ -87,12 +86,11 @@ async def get_country_geometry(

@app.get("/admin2/geometries")
async def get_admin2_geometries(
admin1_codes: list[int] = Query(default=[]),
admin2_codes: list[int] = Query(default=[]),
admin1_codes: list[int] = Query(default=[]), admin2_codes: list[int] = Query(default=[]), simplified: bool = False
) -> geocoding.AdminGeometry:
"""Get the admin 2 geometries based on admin 1 codes or admin 2 codes"""
try:
geocoder = shared_mem["geocoder"]
geocoder = shared_mem["geocoder"] if not simplified else shared_mem["super_simplified_geocoder"]
if not geocoder:
raise Exception("Geocoder is not initialized")
if admin1_codes or admin2_codes:
Expand Down
Loading