+
+create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
+DECLARE
+ r nearfeature%rowtype;
+ 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
+ LOOP
+ RETURN NEXT r;
+ RETURN;
+ END LOOP;
+ search_diameter := search_diameter * 2;
+ END LOOP;
+ RETURN;
+ END IF;
+-- end
+
+ RAISE EXCEPTION 'Unknown partition %', in_partition;
+END
+$$
+LANGUAGE plpgsql;
+
+create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
+DECLARE
+ r nearfeature%rowtype;
+ search_diameter FLOAT;
+ p1 GEOMETRY;
+ p2 GEOMETRY;
+ p3 GEOMETRY;
+BEGIN
+
+ IF st_geometrytype(line) not in ('ST_LineString') THEN
+ RETURN;
+ END IF;
+
+ p1 := ST_Line_Interpolate_Point(line,0);
+ p2 := ST_Line_Interpolate_Point(line,0.5);
+ p3 := ST_Line_Interpolate_Point(line,1);
+
+-- start
+ 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
+ LOOP
+ RETURN NEXT r;
+ RETURN;
+ END LOOP;
+ search_diameter := search_diameter * 2;
+ END LOOP;
+ RETURN;
+ END IF;
+-- end
+
+ RAISE EXCEPTION 'Unknown partition %', in_partition;
+END
+$$
+LANGUAGE plpgsql;