Skip to content
Merged
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
5 changes: 1 addition & 4 deletions backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ class Config:
# A freely definable secret key for connecting the front end with the back end
SECRET_KEY: str = os.getenv("TM_SECRET", None)

# OSM API, Nomimatim URLs
# OSM API URL
OSM_SERVER_URL: str = os.getenv("OSM_SERVER_URL", "https://www.openstreetmap.org")
OSM_NOMINATIM_SERVER_URL: str = os.getenv(
"OSM_NOMINATIM_SERVER_URL", "https://nominatim.openstreetmap.org"
)

POSTGRES_USER: str = os.getenv("POSTGRES_USER", "postgres")
POSTGRES_PASSWORD: str = os.getenv("POSTGRES_PASSWORD", None)
Expand Down
47 changes: 20 additions & 27 deletions backend/models/postgis/project.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import json
import os
import re
from typing import Optional

import geojson
import requests
from cachetools import TTLCache
from databases import Database
from fastapi import HTTPException
from geoalchemy2 import Geometry, WKTElement
from geoalchemy2.shape import to_shape
from loguru import logger
from pg_nearest_city import AsyncNearestCity, DbConfig
from shapely import wkb
from shapely.geometry import shape
from sqlalchemy import (
Expand Down Expand Up @@ -326,8 +325,9 @@ def set_default_changeset_comment(self):
else f"{default_comment}-{self.id}"
)

def set_country_info(self):
"""Sets the default country based on centroid"""
async def set_country_info(self):
"""Sets the default country based on centroid, using the local
pg-nearest-city dataset instead of a remote Nominatim lookup"""
if not self.centroid:
logger.debug("Skipping country lookup due to missing centroid")
return
Expand All @@ -349,30 +349,23 @@ def set_country_info(self):
centroid = WKTElement(centroid_wkt, srid=4326)
centroid = to_shape(centroid)
lat, lng = (centroid.y, centroid.x)
url = "{0}/reverse?format=jsonv2&lat={1}&lon={2}&accept-language=en".format(
settings.OSM_NOMINATIM_SERVER_URL, lat, lng

db_config = DbConfig(
dbname=settings.POSTGRES_DB,
user=settings.POSTGRES_USER,
password=settings.POSTGRES_PASSWORD,
host=settings.POSTGRES_ENDPOINT,
port=int(settings.POSTGRES_PORT),
)
headers = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/58.0.3029.110 Safari/537.3"
),
"Referer": os.environ.get("TM_APP_BASE_URL", "https://example.com"),
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
country_info = response.json() # returns a dict
if country_info["address"].get("country") is not None:
self.country = [country_info["address"]["country"]]
except (
KeyError,
AttributeError,
requests.exceptions.ConnectionError,
requests.exceptions.HTTPError,
) as e:
logger.debug(e, exc_info=True)
async with AsyncNearestCity(db_config) as geocoder:
location = await geocoder.query(lng, lat)
if location is not None:
self.country = [location.country_name]
except (RuntimeError, ValueError) as e:
logger.debug(
f"Country lookup via pg-nearest-city failed: {e}", exc_info=True
)

async def create(self, project_name: str, db: Database):
"""Creates and saves the current model to the DB"""
Expand Down Expand Up @@ -890,7 +883,7 @@ async def update(self, project_dto: ProjectDTO, db: Database):

# try to update country info if that information is not present
if not self.country:
self.set_country_info()
await self.set_country_info()

columns = {
c.key: getattr(self, c.key) for c in inspect(self).mapper.column_attrs
Expand Down
2 changes: 1 addition & 1 deletion backend/services/project_admin_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async def create_draft_project(
tasks = draft_project_dto.tasks

await ProjectAdminService._attach_tasks_to_project(draft_project, tasks, db)
draft_project.set_country_info()
await draft_project.set_country_info()

if draft_project_dto.cloneFromProjectId:
draft_project.set_default_changeset_comment()
Expand Down
1 change: 0 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ TM_ORG_GITHUB=${TM_ORG_GITHUB:-https://github.com/hotosm}
# By default, it's the public OpenStreetMap.org server
OSM_SERVER_URL=${OSM_SERVER_URL:-https://www.openstreetmap.org}
OSM_SERVER_API_URL=${OSM_SERVER_API_URL:-https://api.openstreetmap.org}
OSM_NOMINATIM_SERVER_URL=${OSM_NOMINATIM_SERVER_URL:-https://nominatim.openstreetmap.org}
OSM_REGISTER_URL=${OSM_REGISTER_URL:-https://www.openstreetmap.org/user/new}
OSM_USER_AGENT=${OSM_USER_AGENT:-HOT-TaskingManager}

Expand Down
Loading
Loading