Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexible geo decoding to include regions #326

Merged
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
16 changes: 9 additions & 7 deletions .github/workflows/canopeum_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ on:
# we're just pushing all updates right now and force-updating on portainer at
# /#!/1/docker/stacks/releaf-canopeum?id=11&regular=true
# Once fixed, remove this "on push" trigger
push:
branches:
- main
paths:
- "canopeum_backend/**"
- "canopeum_frontend/**"
- ".github/workflows/canopeum_deploy.yml"

# (disabled until the end of decenmber whilst Beslogic is still actively working on the project)
# push:
# branches:
# - main
# paths:
# - "canopeum_backend/**"
# - "canopeum_frontend/**"
# - ".github/workflows/canopeum_deploy.yml"

jobs:
GetDateTime:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ For backend

```ini
SECRET_KEY_DJANGO_CANOPEUM="not_empty"
MYSQL_PASSWORD_CANOPEUM=CanopeumUser12345!@
MYSQL_PASSWORD_CANOPEUM="CanopeumUser12345!@"
GOOGLE_API_KEY_CANOPEUM=<your Google Geocoding API key, leave empty if none>
```

4.2. Then run:
Expand Down
24 changes: 20 additions & 4 deletions canopeum_backend/canopeum_backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.db import models
from django.http import QueryDict
from django.utils.datastructures import MultiValueDict as django_MultiValueDict
from googlemaps.geocoding import reverse_geocode
from rest_framework.request import Request as drf_Request

from .settings import GOOGLE_API_KEY
Expand Down Expand Up @@ -112,14 +113,29 @@ def from_dms_lat_long(cls, dms_latitude: str, dms_longitude: str):
dd_longitude *= -1

if gmaps is not None:
data_retrieved = gmaps.reverse_geocode( # pyright: ignore[reportAttributeAccessIssue] -- No type stub currently exists
(dd_latitude, dd_longitude), result_type="street_address"
data_retrieved: list[dict[str, Any]] = reverse_geocode(
gmaps,
(dd_latitude, dd_longitude),
# https://developers.google.com/maps/documentation/geocoding/requests-reverse-geocoding
result_type=[
# Gives lots of good civil administrations polygons,
# automatically includes many administrative_area_level
"political",
# Direct address, if possible
"street_address",
],
)
# Naive way to get the location we want, longer name usually means more precise
data_retrieved = sorted(
data_retrieved, key=lambda location: len(location["formatted_address"])
)
formatted_address = (
data_retrieved[0]["formatted_address"] if data_retrieved else "Custom address"
data_retrieved[0]["formatted_address"]
if data_retrieved
else "Unretrievable location"
)
else:
formatted_address = "Unknown address"
formatted_address = "Missing Google API Key"

return cls.objects.create(
dms_latitude=dms_latitude,
Expand Down
Loading