From da82303042b32dfe8ca49dc593ecdf527664a278 Mon Sep 17 00:00:00 2001 From: gluap Date: Mon, 29 May 2023 13:20:34 +0200 Subject: [PATCH] implement reviewer comments - rename road_usage -> segments (as we're actually dealing with segments not with roads) - use "select as" to ensure defined behaviour - cleanup --- api/obs/api/routes/exports.py | 55 ++++++++++++------------- frontend/src/pages/ExportPage/index.tsx | 2 +- frontend/src/translations/de.yaml | 2 +- frontend/src/translations/en.yaml | 2 +- frontend/src/translations/fr.yaml | 2 +- 5 files changed, 31 insertions(+), 32 deletions(-) diff --git a/api/obs/api/routes/exports.py b/api/obs/api/routes/exports.py index 3dcf2c0..4fa1ce3 100644 --- a/api/obs/api/routes/exports.py +++ b/api/obs/api/routes/exports.py @@ -122,22 +122,22 @@ async def export_events(req): raise InvalidUsage("unknown export format") -@api.get(r"/export/road_usage") -async def export_road_usage(req): +@api.get(r"/export/segments") +async def export_segments(req): async with use_request_semaphore(req, "export_semaphore", timeout=30): bbox = req.ctx.get_single_arg( "bbox", default="-180,-90,180,90" ) assert re.match(r"(-?\d+\.?\d+,?){4}", bbox) fmt = req.ctx.get_single_arg("fmt", convert=ExportFormat) - events = await req.ctx.db.stream( + segments = await req.ctx.db.stream( text( - f"select ST_AsGeoJSON(ST_Transform(geometry,4326)), way_id, distance_overtaker_mean, distance_overtaker_min,distance_overtaker_max,distance_overtaker_median,overtaking_event_count,usage_count,direction,zone,offset_direction,distance_overtaker_array from layer_obs_roads(ST_Transform(ST_MakeEnvelope({bbox},4326),3857),11,NULL,'1900-01-01'::timestamp,'2100-01-01'::timestamp) WHERE usage_count>0" + f"select ST_AsGeoJSON(ST_Transform(geometry,4326)) AS geometry, way_id, distance_overtaker_mean, distance_overtaker_min,distance_overtaker_max,distance_overtaker_median,overtaking_event_count,usage_count,direction,zone,offset_direction,distance_overtaker_array from layer_obs_roads(ST_Transform(ST_MakeEnvelope({bbox},4326),3857),11,NULL,'1900-01-01'::timestamp,'2100-01-01'::timestamp) WHERE usage_count>0" ) ) if fmt == ExportFormat.SHAPEFILE: - with shapefile_zip(shape_type=3, basename="road_usage") as (writer, zip_buffer): + with shapefile_zip(shape_type=3, basename="segments") as (writer, zip_buffer): writer.field("distance_overtaker_mean", "N", decimal=4) writer.field("distance_overtaker_max", "N", decimal=4) writer.field("distance_overtaker_min", "N", decimal=4) @@ -148,41 +148,40 @@ async def export_road_usage(req): writer.field("direction", "N", decimal=0) writer.field("zone", "C") - async for road in events: - geom = json.loads(road.st_asgeojson) + async for segment in segments: + geom = json.loads(segment.st_asgeojson) writer.line([geom["coordinates"]]) writer.record( - distance_overtaker_mean=road.distance_overtaker_mean, - distance_overtaker_median=road.distance_overtaker_median, - distance_overtaker_max=road.distance_overtaker_max, - distance_overtaker_min=road.distance_overtaker_min, - usage_count=road.usage_count, - overtaking_event_count=road.overtaking_event_count, - direction=road.direction, - way_id=road.way_id, - zone=road.zone, - # "time"=event.time, + distance_overtaker_mean=segment.distance_overtaker_mean, + distance_overtaker_median=segment.distance_overtaker_median, + distance_overtaker_max=segment.distance_overtaker_max, + distance_overtaker_min=segment.distance_overtaker_min, + usage_count=segment.usage_count, + overtaking_event_count=segment.overtaking_event_count, + direction=segment.direction, + way_id=segment.way_id, + zone=segment.zone, ) return raw(zip_buffer.getbuffer()) if fmt == ExportFormat.GEOJSON: features = [] - async for road in events: + async for segment in segments: features.append( { "type": "Feature", - "geometry": json.loads(road.st_asgeojson), + "geometry": json.loads(segment.geometry), "properties": { - "distance_overtaker_mean": road.distance_overtaker_mean, - "distance_overtaker_max": road.distance_overtaker_max, - "distance_overtaker_median": road.distance_overtaker_median, - "overtaking_event_count": road.overtaking_event_count, - "usage_count": road.usage_count, - "distance_overtaker_array": road.distance_overtaker_array, - "direction": road.direction, - "way_id": road.way_id, - "zone": road.zone, + "distance_overtaker_mean": segment.distance_overtaker_mean, + "distance_overtaker_max": segment.distance_overtaker_max, + "distance_overtaker_median": segment.distance_overtaker_median, + "overtaking_event_count": segment.overtaking_event_count, + "usage_count": segment.usage_count, + "distance_overtaker_array": segment.distance_overtaker_array, + "direction": segment.direction, + "way_id": segment.way_id, + "zone": segment.zone, }, } ) diff --git a/frontend/src/pages/ExportPage/index.tsx b/frontend/src/pages/ExportPage/index.tsx index 787c3cd..65cdac5 100644 --- a/frontend/src/pages/ExportPage/index.tsx +++ b/frontend/src/pages/ExportPage/index.tsx @@ -104,7 +104,7 @@ const BoundingBoxSelector = React.forwardRef( } ); -const MODES = ["events", "road_usage"]; +const MODES = ["events", "segments"]; const FORMATS = ["geojson", "shapefile"]; export default function ExportPage() { diff --git a/frontend/src/translations/de.yaml b/frontend/src/translations/de.yaml index 94f4288..62e214c 100644 --- a/frontend/src/translations/de.yaml +++ b/frontend/src/translations/de.yaml @@ -95,7 +95,7 @@ ExportPage: label: Modus placeholder: Modus wählen events: Überholvorgänge - road_usage: Straßennutzung + segments: Straßenstatistiken format: label: Format placeholder: Format wählen diff --git a/frontend/src/translations/en.yaml b/frontend/src/translations/en.yaml index 25a063d..00513a8 100644 --- a/frontend/src/translations/en.yaml +++ b/frontend/src/translations/en.yaml @@ -101,7 +101,7 @@ ExportPage: label: Mode placeholder: Select mode events: Events - road_usage: Road usage + segments: Road statistics format: label: Format placeholder: Select format diff --git a/frontend/src/translations/fr.yaml b/frontend/src/translations/fr.yaml index beed397..28e9fe1 100644 --- a/frontend/src/translations/fr.yaml +++ b/frontend/src/translations/fr.yaml @@ -102,7 +102,7 @@ ExportPage: label: Mode placeholder: Sélectionner un mode events: Evénements - road_usage: Utilisation des rues + segments: Statistiques des rues format: label: Format placeholder: Sélectionner un format