22-- Function: create_transport_lines_mview
33-- Description:
44-- Creates a materialized view that merges transport lines from:
5- -- - `lines_table ` (ways: e.g. highway, railway),
6- -- - `multilines_table ` (relations: e.g. route relations).
5+ -- - `osm_transport_lines ` (ways: e.g. highway, railway),
6+ -- - `osm_transport_multilines ` (relations: e.g. route relations).
77--
8- -- Each row gets a hash-based `id` and is marked with a `source_type`
9- -- ('way' or 'relation'). Multilingual name columns are added.
8+ -- Each row gets a concatenated `id` (id + osm_id) and is marked with a
9+ -- `source_type` ('way' or 'relation'). Multilingual name columns are added.
10+ -- Supports geometry simplification and filtering by type/class.
1011--
1112-- Parameters:
12- -- lines_table TEXT - Table with way-based transport lines.
13- -- multilines_table TEXT - Table with relation-based transport lines.
14- -- view_name TEXT - Final materialized view name.
13+ -- view_name TEXT - Final materialized view name.
14+ -- simplified_tolerance INTEGER - Tolerance for ST_Simplify (0 = no simplification).
15+ -- types TEXT[] - Array of types to include (use ARRAY['*'] for all).
16+ -- classes TEXT[] - Array of classes to include (use ARRAY['*'] for all).
1517--
1618-- Notes:
17- -- - Input tables must include geometry, osm_id, type, class, name, and transport tags .
19+ -- - Filtering uses OR logic: ( type IN types) OR (class IN classes) .
1820-- - Only valid geometries (LineString) are included from relation sources.
1921-- - View uses GiST index on geometry and unique index on `id`.
2022-- ============================================================================
2123
2224DROP FUNCTION IF EXISTS create_transport_lines_mview;
2325
2426CREATE OR REPLACE FUNCTION create_transport_lines_mview (
25- lines_table TEXT ,
26- multilines_table TEXT ,
27- view_name TEXT
27+ view_name TEXT ,
28+ simplified_tolerance INTEGER ,
29+ types TEXT [],
30+ classes TEXT []
2831)
2932RETURNS void AS $$
3033DECLARE
3134 lang_columns TEXT := get_language_columns();
3235 tmp_view_name TEXT := view_name || ' _tmp' ;
3336 unique_columns TEXT := ' id' ;
37+ type_filter TEXT ;
38+ class_filter TEXT ;
3439 sql_create TEXT ;
3540BEGIN
41+ -- Build type filter: '*' means all types
42+ IF types @> ARRAY[' *' ] THEN
43+ type_filter := ' 1=1' ;
44+ ELSE
45+ type_filter := format(' type = ANY(%L)' , types);
46+ END IF;
47+
48+ -- Build class filter: '*' means all classes
49+ IF classes @> ARRAY[' *' ] THEN
50+ class_filter := ' 1=1' ;
51+ ELSE
52+ class_filter := format(' class = ANY(%L)' , classes);
53+ END IF;
54+
3655 sql_create := format($sql$
3756 CREATE MATERIALIZED VIEW %I AS
3857 WITH combined AS (
3958 SELECT
40- md5(
41- COALESCE(CAST(osm_id AS TEXT ), ' ' ) || ' _' ||
42- COALESCE(type, ' ' ) || ' _' ||
43- COALESCE(class, ' ' )
44- ) AS id,
59+ (COALESCE(CAST(id AS TEXT ), ' ' ) || ' _' || COALESCE(CAST(osm_id AS TEXT ), ' ' )) AS id,
4560 ABS(osm_id) AS osm_id,
46- geometry,
61+ CASE
62+ WHEN %s > 0 THEN ST_Simplify(geometry, %s)
63+ ELSE geometry
64+ END AS geometry,
4765 -- Detect highways in construcion https://github.com/OpenHistoricalMap/issues/issues/1151
4866 CASE
4967 WHEN highway = ' construction' THEN
@@ -77,20 +95,20 @@ BEGIN
7795 NULL AS member,
7896 ' way' AS source_type,
7997 %s
80- FROM %I
98+ FROM osm_transport_lines
8199 WHERE geometry IS NOT NULL
100+ AND ( %s
101+ OR %s)
82102
83103 UNION ALL
84104
85105 SELECT
86- md5(
87- COALESCE(CAST(osm_id AS TEXT ), ' ' ) || ' _' ||
88- COALESCE(CAST(member AS TEXT ), ' ' ) || ' _' ||
89- COALESCE(type, ' ' ) || ' _' ||
90- COALESCE(class, ' ' )
91- ) AS id,
106+ (COALESCE(CAST(id AS TEXT ), ' ' ) || ' _' || COALESCE(CAST(osm_id AS TEXT ), ' ' )) AS id,
92107 ABS(osm_id) AS osm_id,
93- geometry,
108+ CASE
109+ WHEN %s > 0 THEN ST_Simplify(geometry, %s)
110+ ELSE geometry
111+ END AS geometry,
94112 CASE
95113 WHEN highway = ' construction' THEN
96114 -- If the 'construction' tag has a value, append '_construction'. Otherwise, use 'road_construction'.
@@ -123,12 +141,16 @@ BEGIN
123141 member,
124142 ' relation' AS source_type,
125143 %s
126- FROM %I
144+ FROM osm_transport_multilines
127145 WHERE ST_GeometryType(geometry) = ' ST_LineString' AND geometry IS NOT NULL
146+ AND (%s
147+ OR %s)
128148 )
129149 SELECT DISTINCT ON (id) *
130150 FROM combined;
131- $sql$, tmp_view_name, lang_columns, lines_table, lang_columns, multilines_table);
151+ $sql$, tmp_view_name,
152+ simplified_tolerance, simplified_tolerance, lang_columns, type_filter, class_filter,
153+ simplified_tolerance, simplified_tolerance, lang_columns, type_filter, class_filter);
132154
133155 PERFORM finalize_materialized_view(
134156 tmp_view_name,
@@ -142,11 +164,11 @@ $$ LANGUAGE plpgsql;
142164-- ============================================================================
143165-- Create materialized views for transport lines
144166-- ============================================================================
145- SELECT create_transport_lines_mview(' osm_transport_lines_z5 ' , ' osm_transport_multilines_z5 ' , ' mv_transport_lines_z5 ' );
146- SELECT create_transport_lines_mview(' osm_transport_lines_z6 ' , ' osm_transport_multilines_z6 ' , ' mv_transport_lines_z6 ' );
147- SELECT create_transport_lines_mview(' osm_transport_lines_z7 ' , ' osm_transport_multilines_z7 ' , ' mv_transport_lines_z7 ' );
148- SELECT create_transport_lines_mview(' osm_transport_lines_z8 ' , ' osm_transport_multilines_z8 ' , ' mv_transport_lines_z8 ' );
149- SELECT create_transport_lines_mview(' osm_transport_lines_z9 ' , ' osm_transport_multilines_z9 ' , ' mv_transport_lines_z9 ' );
150- SELECT create_transport_lines_mview(' osm_transport_lines_z10_11 ' , ' osm_transport_multilines_z10_11 ' , ' mv_transport_lines_z10_11 ' );
151- SELECT create_transport_lines_mview(' osm_transport_lines_z12_13 ' , ' osm_transport_multilines_z12_13 ' , ' mv_transport_lines_z12_13 ' );
152- SELECT create_transport_lines_mview(' osm_transport_lines ' , ' osm_transport_multilines ' , ' mv_transport_lines_z14_20 ' );
167+ SELECT create_transport_lines_mview(' mv_transport_lines_z5 ' , 2000 , ARRAY[ ' motorway ' , ' motorway_link ' , ' trunk ' , ' trunk_link ' , ' construction ' , ' primary ' , ' primary_link ' , ' rail ' ], ARRAY[ ' railway ' ] );
168+ SELECT create_transport_lines_mview(' mv_transport_lines_z6 ' , 1500 , ARRAY[ ' motorway ' , ' motorway_link ' , ' trunk ' , ' trunk_link ' , ' construction ' , ' primary ' , ' primary_link ' , ' rail ' ], ARRAY[ ' railway ' ] );
169+ SELECT create_transport_lines_mview(' mv_transport_lines_z7 ' , 1000 , ARRAY[ ' motorway ' , ' motorway_link ' , ' trunk ' , ' trunk_link ' , ' construction ' , ' primary ' , ' primary_link ' , ' rail ' ], ARRAY[ ' railway ' ] );
170+ SELECT create_transport_lines_mview(' mv_transport_lines_z8 ' , 500 , ARRAY[ ' motorway ' , ' motorway_link ' , ' trunk ' , ' trunk_link ' , ' construction ' , ' primary ' , ' primary_link ' , ' rail ' , ' secondary ' , ' secondary_link ' ], ARRAY[ ' railway ' ] );
171+ SELECT create_transport_lines_mview(' mv_transport_lines_z9 ' , 200 , ARRAY[ ' motorway ' , ' motorway_link ' , ' trunk ' , ' trunk_link ' , ' construction ' , ' primary ' , ' primary_link ' , ' rail ' , ' secondary ' , ' secondary_link ' , ' tertiary ' , ' tertiary_link ' ], ARRAY[ ' railway ' ] );
172+ SELECT create_transport_lines_mview(' mv_transport_lines_z10_11 ' , 100 , ARRAY[ ' motorway ' , ' motorway_link ' , ' trunk ' , ' trunk_link ' , ' construction ' , ' primary ' , ' primary_link ' , ' rail ' , ' secondary ' , ' secondary_link ' , ' tertiary ' , ' tertiary_link ' , ' taxiway ' , ' runway ' ], ARRAY[ ' railway ' ] );
173+ SELECT create_transport_lines_mview(' mv_transport_lines_z12_13 ' , 50 , ARRAY[ ' motorway ' , ' motorway_link ' , ' trunk ' , ' trunk_link ' , ' construction ' , ' primary ' , ' primary_link ' , ' rail ' , ' secondary ' , ' secondary_link ' , ' tertiary ' , ' tertiary_link ' , ' miniature ' , ' narrow_gauge ' , ' dismantled ' , ' abandoned ' , ' disused ' , ' razed ' , ' light_rail ' , ' preserved ' , ' proposed ' , ' tram ' , ' funicular ' , ' monorail ' , ' taxiway ' , ' runway ' , ' raceway ' , ' residential ' , ' service ' , ' unclassified ' ], ARRAY[ ' railway ' ] );
174+ SELECT create_transport_lines_mview(' mv_transport_lines_z14_20 ' , 0 , ARRAY[ ' * ' ], ARRAY[ ' railway ' , ' route ' ] );
0 commit comments