1 create or replace function getNearFeatures(in_partition INTEGER, point GEOMETRY, maxrank INTEGER, isin_tokens INT[]) RETURNS setof nearfeaturecentr AS $$
3 r nearfeaturecentr%rowtype;
7 IF in_partition = -partition- THEN
9 SELECT place_id, keywords, rank_address, rank_search, min(ST_Distance(point, centroid)) as distance, isguess, centroid FROM (
10 SELECT * FROM location_area_large_-partition- WHERE ST_Contains(geometry, point) and rank_search < maxrank
12 SELECT * FROM location_area_country WHERE ST_Contains(geometry, point) and rank_search < maxrank
14 GROUP BY place_id, keywords, rank_address, rank_search, isguess, centroid
15 ORDER BY rank_address, isin_tokens && keywords desc, isguess asc,
16 ST_Distance(point, centroid) *
18 WHEN rank_address = 16 AND rank_search = 15 THEN 0.2 -- capital city
19 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25 -- city
20 WHEN rank_address = 16 AND rank_search = 17 THEN 0.5 -- town
21 ELSE 1 END ASC -- everything else
29 RAISE EXCEPTION 'Unknown partition %', in_partition;
34 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT, in_rank_search INTEGER) RETURNS BOOLEAN AS $$
38 IF in_rank_search <= 4 THEN
39 DELETE from location_area_country WHERE place_id = in_place_id;
44 IF in_partition = -partition- THEN
45 DELETE from location_area_large_-partition- WHERE place_id = in_place_id;
50 RAISE EXCEPTION 'Unknown partition %', in_partition;
57 create or replace function insertLocationAreaLarge(
58 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_keywords INTEGER[],
59 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN,
60 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
64 IF in_rank_search <= 4 THEN
65 INSERT INTO location_area_country values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, in_centroid, in_geometry);
70 IF in_partition = -partition- THEN
71 INSERT INTO location_area_large_-partition- values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, in_centroid, in_geometry);
76 RAISE EXCEPTION 'Unknown partition %', in_partition;
82 create or replace function getNearestNamedFeature(in_partition INTEGER, point GEOMETRY, maxrank INTEGER, isin_token INTEGER) RETURNS setof nearfeature AS $$
84 r nearfeature%rowtype;
88 IF in_partition = -partition- THEN
90 SELECT place_id, name_vector, address_rank, search_rank,
91 ST_Distance(centroid, point) as distance, null as isguess
92 FROM search_name_-partition-
93 WHERE name_vector @> ARRAY[isin_token]
94 AND search_rank < maxrank
96 SELECT place_id, name_vector, address_rank, search_rank,
97 ST_Distance(centroid, point) as distance, null as isguess
98 FROM search_name_country
99 WHERE name_vector @> ARRAY[isin_token]
100 AND search_rank < maxrank
101 ORDER BY distance ASC limit 1
109 RAISE EXCEPTION 'Unknown partition %', in_partition;
114 create or replace function getNearestNamedRoadFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER)
115 RETURNS setof nearfeature AS $$
117 r nearfeature%rowtype;
121 IF in_partition = -partition- THEN
123 SELECT place_id, name_vector, address_rank, search_rank,
124 ST_Distance(centroid, point) as distance, null as isguess
125 FROM search_name_-partition-
126 WHERE name_vector @> ARRAY[isin_token]
127 AND ST_DWithin(centroid, point, 0.01)
128 AND search_rank between 26 and 27
129 ORDER BY distance ASC limit 1
137 RAISE EXCEPTION 'Unknown partition %', in_partition;
142 create or replace function getNearestNamedPlaceFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER)
143 RETURNS setof nearfeature AS $$
145 r nearfeature%rowtype;
149 IF in_partition = -partition- THEN
151 SELECT place_id, name_vector, address_rank, search_rank,
152 ST_Distance(centroid, point) as distance, null as isguess
153 FROM search_name_-partition-
154 WHERE name_vector @> ARRAY[isin_token]
155 AND ST_DWithin(centroid, point, 0.03)
156 AND search_rank between 16 and 22
157 ORDER BY distance ASC limit 1
165 RAISE EXCEPTION 'Unknown partition %', in_partition;
171 create or replace function getNearestPostcode(in_partition INTEGER, point GEOMETRY)
178 IF in_partition = -partition- THEN
180 FROM location_area_large_-partition- join placex using (place_id)
181 WHERE st_contains(location_area_large_-partition-.geometry, point)
182 AND class = 'place' and type = 'postcode'
183 ORDER BY st_distance(location_area_large_-partition-.centroid, point) ASC limit 1
189 RAISE EXCEPTION 'Unknown partition %', in_partition;
194 create or replace function insertSearchName(
195 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2),
196 in_name_vector INTEGER[], in_nameaddress_vector INTEGER[],
197 in_rank_search INTEGER, in_rank_address INTEGER, in_importance FLOAT,
198 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
202 DELETE FROM search_name WHERE place_id = in_place_id;
203 INSERT INTO search_name values (in_place_id, in_rank_search, in_rank_address, in_importance, in_country_code,
204 in_name_vector, in_nameaddress_vector, in_centroid);
206 IF in_rank_search <= 4 THEN
207 DELETE FROM search_name_country WHERE place_id = in_place_id;
208 INSERT INTO search_name_country values (in_place_id, in_rank_search, in_rank_address,
209 in_name_vector, in_geometry);
214 IF in_partition = -partition- THEN
215 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
216 INSERT INTO search_name_-partition- values (in_place_id, in_rank_search, in_rank_address,
217 in_name_vector, in_geometry);
222 RAISE EXCEPTION 'Unknown partition %', in_partition;
228 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
232 DELETE from search_name WHERE place_id = in_place_id;
233 DELETE from search_name_country WHERE place_id = in_place_id;
236 IF in_partition = -partition- THEN
237 DELETE from search_name_-partition- WHERE place_id = in_place_id;
242 RAISE EXCEPTION 'Unknown partition %', in_partition;
249 create or replace function insertLocationRoad(
250 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
255 IF in_partition = -partition- THEN
256 DELETE FROM location_road_-partition- where place_id = in_place_id;
257 INSERT INTO location_road_-partition- values (in_partition, in_place_id, in_country_code, in_geometry);
262 RAISE EXCEPTION 'Unknown partition %', in_partition;
268 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
273 IF in_partition = -partition- THEN
274 DELETE FROM location_road_-partition- where place_id = in_place_id;
279 RAISE EXCEPTION 'Unknown partition %', in_partition;
286 create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
288 r nearfeature%rowtype;
289 search_diameter FLOAT;
293 IF in_partition = -partition- THEN
294 search_diameter := 0.00005;
295 WHILE search_diameter < 0.1 LOOP
297 SELECT place_id, null, null, null,
298 ST_Distance(geometry, point) as distance, null as isguess
299 FROM location_road_-partition-
300 WHERE ST_DWithin(geometry, point, search_diameter)
301 ORDER BY distance ASC limit 1
306 search_diameter := search_diameter * 2;
312 RAISE EXCEPTION 'Unknown partition %', in_partition;
317 create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
319 r nearfeature%rowtype;
320 search_diameter FLOAT;
326 IF st_geometrytype(line) not in ('ST_LineString') THEN
330 p1 := ST_Line_Interpolate_Point(line,0);
331 p2 := ST_Line_Interpolate_Point(line,0.5);
332 p3 := ST_Line_Interpolate_Point(line,1);
335 IF in_partition = -partition- THEN
336 search_diameter := 0.0005;
337 WHILE search_diameter < 0.01 LOOP
339 SELECT place_id, null, null, null,
340 ST_Distance(geometry, line) as distance, null as isguess
341 FROM location_road_-partition-
342 WHERE ST_DWithin(line, geometry, search_diameter)
343 ORDER BY (ST_distance(geometry, p1)+
344 ST_distance(geometry, p2)+
345 ST_distance(geometry, p3)) ASC limit 1
350 search_diameter := search_diameter * 2;
356 RAISE EXCEPTION 'Unknown partition %', in_partition;