Skip to content

Commit 2a6a201

Browse files
authored
Ft sensors cities (#105)
* Adds a management command to reverse geo code cities for nodes with no cities * Return cities in meta api endpoint * Update response to use sensors_countries * Remove un used code
1 parent 6932fae commit 2a6a201

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ celery-slack==0.3.0
2020

2121
urllib3<1.25,>=1.21.1 #requests 2.21.0
2222

23-
django-cors-headers==3.0.2
23+
django-cors-headers==3.0.2
24+
25+
geopy==2.1.0

sensorsafrica/api/v2/views.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ def meta_data(request):
420420

421421
database_size = get_database_size()
422422
database_last_updated = get_database_last_updated()
423-
sensors_locations = get_sensors_locations()
423+
sensors_countries = get_sensors_countries()
424+
sensors_cities = get_sensors_cities()
424425

425426
return Response({
426427
"sensor_networks": get_sensors_networks(),
@@ -430,7 +431,8 @@ def meta_data(request):
430431
},
431432
"sensors_count": sensors_count,
432433
"sensor_data_count": sensor_data_count,
433-
"sensors_locations": sensors_locations,
434+
"sensors_countries": sensors_countries,
435+
"sensors_cities": sensors_cities,
434436
"database_size": database_size[0],
435437
"database_last_updated": database_last_updated,
436438
})
@@ -446,9 +448,13 @@ def get_sensors_networks():
446448
networks.append("sensors.AFRICA")
447449
return {"networks": networks, "count": len(networks)}
448450

449-
def get_sensors_locations():
450-
sensor_locations = SensorLocation.objects.filter(country__isnull=False).values_list('country', flat=True)
451-
return sorted(set(sensor_locations))
451+
def get_sensors_countries():
452+
sensors_countries = SensorLocation.objects.filter(country__isnull=False).values_list('country', flat=True)
453+
return sorted(set(sensors_countries))
454+
455+
def get_sensors_cities():
456+
sensor_cities = Node.objects.filter(location__city__isnull=False).values_list('location__city', flat=True)
457+
return sorted(set(sensor_cities))
452458

453459
def get_database_size():
454460
with connection.cursor() as c:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from django.core.management import BaseCommand
2+
from django.db.models import Q
3+
4+
from geopy.geocoders import Nominatim
5+
6+
from feinstaub.sensors.models import Node
7+
8+
9+
class Command(BaseCommand):
10+
help = "Adds city names to SensorLocation by geo reversing the SensorLocation longitude and latitude."
11+
12+
def handle(self, *args, **options):
13+
geolocator = Nominatim(user_agent="sensors-api")
14+
all_nodes = Node.objects.filter(Q(location__city=None) | Q(location__city=''))
15+
16+
for node in all_nodes:
17+
try:
18+
location = geolocator.reverse(f"{node.location.latitude}, {node.location.longitude}")
19+
except Exception:
20+
# Nodes with location like Soul Buoy raises exceptions
21+
continue
22+
city = location.raw['address'].get('city')
23+
node.location.city = city
24+
node.save()

0 commit comments

Comments
 (0)