fix: rework geocoding api usage, make api url custom
This commit is contained in:
parent
2a9dc337d2
commit
677fc6626b
@ -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",
|
||||
),
|
||||
]
|
@ -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)
|
||||
|
@ -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")))
|
||||
|
@ -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]]")
|
||||
|
Loading…
Reference in New Issue
Block a user