22-- Function: create_admin_boundaries_centroids_mview
33-- Description:
44-- Creates a materialized view of admin boundary centroids using
5- -- ST_MaximumInscribedCircle from polygons in the input table.
5+ -- ST_MaximumInscribedCircle from polygons in the input materialized view.
6+ -- Extracts all columns dynamically from the source materialized view and
7+ -- converts the geometry to a centroid point.
68--
79-- Parameters:
8- -- input_table TEXT - Source table name (e.g., osm_admin_areas ).
10+ -- source_mview TEXT - Source materialized view name (e.g., mv_admin_boundaries_areas_z16_20 ).
911-- mview_name TEXT - Name of the final materialized view to create.
1012-- unique_columns TEXT - Comma-separated list of columns for uniqueness
1113-- (default: 'id, osm_id, type').
1416--
1517-- Notes:
1618-- - Excludes boundaries with role='label' from centroid calculation.
17- -- - Area is stored in square kilometers as integer.
19+ -- - Extracts all columns dynamically from the source materialized view.
20+ -- - Converts geometry to centroid using ST_MaximumInscribedCircle.
1821-- - Geometry is indexed using GiST.
1922-- - Uniqueness is enforced on the specified unique_columns.
20- -- - Includes multilingual name columns via get_language_columns().
2123-- - Uses finalize_materialized_view() for atomic creation and renaming.
2224-- ============================================================================
2325
2426DROP FUNCTION IF EXISTS create_admin_boundaries_centroids_mview;
2527CREATE OR REPLACE FUNCTION create_admin_boundaries_centroids_mview (
26- input_table TEXT ,
28+ source_mview TEXT ,
2729 mview_name TEXT ,
2830 unique_columns TEXT DEFAULT ' id, osm_id, type' ,
2931 where_filter TEXT DEFAULT NULL
@@ -32,7 +34,7 @@ RETURNS void AS $$
3234DECLARE
3335 tmp_mview_name TEXT := mview_name || ' _tmp' ;
3436 sql_create TEXT ;
35- lang_columns TEXT : = get_language_columns() ;
37+ all_cols TEXT ;
3638 custom_filter TEXT ;
3739BEGIN
3840 -- Build custom WHERE filter (if provided)
@@ -43,27 +45,37 @@ BEGIN
4345 custom_filter := ' ' ;
4446 END IF;
4547
48+ -- Get all columns from the source materialized view, replacing geometry with centroid
49+ SELECT COALESCE(string_agg(
50+ CASE
51+ WHEN a .attname = ' geometry' THEN ' (ST_MaximumInscribedCircle(geometry)).center AS geometry'
52+ ELSE quote_ident(a .attname )
53+ END,
54+ ' , ' ORDER BY a .attnum
55+ ), ' ' )
56+ INTO all_cols
57+ FROM pg_attribute a
58+ JOIN pg_class c ON a .attrelid = c .oid
59+ JOIN pg_namespace n ON c .relnamespace = n .oid
60+ WHERE n .nspname = ' public'
61+ AND c .relname = source_mview
62+ AND a .attnum > 0
63+ AND NOT a .attisdropped ;
64+
65+ IF all_cols IS NULL THEN
66+ RAISE EXCEPTION ' No columns found for %. Make sure the materialized view exists.' , source_mview;
67+ END IF;
68+
4669 sql_create := format($sql$
4770 CREATE MATERIALIZED VIEW %I AS
4871 SELECT
49- ABS(osm_id) AS id,
50- osm_id,
51- NULLIF(name, ' ' ) AS name,
52- admin_level,
53- NULLIF(type, ' ' ) AS type,
54- (ST_MaximumInscribedCircle(geometry)).center AS geometry,
55- NULLIF(start_date, ' ' ) AS start_date,
56- NULLIF(end_date, ' ' ) AS end_date,
57- isodatetodecimaldate(pad_date(start_date, ' start' ), FALSE) AS start_decdate,
58- isodatetodecimaldate(pad_date(end_date, ' end' ), FALSE) AS end_decdate,
59- ROUND(CAST(area AS numeric ) / 1000000 )::integer AS area_km2,
6072 %s
6173 FROM %I
6274 WHERE name IS NOT NULL AND name <> ' '
6375 AND osm_id NOT IN (
6476 SELECT osm_id FROM osm_relation_members WHERE role = ' label'
6577 )%s;
66- $sql$, tmp_mview_name, lang_columns, input_table , custom_filter);
78+ $sql$, tmp_mview_name, all_cols, source_mview , custom_filter);
6779
6880 -- Finalize the materialized view and its indexes
6981 PERFORM finalize_materialized_view(
@@ -78,13 +90,15 @@ $$ LANGUAGE plpgsql;
7890-- ============================================================================
7991-- Execute force creation of all admin boundaries centroids materialized views
8092-- ============================================================================
81- SELECT create_admin_boundaries_centroids_mview(' osm_admin_areas' , ' mv_admin_boundaries_centroids_z0_2' , ' id, osm_id, type' , ' admin_level IN (1,2)' );
82- SELECT create_admin_boundaries_centroids_mview(' osm_admin_areas' , ' mv_admin_boundaries_centroids_z3_5' , ' id, osm_id, type' , ' admin_level IN (1,2,3,4)' );
83- SELECT create_admin_boundaries_centroids_mview(' osm_admin_areas' , ' mv_admin_boundaries_centroids_z6_7' , ' id, osm_id, type' , ' admin_level IN (1,2,3,4,5,6)' );
84- SELECT create_admin_boundaries_centroids_mview(' osm_admin_areas' , ' mv_admin_boundaries_centroids_z8_9' , ' id, osm_id, type' , ' admin_level IN (1,2,3,4,5,6,7,8,9)' );
85- SELECT create_admin_boundaries_centroids_mview(' osm_admin_areas' , ' mv_admin_boundaries_centroids_z10_12' , ' id, osm_id, type' , ' admin_level IN (1,2,3,4,5,6,7,8,9,10)' );
86- SELECT create_admin_boundaries_centroids_mview(' osm_admin_areas' , ' mv_admin_boundaries_centroids_z13_15' , ' id, osm_id, type' , ' admin_level IN (1,2,3,4,5,6,7,8,9,10,11)' );
87- SELECT create_admin_boundaries_centroids_mview(' osm_admin_areas' , ' mv_admin_boundaries_centroids_z16_20' , ' id, osm_id, type' , ' admin_level IN (1,2,3,4,5,6,7,8,9,10,11)' );
93+ -- Create centroids from corresponding area materialized views
94+ -- The where_filter is no longer needed as the area views already have the appropriate filters
95+ SELECT create_admin_boundaries_centroids_mview(' mv_admin_boundaries_areas_z0_2' , ' mv_admin_boundaries_centroids_z0_2' , ' id, osm_id, type' , NULL );
96+ SELECT create_admin_boundaries_centroids_mview(' mv_admin_boundaries_areas_z3_5' , ' mv_admin_boundaries_centroids_z3_5' , ' id, osm_id, type' , NULL );
97+ SELECT create_admin_boundaries_centroids_mview(' mv_admin_boundaries_areas_z6_7' , ' mv_admin_boundaries_centroids_z6_7' , ' id, osm_id, type' , NULL );
98+ SELECT create_admin_boundaries_centroids_mview(' mv_admin_boundaries_areas_z8_9' , ' mv_admin_boundaries_centroids_z8_9' , ' id, osm_id, type' , NULL );
99+ SELECT create_admin_boundaries_centroids_mview(' mv_admin_boundaries_areas_z10_12' , ' mv_admin_boundaries_centroids_z10_12' , ' id, osm_id, type' , NULL );
100+ SELECT create_admin_boundaries_centroids_mview(' mv_admin_boundaries_areas_z13_15' , ' mv_admin_boundaries_centroids_z13_15' , ' id, osm_id, type' , NULL );
101+ SELECT create_admin_boundaries_centroids_mview(' mv_admin_boundaries_areas_z16_20' , ' mv_admin_boundaries_centroids_z16_20' , ' id, osm_id, type' , NULL );
88102
89103-- Refresh centroids views
90104-- REFRESH MATERIALIZED VIEW CONCURRENTLY mv_admin_boundaries_centroids_z0_2;
@@ -94,3 +108,5 @@ SELECT create_admin_boundaries_centroids_mview('osm_admin_areas', 'mv_admin_boun
94108-- REFRESH MATERIALIZED VIEW CONCURRENTLY mv_admin_boundaries_centroids_z10_12;
95109-- REFRESH MATERIALIZED VIEW CONCURRENTLY mv_admin_boundaries_centroids_z13_15;
96110-- REFRESH MATERIALIZED VIEW CONCURRENTLY mv_admin_boundaries_centroids_z16_20;
111+
112+
0 commit comments