a7566fb6b3
Only compute the regions layer at the required admin level 6 (kreise/kreisfreie Städte)- we're not using other levels yet and this speeds it up notably.
28 lines
919 B
PL/PgSQL
28 lines
919 B
PL/PgSQL
DROP FUNCTION IF EXISTS layer_obs_regions(geometry, int);
|
|
|
|
CREATE OR REPLACE FUNCTION layer_obs_regions(bbox geometry, zoom_level int)
|
|
RETURNS TABLE(
|
|
region_id bigint,
|
|
geometry geometry,
|
|
name text,
|
|
admin_level int,
|
|
overtaking_event_count int
|
|
) AS $$
|
|
|
|
SELECT
|
|
region.relation_id::bigint as region_id,
|
|
ST_SimplifyPreserveTopology(region.geometry, ZRes(zoom_level + 2)) as geometry,
|
|
region.name as name,
|
|
region.admin_level as admin_level,
|
|
count(overtaking_event.id)::int as overtaking_event_count
|
|
FROM region
|
|
LEFT JOIN overtaking_event on ST_Within(ST_Transform(overtaking_event.geometry, 3857), region.geometry)
|
|
WHERE
|
|
zoom_level >= 4 AND
|
|
zoom_level <= 12 AND
|
|
region.admin_level = 6 AND
|
|
ST_Transform(region.geometry, 3857) && bbox
|
|
GROUP BY region.relation_id, region.name, region.geometry, region.admin_level
|
|
|
|
$$ LANGUAGE SQL IMMUTABLE;
|