obs-portal/tile-generator/layers/obs_roads/layer.sql
2021-11-22 09:50:33 +01:00

16 lines
635 B
PL/PgSQL

CREATE OR REPLACE FUNCTION layer_obs_roads(bbox geometry, zoom_level int)
RETURNS TABLE(way_id bigint, geometry geometry, distance_overtaker_mean float, direction int) AS $$
SELECT
road.way_id::bigint as way_id,
road.geometry as geometry,
avg(distance_overtaker) as distance_overtaker_mean,
r.dir as direction
FROM road
JOIN overtaking_event on road.way_id = overtaking_event.way_id
JOIN (VALUES (1, TRUE), (-1, FALSE)) AS r(dir, rev) ON overtaking_event.direction_reversed = r.rev
WHERE road.geometry && bbox
GROUP BY road.way_id, road.geometry, direction;
$$ LANGUAGE SQL IMMUTABLE;