Skip to content

Commit

Permalink
Merge pull request #886 from sickelap/more_geocode_providers
Browse files Browse the repository at this point in the history
more geocode providers, not used yet
  • Loading branch information
derneuere authored Jun 19, 2023
2 parents 39166fc + 039d87b commit cb7bf0a
Show file tree
Hide file tree
Showing 31 changed files with 9,276 additions and 2 deletions.
Empty file added api/geocode/__init__.py
Empty file.
50 changes: 50 additions & 0 deletions api/geocode/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from constance import config as settings

from .parsers.mapbox import parse as parse_mapbox
from .parsers.nominatim import parse as parse_nominatim
from .parsers.opencage import parse as parse_opencage
from .parsers.photon import parse as parse_photon
from .parsers.tomtom import parse as parse_tomtom

config = {
"mapbox": {
"geocode_args": {"api_key": settings.MAP_API_KEY},
"parser": parse_mapbox,
},
"maptiler": {
"geocode_args": {"api_key": settings.MAP_API_KEY},
"parser": parse_mapbox,
},
"tomtom": {
"geocode_args": {"api_key": settings.MAP_API_KEY},
"parser": parse_tomtom,
},
"photon": {
"geocode_args": {
"domain": "photon.komoot.io",
},
"parser": parse_photon,
},
"nominatim": {
"geocode_args": {"user_agent": "librephotos"},
"parser": parse_nominatim,
},
"opencage": {
"geocode_args": {
"api_key": settings.MAP_API_KEY,
},
"parser": parse_opencage,
},
}


def get_provider_config(provider) -> dict:
if provider not in config:
raise Exception(f"Map provider not found: {provider}.")
return config[provider]["geocode_args"]


def get_provider_parser(provider) -> callable:
if provider not in config:
raise Exception(f"Map provider not found: {provider}.")
return config[provider]["parser"]
19 changes: 19 additions & 0 deletions api/geocode/geocode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import geopy
from constance import config as site_config

from .config import get_provider_config, get_provider_parser


class Geocode:
def __init__(self, provider):
provider_config = get_provider_config(provider)
self._parser = get_provider_parser(provider)
self._geocoder = geopy.get_geocoder_for_service(provider)(**provider_config)

def reverse(self, lat: float, lon: float) -> dict:
location = self._geocoder.reverse(f"{lat},{lon}")
return self._parser(location)


def reverse_geocode(lat: float, lon: float) -> dict:
return Geocode(site_config.MAP_API_PROVIDER).reverse(lat, lon)
Empty file added api/geocode/parsers/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions api/geocode/parsers/mapbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def parse(location):
context = location.raw["context"]
center = location.raw["center"]
local_name = location.raw["text"]
places = [local_name] + [
i["text"] for i in context if not i["id"].startswith("post")
]
return {
"features": [{"text": place} for place in places],
"places": places,
"address": location.address,
"center": [center[1], center[0]],
}
21 changes: 21 additions & 0 deletions api/geocode/parsers/nominatim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def parse(location):
data = location.raw["address"]
props = [
"road",
"town",
"neighbourhood",
"suburb",
"hamlet",
"borough",
"city",
"county",
"state",
"country",
]
places = [data[prop] for prop in props if prop in data]
return {
"features": [{"text": place} for place in places],
"places": places,
"address": location.address,
"center": [float(location.raw["lon"]), float(location.raw["lat"])],
}
22 changes: 22 additions & 0 deletions api/geocode/parsers/opencage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def parse(location):
data = location.raw["components"]
center = location.raw["geometry"]
props = [
data["_type"],
"road",
"suburb",
"municipality",
"hamlet",
"town" "city",
"borough",
"state",
"county",
"country",
]
places = [data[prop] for prop in props if prop in data]
return {
"features": [{"text": place} for place in places],
"places": places,
"address": location.address,
"center": [center["lat"], center["lng"]],
}
17 changes: 17 additions & 0 deletions api/geocode/parsers/photon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def parse(location):
data = location.raw["properties"]
props = [
"street",
"locality",
"district",
"city",
"state",
"country",
]
places = [data[prop] for prop in props if prop in data]
return {
"features": [{"text": place} for place in places],
"places": places,
"address": location.address,
"center": location.raw["geometry"]["coordinates"],
}
38 changes: 38 additions & 0 deletions api/geocode/parsers/tomtom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from functools import reduce


