From 8a4c7f6e2b5f46fca1adbfbe1718a273a3d43760 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Wed, 26 Feb 2020 10:14:28 +0100 Subject: [PATCH] simplify getNearestParallelRoadFeature 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). Also fix typo in function name. --- sql/partition-functions.src.sql | 36 ++++++++++++++++----------------- sql/tiger_import_start.sql | 5 ++--- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/sql/partition-functions.src.sql b/sql/partition-functions.src.sql index 4a8f9139..dbe34363 100644 --- a/sql/partition-functions.src.sql +++ b/sql/partition-functions.src.sql @@ -242,19 +242,22 @@ BEGIN RAISE EXCEPTION 'Unknown partition %', in_partition; END $$ -LANGUAGE plpgsql; +LANGUAGE plpgsql IMMUTABLE; -create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$ +CREATE OR REPLACE FUNCTION getNearestParallelRoadFeature(in_partition INTEGER, + line GEOMETRY) + RETURNS BIGINT + AS $$ DECLARE - r nearfeature%rowtype; - search_diameter FLOAT; + r RECORD; + search_diameter FLOAT; p1 GEOMETRY; p2 GEOMETRY; p3 GEOMETRY; BEGIN - IF st_geometrytype(line) not in ('ST_LineString') THEN - RETURN; + IF ST_GeometryType(line) not in ('ST_LineString') THEN + RETURN NULL; END IF; p1 := ST_LineInterpolatePoint(line,0); @@ -265,25 +268,22 @@ BEGIN IF in_partition = -partition- THEN search_diameter := 0.0005; WHILE search_diameter < 0.01 LOOP - FOR r IN - SELECT place_id, null, null, null, - ST_Distance(geometry, line) as distance, null as isguess - FROM location_road_-partition- - WHERE ST_DWithin(line, geometry, search_diameter) - ORDER BY (ST_distance(geometry, p1)+ - ST_distance(geometry, p2)+ - ST_distance(geometry, p3)) ASC limit 1 + FOR r IN + SELECT place_id FROM location_road_-partition- + WHERE ST_DWithin(line, geometry, search_diameter) + ORDER BY (ST_distance(geometry, p1)+ + ST_distance(geometry, p2)+ + ST_distance(geometry, p3)) 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 RAISE EXCEPTION 'Unknown partition %', in_partition; END $$ -LANGUAGE plpgsql; +LANGUAGE plpgsql IMMUTABLE; diff --git a/sql/tiger_import_start.sql b/sql/tiger_import_start.sql index 5f916501..4b9c33fc 100644 --- a/sql/tiger_import_start.sql +++ b/sql/tiger_import_start.sql @@ -63,9 +63,8 @@ BEGIN END IF; IF out_parent_place_id IS NULL THEN - FOR location IN SELECT place_id FROM getNearestParellelRoadFeature(out_partition, linegeo) LOOP - out_parent_place_id := location.place_id; - END LOOP; + SELECT getNearestParallelRoadFeature(out_partition, linegeo) + INTO out_parent_place_id; END IF; IF out_parent_place_id IS NULL THEN -- 2.39.5