diff --git a/requirements.txt b/requirements.txt index fdca0c5..7ad0f1b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file +django-cors-headers==3.0.2 + +geopy==2.1.0 diff --git a/sensorsafrica/api/v2/views.py b/sensorsafrica/api/v2/views.py index b334231..cccb1b6 100644 --- a/sensorsafrica/api/v2/views.py +++ b/sensorsafrica/api/v2/views.py @@ -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, }) @@ -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: diff --git a/sensorsafrica/management/commands/add_city_names.py b/sensorsafrica/management/commands/add_city_names.py new file mode 100644 index 0000000..3ceaf2e --- /dev/null +++ b/sensorsafrica/management/commands/add_city_names.py @@ -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()