|
23 | 23 | from apps.webcam.models import Webcam |
24 | 24 | from apps.webcam.serializers import WebcamSerializer |
25 | 25 | from django.conf import settings |
| 26 | +from django.contrib.gis.db.models.functions import Distance |
26 | 27 | from django.contrib.gis.geos import LineString, MultiLineString, Point |
27 | 28 | from django.core.exceptions import ObjectDoesNotExist |
28 | 29 | from django.db.models import F |
@@ -341,10 +342,40 @@ def build_route_geometries(coords=hwy_coords): |
341 | 342 | RouteGeometry.objects.filter(id=key).update(routes=MultiLineString(ls_routes)) |
342 | 343 |
|
343 | 344 |
|
344 | | -def update_camera_area_relations(): |
| 345 | +# DBC22-4679 find and update the nearest weather stations for a camera |
| 346 | +def update_camera_weather_station(camera, weather_class): |
| 347 | + meters_in_degrees = 0.000008983152841195 # 1 meter in degrees at the equator |
| 348 | + |
| 349 | + stations_qs = weather_class.objects.filter( |
| 350 | + location__dwithin=(camera.location, 30000 * meters_in_degrees), # 30km in degrees |
| 351 | + elevation__lte=camera.elevation + 300, |
| 352 | + elevation__gte=camera.elevation - 300, # within 300m elevation, |
| 353 | + ) if isinstance(weather_class, CurrentWeather) else weather_class.objects.all() |
| 354 | + |
| 355 | + closest_station = stations_qs.annotate( |
| 356 | + distance=Distance('location', camera.location) |
| 357 | + ).order_by('distance').first() |
| 358 | + |
| 359 | + if closest_station: |
| 360 | + if weather_class == CurrentWeather: |
| 361 | + Webcam.objects.filter(id=camera.id).update(local_weather_station=closest_station) |
| 362 | + |
| 363 | + elif weather_class == RegionalWeather: |
| 364 | + Webcam.objects.filter(id=camera.id).update(regional_weather_station=closest_station) |
| 365 | + |
| 366 | + elif weather_class == HighElevationForecast: |
| 367 | + Webcam.objects.filter(id=camera.id).update(hev_station=closest_station) |
| 368 | + |
| 369 | + |
| 370 | +def update_camera_relations(): |
345 | 371 | for area in Area.objects.all(): |
346 | 372 | Webcam.objects.filter(location__within=area.geometry).update(area=area) |
347 | 373 |
|
| 374 | + for camera in Webcam.objects.all(): |
| 375 | + update_camera_weather_station(camera, RegionalWeather) |
| 376 | + update_camera_weather_station(camera, CurrentWeather) |
| 377 | + update_camera_weather_station(camera, HighElevationForecast) |
| 378 | + |
348 | 379 |
|
349 | 380 | def update_camera_group_id(camera): |
350 | 381 | try: |
|
0 commit comments