2020-09-14 16:13:32 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS ne_10m_admin_0_bg_buffer AS
|
|
|
|
SELECT ST_Buffer(geometry, 10000)
|
|
|
|
FROM ne_10m_admin_0_countries
|
|
|
|
WHERE iso_a2 = 'GB';
|
2017-07-12 14:06:15 +00:00
|
|
|
|
2020-09-28 12:56:10 +00:00
|
|
|
CREATE OR REPLACE VIEW gbr_route_members_view AS
|
|
|
|
SELECT 0,
|
|
|
|
osm_id,
|
|
|
|
substring(ref FROM E'^[AM][0-9AM()]+'),
|
|
|
|
CASE WHEN highway = 'motorway' THEN 'omt-gb-motorway' ELSE 'omt-gb-trunk' END
|
|
|
|
FROM osm_highway_linestring
|
|
|
|
WHERE length(ref) > 0
|
|
|
|
AND ST_Intersects(geometry, (SELECT * FROM ne_10m_admin_0_bg_buffer))
|
|
|
|
AND highway IN ('motorway', 'trunk')
|
|
|
|
;
|
|
|
|
-- Create GBR relations (so we can use it in the same way as other relations)
|
|
|
|
DELETE
|
|
|
|
FROM osm_route_member
|
|
|
|
WHERE network IN ('omt-gb-motorway', 'omt-gb-trunk');
|
|
|
|
-- etldoc: osm_highway_linestring -> osm_route_member
|
|
|
|
INSERT INTO osm_route_member (osm_id, member, ref, network)
|
|
|
|
SELECT *
|
|
|
|
FROM gbr_route_members_view;
|
2017-07-12 14:06:15 +00:00
|
|
|
|
2021-07-26 14:37:12 +00:00
|
|
|
CREATE OR REPLACE FUNCTION osm_route_member_network_type(network text) RETURNS route_network_type AS
|
2020-09-14 16:13:32 +00:00
|
|
|
$$
|
|
|
|
SELECT CASE
|
|
|
|
WHEN network = 'US:I' THEN 'us-interstate'::route_network_type
|
|
|
|
WHEN network = 'US:US' THEN 'us-highway'::route_network_type
|
|
|
|
WHEN network LIKE 'US:__' THEN 'us-state'::route_network_type
|
|
|
|
-- https://en.wikipedia.org/wiki/Trans-Canada_Highway
|
2021-07-06 06:43:26 +00:00
|
|
|
WHEN network LIKE 'CA:transcanada%' THEN 'ca-transcanada'::route_network_type
|
2020-09-14 16:13:32 +00:00
|
|
|
WHEN network = 'omt-gb-motorway' THEN 'gb-motorway'::route_network_type
|
|
|
|
WHEN network = 'omt-gb-trunk' THEN 'gb-trunk'::route_network_type
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE sql IMMUTABLE
|
|
|
|
PARALLEL SAFE;
|
|
|
|
|
2017-07-12 14:06:15 +00:00
|
|
|
-- etldoc: osm_route_member -> osm_route_member
|
2020-09-28 12:56:10 +00:00
|
|
|
-- see http://wiki.openstreetmap.org/wiki/Relation:route#Road_routes
|
|
|
|
UPDATE osm_route_member
|
2021-07-26 14:37:12 +00:00
|
|
|
SET network_type = osm_route_member_network_type(network)
|
2020-09-28 12:56:10 +00:00
|
|
|
WHERE network != ''
|
2021-07-26 14:37:12 +00:00
|
|
|
AND network_type IS DISTINCT FROM osm_route_member_network_type(network)
|
2020-09-28 12:56:10 +00:00
|
|
|
;
|
|
|
|
|
2020-06-08 16:19:55 +00:00
|
|
|
CREATE OR REPLACE FUNCTION update_osm_route_member() RETURNS void AS
|
|
|
|
$$
|
2017-07-12 14:06:15 +00:00
|
|
|
BEGIN
|
2020-09-28 12:56:10 +00:00
|
|
|
DELETE
|
|
|
|
FROM osm_route_member AS r
|
|
|
|
USING
|
|
|
|
transportation_name.network_changes AS c
|
|
|
|
WHERE network IN ('omt-gb-motorway', 'omt-gb-trunk')
|
|
|
|
AND r.osm_id = c.osm_id;
|
2017-07-12 14:06:15 +00:00
|
|
|
|
2020-09-28 12:56:10 +00:00
|
|
|
INSERT INTO osm_route_member (osm_id, member, ref, network)
|
|
|
|
SELECT r.*
|
|
|
|
FROM gbr_route_members_view AS r
|
|
|
|
JOIN transportation_name.network_changes AS c ON
|
|
|
|
r.osm_id = c.osm_id;
|
2017-07-12 14:06:15 +00:00
|
|
|
|
2021-11-18 16:35:34 +00:00
|
|
|
INSERT INTO osm_route_member (id, osm_id, network_type, concurrency_index, rank)
|
2021-09-21 10:56:25 +00:00
|
|
|
SELECT
|
|
|
|
id,
|
2021-10-14 15:38:28 +00:00
|
|
|
osm_id,
|
2021-09-21 10:56:25 +00:00
|
|
|
osm_route_member_network_type(network) AS network_type,
|
2021-11-18 16:35:34 +00:00
|
|
|
DENSE_RANK() over (PARTITION BY member ORDER BY network_type, network, LENGTH(ref), ref) AS concurrency_index,
|
|
|
|
CASE
|
|
|
|
WHEN network IN ('iwn', 'nwn', 'rwn') THEN 1
|
|
|
|
WHEN network = 'lwn' THEN 2
|
|
|
|
WHEN osmc_symbol || colour <> '' THEN 2
|
|
|
|
END AS rank
|
2021-09-21 10:56:25 +00:00
|
|
|
FROM osm_route_member rm
|
|
|
|
WHERE rm.member IN
|
|
|
|
(SELECT DISTINCT osm_id FROM transportation_name.network_changes)
|
2021-11-18 16:35:34 +00:00
|
|
|
ON CONFLICT (id, osm_id) DO UPDATE SET concurrency_index = EXCLUDED.concurrency_index,
|
2022-05-05 11:14:35 +00:00
|
|
|
rank = EXCLUDED.rank,
|
|
|
|
network_type = EXCLUDED.network_type;
|
2017-07-12 14:06:15 +00:00
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
|
2020-06-08 16:19:55 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS osm_route_member_network_idx ON osm_route_member ("network");
|
|
|
|
CREATE INDEX IF NOT EXISTS osm_route_member_member_idx ON osm_route_member ("member");
|
|
|
|
CREATE INDEX IF NOT EXISTS osm_route_member_name_idx ON osm_route_member ("name");
|
|
|
|
CREATE INDEX IF NOT EXISTS osm_route_member_ref_idx ON osm_route_member ("ref");
|
2017-07-12 14:06:15 +00:00
|
|
|
|
2020-06-08 16:19:55 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS osm_route_member_network_type_idx ON osm_route_member ("network_type");
|
2021-07-02 13:00:25 +00:00
|
|
|
|
Transportation generalization table optimization and cleanup (#1172)
This PR updates the `transportation` layer creation scripts to simplify the SQL and remove unneeded sub-selects, checks and conditionals, fix indexes, and improve inline documentation.
Currently, there are two sequences of materialized view creations. There is one from zoom 11 through 9, and a second one for zoom 8 through 4. This PR removes that break in the sequence of transportation table materialized view creations, in favor of one in which high-zoom views are created first, and then each lower zoom is created from the zoom above.
Instead, the current generalized zoom 8 transportation table is built directly from `osm_transportation_linestring` rather than being built from the zoom 9 transportation table. This means that when building the zoom 8 table, it must scan the entire transportation network rather than just selecting from the pre-filtered zoom 9 table.
This PR removes an unneeded sub-select in the build of the zoom 8 table, which appears to be a leftover from an old version of the SQL that did some sort of merge. Once this PR is implemented all zooms from 11 through 4 will be linked via a progressive series of materialized views.
Lastly, this adds in missing materialized view refreshes for zooms 9-11, which appear to have been entirely missing, and as far as I can tell aren't getting updated in the current version of this code.
In addition, the following optimizations were added as part of this commit:
1. Updates the `osm_highway_linestring_highway_partial_idx` partial index to match the `SELECT..WHERE` clause actually present in `transportation/update_route_member.sql`, which is where it appears to be actually used, and update inline documentation to reflect this.
2. Collapses unnecessary sub-select block in `osm_transportation_merge_linestring_gen_z11`, and removes unnecessary ST_IsValid() call, which already provided in `mapping.yaml`, and update inline documentation to reflect these assumptions.
3. Updates `WHERE` blocks to remove unnecesary checks and further document assumptions. The `highway=construction` check is unnecessary in all cases, because it is sufficient to check the `construction` key alone. If `construction=*` is set, then `highway=construction` is implied.
4. Two indexes were added to `layers/transportation/update_route_member.sql` to improve route population performance.
In testing locally, I'm seeing performance improvements around 10% in the generation of the `transportation` layer, based on modifying `openmaptiles.yaml` to generate only the transportation layer and then repeatedly running `time make import-sql`, however, this timing might be impacted by docker, so I would ask for confirmation of acceptable performance.
In addition, this PR shortens the length of the transportation update SQL file by 30 lines, which makes it easier for contributors to work with.
2021-08-13 16:00:33 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS osm_highway_linestring_osm_id_idx ON osm_highway_linestring ("osm_id");
|
2022-05-04 12:22:15 +00:00
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS osm_highway_linestring_gen_z11_osm_id_idx ON osm_highway_linestring_gen_z11 ("osm_id");
|
Transportation generalization table optimization and cleanup (#1172)
This PR updates the `transportation` layer creation scripts to simplify the SQL and remove unneeded sub-selects, checks and conditionals, fix indexes, and improve inline documentation.
Currently, there are two sequences of materialized view creations. There is one from zoom 11 through 9, and a second one for zoom 8 through 4. This PR removes that break in the sequence of transportation table materialized view creations, in favor of one in which high-zoom views are created first, and then each lower zoom is created from the zoom above.
Instead, the current generalized zoom 8 transportation table is built directly from `osm_transportation_linestring` rather than being built from the zoom 9 transportation table. This means that when building the zoom 8 table, it must scan the entire transportation network rather than just selecting from the pre-filtered zoom 9 table.
This PR removes an unneeded sub-select in the build of the zoom 8 table, which appears to be a leftover from an old version of the SQL that did some sort of merge. Once this PR is implemented all zooms from 11 through 4 will be linked via a progressive series of materialized views.
Lastly, this adds in missing materialized view refreshes for zooms 9-11, which appear to have been entirely missing, and as far as I can tell aren't getting updated in the current version of this code.
In addition, the following optimizations were added as part of this commit:
1. Updates the `osm_highway_linestring_highway_partial_idx` partial index to match the `SELECT..WHERE` clause actually present in `transportation/update_route_member.sql`, which is where it appears to be actually used, and update inline documentation to reflect this.
2. Collapses unnecessary sub-select block in `osm_transportation_merge_linestring_gen_z11`, and removes unnecessary ST_IsValid() call, which already provided in `mapping.yaml`, and update inline documentation to reflect these assumptions.
3. Updates `WHERE` blocks to remove unnecesary checks and further document assumptions. The `highway=construction` check is unnecessary in all cases, because it is sufficient to check the `construction` key alone. If `construction=*` is set, then `highway=construction` is implied.
4. Two indexes were added to `layers/transportation/update_route_member.sql` to improve route population performance.
In testing locally, I'm seeing performance improvements around 10% in the generation of the `transportation` layer, based on modifying `openmaptiles.yaml` to generate only the transportation layer and then repeatedly running `time make import-sql`, however, this timing might be impacted by docker, so I would ask for confirmation of acceptable performance.
In addition, this PR shortens the length of the transportation update SQL file by 30 lines, which makes it easier for contributors to work with.
2021-08-13 16:00:33 +00:00
|
|
|
|
2021-11-18 16:35:34 +00:00
|
|
|
ALTER TABLE osm_route_member ADD COLUMN IF NOT EXISTS concurrency_index int,
|
|
|
|
ADD COLUMN IF NOT EXISTS rank int;
|
2021-07-02 13:00:25 +00:00
|
|
|
|
2021-09-21 10:56:25 +00:00
|
|
|
-- One-time load of concurrency indexes; updates occur via trigger
|
2021-11-18 16:35:34 +00:00
|
|
|
INSERT INTO osm_route_member (id, osm_id, concurrency_index, rank)
|
2021-07-02 13:00:25 +00:00
|
|
|
SELECT
|
|
|
|
id,
|
2021-10-14 15:38:28 +00:00
|
|
|
osm_id,
|
2021-11-18 16:35:34 +00:00
|
|
|
DENSE_RANK() over (PARTITION BY member ORDER BY network_type, network, LENGTH(ref), ref) AS concurrency_index,
|
|
|
|
CASE
|
|
|
|
WHEN network IN ('iwn', 'nwn', 'rwn') THEN 1
|
|
|
|
WHEN network = 'lwn' THEN 2
|
|
|
|
WHEN osmc_symbol || colour <> '' THEN 2
|
|
|
|
END AS rank
|
2021-07-02 13:00:25 +00:00
|
|
|
FROM osm_route_member
|
2021-11-18 16:35:34 +00:00
|
|
|
ON CONFLICT (id, osm_id) DO UPDATE SET concurrency_index = EXCLUDED.concurrency_index, rank = EXCLUDED.rank;
|
2021-08-04 08:10:16 +00:00
|
|
|
|
|
|
|
UPDATE osm_highway_linestring hl
|
|
|
|
SET network = rm.network_type
|
|
|
|
FROM osm_route_member rm
|
|
|
|
WHERE hl.osm_id=rm.member AND rm.concurrency_index=1;
|
|
|
|
|
|
|
|
UPDATE osm_highway_linestring_gen_z11 hl
|
|
|
|
SET network = rm.network_type
|
|
|
|
FROM osm_route_member rm
|
|
|
|
WHERE hl.osm_id=rm.member AND rm.concurrency_index=1;
|