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);
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;
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