Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import Vantaa parking payzones from ArcGIS Server #233

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
1 change: 1 addition & 0 deletions scripts/run_imports.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function stage_1 {
./manage.py geo_import helsinki --addresses
./manage.py geo_import uusimaa --addresses
./manage.py update_vantaa_parking_areas
./manage.py update_vantaa_parking_payzones
./manage.py index_search_columns
}

Expand Down
118 changes: 118 additions & 0 deletions services/management/commands/update_vantaa_parking_payzones.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import logging
from time import time

import restapi
from django.contrib.gis.gdal import CoordTransform, SpatialReference
from django.contrib.gis.geos import GEOSGeometry, MultiPolygon
from django.core.management.base import BaseCommand
from munigeo.models import (
AdministrativeDivision,
AdministrativeDivisionGeometry,
AdministrativeDivisionType,
Municipality,
)
from munigeo.utils import get_default_srid

logger = logging.getLogger("services.management")

OCD_ID_VANTAA_PARKING_PAYZONE_BASE = (
"ocd-division/country:fi/kunta:vantaa/pysakointimaksuvyohyke:"
)

SRC_SERVICE_URL = "https://matti.vantaa.fi/server2/rest/services/Hosted/Maksuvy%C3%B6hykkeet/FeatureServer"
SRC_LAYER_NAME = "Maksuvyöhykkeet MUOKATTAVA"
SRC_SRID = 4326

PAYZONE_TRANSLATIONS = {
"1 € / tunti": {"sv": "1 € / timme", "en": "1 € / hour"},
"2 € / tunti": {"sv": "2 € / timme", "en": "2 € / hour"},
mhieta marked this conversation as resolved.
Show resolved Hide resolved
}


class Command(BaseCommand):
help = "Update Vantaa parking payzones from Vantaa ArcGIS Server."

def handle(self, *args, **options) -> None:
self.update_parking_payzones()

def get_features(self):
service = restapi.FeatureService(SRC_SERVICE_URL)
parking_payzones = service.layer(SRC_LAYER_NAME)
return parking_payzones.query()

def update_parking_payzones(self):
start_time = time()
num_parking_payzones_updated = 0

src_srs = SpatialReference(SRC_SRID)
dest_srs = SpatialReference(get_default_srid())
src_to_dest = CoordTransform(src_srs, dest_srs)

municipality = Municipality.objects.get(name_fi="Vantaa")
division_type = AdministrativeDivisionType.objects.get(type="parking_payzone")

features = self.get_features()

logger.info("Found {} parking payzones for Vantaa".format(features.count))
logger.info("Importing parking payzones...")

updated_parking_payzones = []
for feature in features:
props = feature.get("properties")
name_fi = props.get("maksullisu")
geometry = feature.get("geometry")

if not geometry:
logger.warning(f"Parking payzone {name_fi} has no geometry, skipping")
continue

geom = MultiPolygon(GEOSGeometry(str(geometry), srid=SRC_SRID))
geom.transform(src_to_dest)

origin_id = str(props.get("objectid")) if props.get("objectid") else None
if not origin_id:
logger.warning("Parking payzone has no origin ID, skipping")
continue

defaults = {
"name_fi": name_fi,
"municipality": municipality,
"type": division_type,
"extra": props,
"origin_id": origin_id,
}

try:
name_en = PAYZONE_TRANSLATIONS[name_fi]["en"]
name_sv = PAYZONE_TRANSLATIONS[name_fi]["sv"]
defaults["name_en"] = name_en
defaults["name_sv"] = name_sv
except KeyError:
logger.warning(f"No translation for {name_fi}")

division, _ = AdministrativeDivision.objects.update_or_create(
ocd_id=OCD_ID_VANTAA_PARKING_PAYZONE_BASE + origin_id,
defaults=defaults,
)
AdministrativeDivisionGeometry.objects.update_or_create(
division=division,
defaults={"boundary": geom},
)
logger.debug("Updated parking payzone {}".format(division.name_fi))

updated_parking_payzones.append(division)
num_parking_payzones_updated += 1

# Delete parking payzones that are no longer in the source data
all_parking_payzones = AdministrativeDivision.objects.filter(
type=division_type, municipality=municipality
)
removed_parking_payzones = all_parking_payzones.exclude(
id__in=[zone.id for zone in updated_parking_payzones]
)
num_parking_payzones_deleted = removed_parking_payzones.delete()[0]

logger.info(
f"Import completed. {num_parking_payzones_updated} parking payzones updated and "
f"{num_parking_payzones_deleted} deleted in {time() - start_time:.0f} seconds."
)
178 changes: 178 additions & 0 deletions services/tests/data/vantaa_parking_payzones.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
{
"features": [
{
"geometry": {
"coordinates": [
[
[
25.038330550685124,
60.29497921873359
],
[
25.037180473305117,
60.29487636307983
],
[
25.036827917541686,
60.29429636983492
],
[
25.035635781656623,
60.294509434771044
],
[
25.0346088916754,
60.29254891106162
],
[
25.040769788807683,
60.291589437497
],
[
25.04022566806205,
60.29036550511179
],
[
25.042305325413064,
60.290149463457354
],
[
25.043072950077036,
60.29013333869665
],
[
25.04724510980411,
60.29261738435491
],
[
25.048514262487213,
60.29197399505413
],
[
25.04919074613015,
60.29229259744652
],
[
25.047275666908973,
60.293357603681955
],
[
25.04674651339548,
60.29396094884831
],
[
25.045984538606916,
60.29556359293879
],
[
25.0410192803547,
60.29602971167706
],
[
25.039211949420743,
60.296491443248684
],
[
25.038330550685124,
60.29497921873359
]
]
],
"type": "Polygon"
},
"id": 1,
"properties": {
"maksullisu": "2 € / tunti",
"objectid": 1
},
"type": "Feature"
},
{
"geometry": {
"coordinates": [
[
[
24.850767495413702,
60.319266692115555
],
[
24.846091195840064,
60.317177969192386
],
[
24.83716953508307,
60.313142225053056
],
[
24.83840497595719,
60.31100208304065
],
[
24.841280862993386,
60.311415618492354
],
[
24.841642847963975,
60.31254006355655
],
[
24.84665891940124,
60.31491286135265
],
[
24.851120618528427,
60.31319172563568
],
[
24.852447081822483,
60.3140296975698
],
[
24.856279938251905,
60.31568335693376
],
[
24.859057217250353,
60.31626742568362
],
[
24.858139399790076,
60.317817202575384
],
[
24.85727295941106,
60.318502833518636
],
[
24.85597413934648,
60.31811439861645
],
[
24.85421957257933,
60.31897565427159
],
[
24.853341061488837,
60.31942031741433
],
[
24.850767495413702,
60.319266692115555
]
]
],
"type": "Polygon"
},
"id": 2,
"properties": {
"maksullisu": "1 € / tunti",
"objectid": 2
},
"type": "Feature"
}
],
"properties": {
"exceededTransferLimit": false
},
"type": "FeatureCollection"
}
17 changes: 17 additions & 0 deletions services/tests/data/vantaa_parking_payzones_null_geometry.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"features": [
{
"geometry": null,
"id": 3,
"properties": {
"maksullisu": "3 € / tunti",
"objectid": 3
},
"type": "Feature"
}
],
"properties": {
"exceededTransferLimit": false
},
"type": "FeatureCollection"
}
Loading
Loading