From bc9bbd2e67bb90163b9e85789c89fd4d87561b8d Mon Sep 17 00:00:00 2001 From: Brian Sperlongano Date: Thu, 26 Jan 2023 10:15:47 -0500 Subject: [PATCH] Coalesce duplicate route concurrencies (#1361) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Problem description #1128 introduced route relation concurrency information in OpenMapTiles via the `route_X` attributes. The original implementation assumed that there would be a single route relation for each `network` and `ref` pair. However, it is increasingly common practice to tag a separate route relation for each direction of a route in order to provide awareness to routers and other data consumers of the directionality of a route. This standard and growing practice is described on the [OSM wiki page on route directions](https://wiki.openstreetmap.org/wiki/Route_directions). Thus, the naïve implementation of #1128 caused duplicate entries to be added as `route_X` attributes in the case where separate route relations were used for directional routes. # Solution description This PR adds grouping when computing route concurrency information, such that duplicate entries are coalesced in a predictable way. Since this grouping is done only within a route membership join of a single member way, the computational complexity should be trivial. --- layers/transportation/update_route_member.sql | 11 +++++++++++ tests/import/500_import-highway.osm | 18 ++++++++++++++++++ tests/test-post-import.sql | 7 +++++++ 3 files changed, 36 insertions(+) diff --git a/layers/transportation/update_route_member.sql b/layers/transportation/update_route_member.sql index fd128139..83361abc 100644 --- a/layers/transportation/update_route_member.sql +++ b/layers/transportation/update_route_member.sql @@ -149,6 +149,17 @@ CREATE INDEX IF NOT EXISTS osm_route_member_ref_idx ON osm_route_member ("ref"); CREATE INDEX IF NOT EXISTS osm_route_member_network_type_idx ON osm_route_member ("network_type"); +/** +* Discard duplicate routes +*/ +DELETE FROM osm_route_member WHERE id IN + (SELECT id + FROM (SELECT id, + ROW_NUMBER() OVER (partition BY member, network, ref ORDER BY id) AS rnum + FROM osm_route_member) t + WHERE t.rnum > 1); +CREATE UNIQUE INDEX IF NOT EXISTS osm_route_member_network_ref_idx ON osm_route_member ("member", "network", "ref"); + CREATE INDEX IF NOT EXISTS osm_highway_linestring_osm_id_idx ON osm_highway_linestring ("osm_id"); CREATE UNIQUE INDEX IF NOT EXISTS osm_highway_linestring_gen_z11_osm_id_idx ON osm_highway_linestring_gen_z11 ("osm_id"); diff --git a/tests/import/500_import-highway.osm b/tests/import/500_import-highway.osm index 7fcbec92..8b745512 100644 --- a/tests/import/500_import-highway.osm +++ b/tests/import/500_import-highway.osm @@ -147,4 +147,22 @@ + + + + + + + + + + + + + + + + + + diff --git a/tests/test-post-import.sql b/tests/test-post-import.sql index f874a25a..b1af312c 100644 --- a/tests/test-post-import.sql +++ b/tests/test-post-import.sql @@ -160,6 +160,13 @@ BEGIN INSERT INTO omt_test_failures VALUES(500, 'import', 'osm_transportation_name_linestring z12 route_rank expected 1, got ' || cnt); END IF; + -- Duplicate route concurrencies collapsed + SELECT COUNT(*) INTO cnt FROM osm_route_member + WHERE network='US:I' AND ref='95'; + IF cnt <> 1 THEN + INSERT INTO omt_test_failures VALUES(500, 'import', 'osm_route_member 1 route membership expected, got ' || cnt); + END IF; + -- Test 600 -- verify that atms are imported with correct name which can come from tags like operator or network