Skip to content

Commit

Permalink
Merge pull request #151 from City-of-Turku/feature/street-maintenance…
Browse files Browse the repository at this point in the history
…-history-improve-data-handling

Feature/street maintenance history improve data handling
  • Loading branch information
juuso-j authored Oct 12, 2022
2 parents 0566472 + 02a562b commit d824c17
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 2 additions & 0 deletions street_maintenance/management/commands/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"Pölynsidonta": PUHTAANAPITO,
"Hiekanpoisto": HIEKANPOISTO,
"Muut työt": MUUT,
"Lisätyö": MUUT,
"Viherhoito": MUUT,
"Kaivot": MUUT,
"Metsätyöt": MUUT,
"Rikkakasvien torjunta": MUUT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from datetime import datetime

from django.contrib.gis.geos import LineString
from django.contrib.gis.geos import LineString, Point
from django.core.management.base import BaseCommand

from street_maintenance.models import DEFAULT_SRID, MaintenanceUnit, MaintenanceWork
Expand All @@ -19,6 +19,7 @@
get_autori_event_types,
get_autori_routes,
get_turku_boundary,
is_nested_coordinates,
)

TURKU_BOUNDARY = get_turku_boundary()
Expand Down Expand Up @@ -49,15 +50,26 @@ def get_and_create_autori_maintenance_works(self, history_size=None):
f"Route contains multiple features. {route['geography']['features']}"
)
coordinates = route["geography"]["features"][0]["geometry"]["coordinates"]
geometry = LineString(coordinates, srid=DEFAULT_SRID)
if is_nested_coordinates(coordinates):
geometry = LineString(coordinates, srid=DEFAULT_SRID)
else:
geometry = Point(coordinates, srid=DEFAULT_SRID)

if not TURKU_BOUNDARY.covers(geometry):
continue

events = []
operations = route["operations"]
for operation in operations:
event_name = event_name_mappings[operation]
events.append(EVENT_MAPPINGS[event_name])
if event_name in EVENT_MAPPINGS:
events.append(EVENT_MAPPINGS[event_name])
else:
logger.warning(f"Found unmapped event: {event_name}")

# If no events found discard the work
if len(events) == 0:
continue
if len(route["geography"]["features"]) > 1:
logger.warning(
f"Route contains multiple features. {route['geography']['features']}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def get_and_create_infraroad_maintenance_works(self, history_size):
events.append(EVENT_MAPPINGS[event])
else:
logger.warning(f"Found unmapped event: {event}")
# If no events found discard the work
if len(events) == 0:
continue
works.append(
MaintenanceWork(
timestamp=timestamp,
Expand Down
7 changes: 6 additions & 1 deletion street_maintenance/management/commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import zoneinfo
from datetime import datetime, timedelta

import numpy as np
import requests
from django.conf import settings
from munigeo.models import AdministrativeDivision, AdministrativeDivisionGeometry
Expand Down Expand Up @@ -96,7 +97,7 @@ def create_autori_maintenance_units(access_token):
)
logger.info(
f"Imported {MaintenanceUnit.objects.filter(provider=MaintenanceUnit.AUTORI).count()}"
+ " Infraroad mainetance Units."
+ " Autori(YIT) mainetance Units."
)


Expand Down Expand Up @@ -145,3 +146,7 @@ def get_autori_access_token():
)
access_token = response.json().get("access_token", None)
return access_token


def is_nested_coordinates(coordinates):
return bool(np.array(coordinates).ndim > 1)

0 comments on commit d824c17

Please sign in to comment.