From 677fc6626b5e0dd81c82f52211c659ca2ebdc750 Mon Sep 17 00:00:00 2001 From: moanos Date: Fri, 31 May 2024 11:41:59 +0200 Subject: [PATCH] fix: rework geocoding api usage, make api url custom --- .../migrations/0008_remove_location_osm_id.py | 20 ++++++++++ src/fellchensammlung/models.py | 3 +- src/fellchensammlung/tools/geo.py | 38 +++++++++++++++---- src/notfellchen/settings.py | 3 ++ 4 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 src/fellchensammlung/migrations/0008_remove_location_osm_id.py diff --git a/src/fellchensammlung/migrations/0008_remove_location_osm_id.py b/src/fellchensammlung/migrations/0008_remove_location_osm_id.py new file mode 100644 index 0000000..72b1cf0 --- /dev/null +++ b/src/fellchensammlung/migrations/0008_remove_location_osm_id.py @@ -0,0 +1,20 @@ +# Generated by Django 5.0.6 on 2024-06-05 19:52 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ( + "fellchensammlung", + "0007_remove_location_country_remove_location_description_and_more", + ), + ] + + operations = [ + migrations.RemoveField( + model_name="location", + name="osm_id", + ), + ] diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index 4d32797..a3089a1 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -64,10 +64,9 @@ class Species(models.Model): class Location(models.Model): def __str__(self): - return f"{self.name}" + return f"{self.name} ({self.latitude:.5}, {self.longitude:.5})" place_id = models.IntegerField() - osm_id = models.IntegerField() latitude = models.FloatField() longitude = models.FloatField() name = models.CharField(max_length=2000) diff --git a/src/fellchensammlung/tools/geo.py b/src/fellchensammlung/tools/geo.py index 77c4d0a..a5185b4 100644 --- a/src/fellchensammlung/tools/geo.py +++ b/src/fellchensammlung/tools/geo.py @@ -1,7 +1,10 @@ +import logging + import requests import json from notfellchen import __version__ as nf_version from fellchensammlung.models import Location +from notfellchen import settings from math import radians, sqrt, sin, cos, atan2 @@ -46,7 +49,7 @@ class RequestMock: class GeoAPI: - api_url = "https://nominatim.openstreetmap.org/search" + api_url = settings.GEOCODING_API_URL headers = { 'User-Agent': f"Notfellchen {nf_version}", 'From': 'info@notfellchen.org' # This is another valid field @@ -58,23 +61,42 @@ class GeoAPI: else: self.requests = requests - def get_coordinates_from_postcode(self, postcode): - result = self.requests.get(self.api_url, {"q": postcode, "format": "jsonv2"}, headers=self.headers).json()[0] + def get_coordinates_from_query(self, location_string): + result = self.requests.get(self.api_url, {"q": location_string, "format": "jsonv2"}, headers=self.headers).json()[0] return result["lat"], result["lon"] + def _get_raw_response(self, location_string): + result = self.requests.get(self.api_url, {"q": location_string, "format": "jsonv2"}, headers=self.headers) + return result.content + def get_location_from_string(self, location_string): - result = self.requests.get(self.api_url, {"q": location_string, "format": "jsonv2"}, headers=self.headers).json()[0] + try: + result = self.requests.get(self.api_url, + {"q": location_string, + "format": "jsonv2"}, + headers=self.headers).json() + except Exception as e: + logging.warning(f"Exception {e} when querying Nominatim") + return None + if len(result) == 0: + logging.warning(f"Couldn't find a result for {location_string} when querying Nominatim") + return None + + result = result[0] + if "name" in result: + name = result["name"] + else: + name = result["display_name"] location = Location.objects.create( place_id=result["place_id"], - osm_id=result["osm_id"], latitude=result["lat"], longitude=result["lon"], - name=result["name"], + name=name, ) return location if __name__ == "__main__": - geo = GeoAPI(debug=True) - print(geo.get_coordinates_from_postcode("72072")) + geo = GeoAPI(debug=False) + print(geo.get_coordinates_from_query("12101")) print(calculate_distance_between_coordinates(('48.4949904', '9.040330235970146'), ("48.648333", "9.451111"))) diff --git a/src/notfellchen/settings.py b/src/notfellchen/settings.py index e2b5093..ed9991f 100644 --- a/src/notfellchen/settings.py +++ b/src/notfellchen/settings.py @@ -79,6 +79,9 @@ DB_HOST = config.get("database", "host", fallback='') BASE_DIR = Path(__file__).resolve().parent.parent LOCALE_PATHS = [os.path.join(BASE_DIR, 'locale')] +""" GEOCODING """ +GEOCODING_API_URL = config.get("geocoding", "api_url", fallback="https://nominatim.hyteck.de/search") + """ E-MAIL """ console_only = config.getboolean("mail", "console_only", fallback="true") EMAIL_SUBJECT_PREFIX = config.get("mail", "prefix", fallback="[notfellchen]]")