def _dedup(iterable):
unique_items = set()

def reducer(acc, item):
if item not in unique_items:
unique_items.add(item)
acc.append(item)
return acc

return reduce(reducer, iterable, [])


def parse(location):
data = location.raw["address"]
address = location.address
center = list(map(lambda x: float(x), location.raw["position"].split(",")))
props = [
"street",
"streetName",
"municipalitySubdivision",
"countrySubdivision",
"countrySecondarySubdivision",
"municipality",
"municipalitySubdivision",
"country",
]
places = _dedup(
[data[prop] for prop in props if prop in data and len(data[prop]) > 2]
)
return {
"features": [{"text": place} for place in places],
"places": places,
"address": address,
"center": center,
}
1 change: 0 additions & 1 deletion api/models/photo.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,6 @@ def _geolocate_mapbox(self, commit=True):
raise e

self.geolocation_json = res

if "search_text" in res.keys():
if self.search_location:
self.search_location = self.search_location + " " + res["search_text"]
Expand Down
2 changes: 2 additions & 0 deletions api/schemas/site_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
{"required": ["allow_upload"]},
{"required": ["skip_patterns"]},
{"required": ["heavyweight_process"]},
# {"required": ["map_api_provider"]},
{"required": ["map_api_key"]},
],
"properties": {
"allow_registration": {"type": "boolean"},
"allow_upload": {"type": "boolean"},
"skip_patterns": {"type": "string"},
"heavyweight_process": {"type": "number"},
"map_api_provider": {"type": "string"},
"map_api_key": {"type": "string"},
},
}
Empty file added api/tests/fixtures/__init__.py
Empty file.
43 changes: 43 additions & 0 deletions api/tests/fixtures/api_util/expectation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
wordcloud_expectation = {
"captions": [
{"label": "outdoor", "y": 1.9459101490553132},
{"label": "indoor", "y": 0.6931471805599453},
{"label": "ticket booth", "y": 0.0},
{"label": "boardwalk", "y": 0.0},
{"label": "phone booth", "y": 0.0},
{"label": "delicatessen", "y": 0.0},
{"label": "lagoon", "y": 0.0},
{"label": "tundra", "y": 0.0},
{"label": "marsh", "y": 0.0},
{"label": "bakery shop", "y": 0.0},
{"label": "market outdoor", "y": 0.0},
{"label": "butchers shop", "y": 0.0},
{"label": "playground", "y": 0.0},
{"label": "picnic area", "y": 0.0},
],
"people": [],
"locations": [
{"label": "New South Wales", "y": 1.3862943611198906},
{"label": "Sydney", "y": 1.3862943611198906},
{"label": "Australia", "y": 1.3862943611198906},
{"label": "Maroubra", "y": 1.0986122886681098},
{"label": "Ladakh", "y": 0.6931471805599453},
{"label": "Leh", "y": 0.6931471805599453},
{"label": "Berlin", "y": 0.6931471805599453},
{"label": "Germany", "y": 0.6931471805599453},
{"label": "India", "y": 0.6931471805599453},
{"label": "Lakeshore Road", "y": 0.0},
{"label": "Shachokol", "y": 0.0},
{"label": "Kreuzberg", "y": 0.0},
{"label": "Canada", "y": 0.0},
{"label": "Bondi Beach", "y": 0.0},
{"label": "Peterborough County", "y": 0.0},
{"label": "Lakefield", "y": 0.0},
{"label": "Main Bazaar", "y": 0.0},
{"label": "Ontario", "y": 0.0},
{"label": "Chuchat Yakma", "y": 0.0},
{"label": "Friedrichshain", "y": 0.0},
{"label": "Beach Road", "y": 0.0},
{"label": "Fire Route 47", "y": 0.0},
],
}
Loading

0 comments on commit cb7bf0a

Please sign in to comment.