2022-03-16 21:20:15 +00:00
|
|
|
DROP FUNCTION IF EXISTS layer_obs_roads(geometry, int);
|
|
|
|
|
2021-10-10 10:32:28 +00:00
|
|
|
CREATE OR REPLACE FUNCTION layer_obs_roads(bbox geometry, zoom_level int)
|
2021-10-31 19:50:09 +00:00
|
|
|
RETURNS TABLE(
|
|
|
|
way_id bigint,
|
|
|
|
geometry geometry,
|
|
|
|
distance_overtaker_mean float,
|
|
|
|
distance_overtaker_min float,
|
|
|
|
distance_overtaker_max float,
|
|
|
|
distance_overtaker_median float,
|
|
|
|
distance_overtaker_array float[],
|
|
|
|
overtaking_event_count int,
|
2022-03-16 21:05:58 +00:00
|
|
|
usage_count bigint,
|
2021-10-31 19:50:09 +00:00
|
|
|
direction int,
|
|
|
|
offset_direction int
|
|
|
|
) AS $$
|
2021-10-10 10:32:28 +00:00
|
|
|
|
|
|
|
SELECT
|
|
|
|
road.way_id::bigint as way_id,
|
|
|
|
road.geometry as geometry,
|
|
|
|
avg(distance_overtaker) as distance_overtaker_mean,
|
2021-10-31 19:50:09 +00:00
|
|
|
min(distance_overtaker) as distance_overtaker_min,
|
|
|
|
max(distance_overtaker) as distance_overtaker_max,
|
|
|
|
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY distance_overtaker) as distance_overtaker_median,
|
|
|
|
array_agg(distance_overtaker) as distance_overtaker_array,
|
|
|
|
count(overtaking_event.id)::int as distance_overtaker_count,
|
2022-03-16 21:20:15 +00:00
|
|
|
(select count(id) from road_usage where road_usage.way_id = road.way_id and
|
|
|
|
(road.directionality != 0 or road_usage.direction_reversed = r.rev)) as usage_count,
|
2021-10-31 19:50:09 +00:00
|
|
|
r.dir as direction,
|
|
|
|
case when road.directionality = 0 then r.dir else 0 end as offset_direction
|
2021-10-10 10:32:28 +00:00
|
|
|
FROM road
|
2021-12-19 20:21:45 +00:00
|
|
|
LEFT JOIN (VALUES (-1, TRUE), (1, FALSE), (0, FALSE)) AS r(dir, rev) ON (abs(r.dir) != road.directionality)
|
2021-10-31 20:33:58 +00:00
|
|
|
FULL OUTER JOIN overtaking_event ON (road.way_id = overtaking_event.way_id and (road.directionality != 0 or overtaking_event.direction_reversed = r.rev))
|
|
|
|
-- WHERE road.name = 'Merzhauser Straße'
|
2021-10-10 10:32:28 +00:00
|
|
|
WHERE road.geometry && bbox
|
2022-03-16 21:20:15 +00:00
|
|
|
GROUP BY road.name, road.way_id, road.geometry, road.directionality, r.dir, r.rev;
|
2021-10-10 10:32:28 +00:00
|
|
|
|
|
|
|
$$ LANGUAGE SQL IMMUTABLE;
|