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
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ celery-slack==0.3.0

urllib3<1.25,>=1.21.1 #requests 2.21.0

django-cors-headers==3.0.2
django-cors-headers==3.0.2

geopy==2.1.0
16 changes: 11 additions & 5 deletions sensorsafrica/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,16 @@ def meta_data(request):

database_size = get_database_size()
database_last_updated = get_database_last_updated()
sensors_locations = get_sensors_locations()
sensors_countries = get_sensors_countries()
sensors_cities = get_sensors_cities()

return Response({
"sensor_networks": get_sensors_networks(),
"nodes_count": nodes_count,
"sensors_count": sensors_count,
"sensor_data_count": sensor_data_count,
"sensors_locations": sensors_locations,
"sensors_countries": sensors_countries,
"sensors_cities": sensors_cities,
"database_size": database_size[0],
"database_last_updated": database_last_updated,
})
Expand All @@ -439,9 +441,13 @@ def get_sensors_networks():
networks.append("sensors.AFRICA")
return {"networks": networks, "count": len(networks)}

def get_sensors_locations():
sensor_locations = SensorLocation.objects.filter(country__isnull=False).values_list('country', flat=True)
return sorted(set(sensor_locations))
def get_sensors_countries():
sensors_countries = SensorLocation.objects.filter(country__isnull=False).values_list('country', flat=True)
return sorted(set(sensors_countries))

def get_sensors_cities():
sensor_cities = Node.objects.filter(location__city__isnull=False).values_list('location__city', flat=True)
return sorted(set(sensor_cities))

def get_database_size():
with connection.cursor() as c:
Expand Down
24 changes: 24 additions & 0 deletions sensorsafrica/management/commands/add_city_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.core.management import BaseCommand
from django.db.models import Q

from geopy.geocoders import Nominatim

from feinstaub.sensors.models import Node


class Command(BaseCommand):
help = "Adds city names to SensorLocation by geo reversing the SensorLocation longitude and latitude."

def handle(self, *args, **options):
geolocator = Nominatim(user_agent="sensors-api")
all_nodes = Node.objects.filter(Q(location__city=None) | Q(location__city=''))

for node in all_nodes:
try:
location = geolocator.reverse(f"{node.location.latitude}, {node.location.longitude}")
except Exception:
# Nodes with location like Soul Buoy raises exceptions
continue
city = location.raw['address'].get('city')
node.location.city = city
node.save()