From 84ea0753d8d7861ff6a47a6eb29f072a1d342879 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Wed, 26 Feb 2020 10:04:17 +0100 Subject: [PATCH] simplify getNearestRoadFeature function The function only ever returns one result of which only the place_id is used. So simplify it to return a single place_id only (or NULL if none is found). Rename funciton to avoid conflicts when updating an existing database. --- sql/functions/aux_property.sql | 4 ++-- sql/functions/placex_triggers.sql | 3 +-- sql/partition-functions.src.sql | 23 +++++++++++------------ sql/tiger_import_start.sql | 5 ++--- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/sql/functions/aux_property.sql b/sql/functions/aux_property.sql index cee5cfb2..6dd99eb2 100644 --- a/sql/functions/aux_property.sql +++ b/sql/functions/aux_property.sql @@ -28,8 +28,8 @@ BEGIN END IF; IF out_parent_place_id IS NULL THEN - FOR location IN SELECT place_id FROM getNearestRoadFeature(out_partition, place_centroid) LOOP - out_parent_place_id := location.place_id; + SELECT getNearestRoadPlaceId(out_partition, place_centroid) + INTO out_parent_place_id; END LOOP; END IF; diff --git a/sql/functions/placex_triggers.sql b/sql/functions/placex_triggers.sql index fb3f2dc5..ffc83fa3 100644 --- a/sql/functions/placex_triggers.sql +++ b/sql/functions/placex_triggers.sql @@ -135,8 +135,7 @@ BEGIN IF fallback THEN IF ST_Area(bbox) < 0.01 THEN -- for smaller features get the nearest road - SELECT place_id FROM getNearestRoadFeature(poi_partition, bbox) - INTO parent_place_id; + SELECT getNearestRoadPlaceId(poi_partition, bbox) INTO parent_place_id; --DEBUG: RAISE WARNING 'Checked for nearest way (%)', parent_place_id; ELSE -- for larger features simply find the area with the largest rank that diff --git a/sql/partition-functions.src.sql b/sql/partition-functions.src.sql index 41758c83..4a8f9139 100644 --- a/sql/partition-functions.src.sql +++ b/sql/partition-functions.src.sql @@ -214,29 +214,28 @@ END $$ LANGUAGE plpgsql; -create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$ +CREATE OR REPLACE FUNCTION getNearestRoadPlaceId(in_partition INTEGER, point GEOMETRY) + RETURNS BIGINT + AS $$ DECLARE - r nearfeature%rowtype; - search_diameter FLOAT; + r RECORD; + search_diameter FLOAT; BEGIN -- start IF in_partition = -partition- THEN search_diameter := 0.00005; WHILE search_diameter < 0.1 LOOP - FOR r IN - SELECT place_id, null, null, null, - ST_Distance(geometry, point) as distance, null as isguess - FROM location_road_-partition- - WHERE ST_DWithin(geometry, point, search_diameter) - ORDER BY distance ASC limit 1 + FOR r IN + SELECT place_id FROM location_road_-partition- + WHERE ST_DWithin(geometry, point, search_diameter) + ORDER BY ST_Distance(geometry, point) ASC limit 1 LOOP - RETURN NEXT r; - RETURN; + RETURN r.place_id; END LOOP; search_diameter := search_diameter * 2; END LOOP; - RETURN; + RETURN NULL; END IF; -- end diff --git a/sql/tiger_import_start.sql b/sql/tiger_import_start.sql index b9c4fcfc..5f916501 100644 --- a/sql/tiger_import_start.sql +++ b/sql/tiger_import_start.sql @@ -69,9 +69,8 @@ BEGIN END IF; IF out_parent_place_id IS NULL THEN - FOR location IN SELECT place_id FROM getNearestRoadFeature(out_partition, place_centroid) LOOP - out_parent_place_id := location.place_id; - END LOOP; + SELECT getNearestRoadPlaceId(out_partition, place_centroid) + INTO out_parent_place_id; END IF; --insert street(line) into import table -- 2.39.5