+
+create or replace function insertLocationRoad(
+ in_partition INTEGER, in_place_id integer, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
+DECLARE
+BEGIN
+
+-- start
+ IF in_partition = -partition- THEN
+ DELETE FROM location_road_-partition- where place_id = in_place_id;
+ INSERT INTO location_road_-partition- values (in_partition, in_place_id, in_country_code, in_geometry);
+ RETURN TRUE;
+ END IF;
+-- end
+
+ RAISE EXCEPTION 'Unknown partition %', in_partition;
+ RETURN FALSE;
+END
+$$
+LANGUAGE plpgsql;
+
+create or replace function deleteRoad(in_partition INTEGER, in_place_id integer) RETURNS BOOLEAN AS $$
+DECLARE
+BEGIN
+
+-- start
+ IF in_partition = -partition- THEN
+ DELETE FROM location_road_-partition- where place_id = in_place_id;
+ RETURN TRUE;
+ END IF;
+-- end
+
+ RAISE EXCEPTION 'Unknown partition %', in_partition;
+
+ RETURN FALSE;
+END
+$$
+LANGUAGE plpgsql;
+
+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
+ 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
+ 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;