Skip to content

Commit

Permalink
Calculate innermost indicator for nested places #50
Browse files Browse the repository at this point in the history
  • Loading branch information
rustprooflabs committed Jan 20, 2021
1 parent 6134b6c commit 67bb51c
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions flex-config/place.sql
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,19 @@ CREATE TABLE osm.place_polygon_nested
name_path TEXT[] NULL,
osm_id_path BIGINT[] NULL,
admin_level_path INT[] NULL,
row_innermost BOOLEAN NOT NULL GENERATED ALWAYS AS (
CASE WHEN osm_id_path[array_length(osm_id_path, 1)] = osm_id THEN True
ELSE False
END
) STORED,
innermost BOOLEAN NOT NULL DEFAULT False,
geom GEOMETRY NOT NULL, -- Can't enforce geom type b/c SRID is dynamic project wide. Can't set MULTIPOLYGON w/out SRID too
CONSTRAINT fk_place_polygon_nested
FOREIGN KEY (osm_id) REFERENCES osm.place_polygon (osm_id)
);



CREATE INDEX ix_osm_place_polygon_nested_osm_id
ON osm.place_polygon_nested (osm_id)
;
Expand All @@ -117,7 +124,8 @@ COMMENT ON COLUMN osm.place_polygon_nested.name_path IS 'Array of names of the c
COMMENT ON COLUMN osm.place_polygon_nested.osm_id_path IS 'Array of osm_id for the current polygon (last) and all containing polygons.';
COMMENT ON COLUMN osm.place_polygon_nested.admin_level_path IS 'Array of admin_level values for the current polygon (last) and all containing polygons.';
COMMENT ON COLUMN osm.place_polygon_nested.name IS 'Best name option determined by helpers.get_name(). Keys with priority are: name, short_name, alt_name and loc_name. See pgosm-flex/flex-config/helpers.lua for full logic of selection.';

COMMENT ON COLUMN osm.place_polygon_nested.row_innermost IS 'Indicates if the osm_id is the most inner ID of the current row. Used to calculated innermost after all nesting paths have been calculated.';
COMMENT ON COLUMN osm.place_polygon_nested.innermost IS 'Indiciates this row is the innermost admin level of the current data set and does **not** itself contain another admin polygon. Calculated by procedure osm.build_nested_admin_polygons() defined in pgosm-flex/flex-config/place.sql.';

INSERT INTO osm.place_polygon_nested (osm_id, name, osm_type, admin_level, geom)
SELECT p.osm_id, p.name, p.osm_type,
Expand All @@ -130,9 +138,6 @@ SELECT p.osm_id, p.name, p.osm_type,






CREATE OR REPLACE PROCEDURE osm.build_nested_admin_polygons(
batch_row_limit BIGINT = 100
)
Expand Down Expand Up @@ -186,6 +191,24 @@ CREATE OR REPLACE PROCEDURE osm.build_nested_admin_polygons(

DROP TABLE IF EXISTS place_batch;

-- With all nested paths calculated the innermost value can be determined.
WITH calc_inner AS (
SELECT a.osm_id
FROM osm.place_polygon_nested a
WHERE a.row_innermost -- Start with per row check...
-- If an osm_id is found in any other path, cannot be innermost
AND NOT EXISTS (
SELECT 1
FROM osm.place_polygon_nested i
WHERE a.osm_id <> i.osm_id
AND a.osm_id = ANY(osm_id_path)
)
)
UPDATE osm.place_polygon_nested n
SET innermost = True
FROM calc_inner i
WHERE n.osm_id = i.osm_id
;
END $$;


Expand All @@ -208,4 +231,3 @@ CREATE INDEX gix_osm_vplace_polygon_subdivide
COMMENT ON MATERIALIZED VIEW osm.vplace_polygon_subdivide IS 'Subdivided geometry from osm.vplace_polygon. Multiple rows per osm_id, one for each subdivided geometry.';

COMMENT ON COLUMN osm.vplace_polygon_subdivide.osm_id IS 'OpenStreetMap ID. Unique along with geometry type. Duplicated in this view!';

0 comments on commit 67bb51c

Please sign in to comment.