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 $$
63 IF in_rank_address = 0 THEN
67 IF in_rank_search <= 4 THEN
68 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);
73 IF in_partition = -partition- THEN
74 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);
79 RAISE EXCEPTION 'Unknown partition %', in_partition;
85 create or replace function getNearestNamedFeature(in_partition INTEGER, point GEOMETRY, maxrank INTEGER, isin_token INTEGER) RETURNS setof nearfeature AS $$
87 r nearfeature%rowtype;
91 IF in_partition = -partition- THEN
93 SELECT place_id, name_vector, address_rank, search_rank,
94 ST_Distance(centroid, point) as distance, null as isguess
95 FROM search_name_-partition-
96 WHERE name_vector @> ARRAY[isin_token]
97 AND search_rank < maxrank
99 SELECT place_id, name_vector, address_rank, search_rank,
100 ST_Distance(centroid, point) as distance, null as isguess
101 FROM search_name_country
102 WHERE name_vector @> ARRAY[isin_token]
103 AND search_rank < maxrank
104 ORDER BY distance ASC limit 1
112 RAISE EXCEPTION 'Unknown partition %', in_partition;
117 create or replace function getNearestNamedRoadFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER[])
118 RETURNS setof nearfeature AS $$
120 r nearfeature%rowtype;
124 IF in_partition = -partition- THEN
126 SELECT place_id, name_vector, address_rank, search_rank,
127 ST_Distance(centroid, point) as distance, null as isguess
128 FROM search_name_-partition-
129 WHERE name_vector @> isin_token
130 AND ST_DWithin(centroid, point, 0.01)
131 AND search_rank between 26 and 27
132 ORDER BY distance ASC limit 1
140 RAISE EXCEPTION 'Unknown partition %', in_partition;
145 create or replace function getNearestNamedPlaceFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER[])
146 RETURNS setof nearfeature AS $$
148 r nearfeature%rowtype;
152 IF in_partition = -partition- THEN
154 SELECT place_id, name_vector, address_rank, search_rank,
155 ST_Distance(centroid, point) as distance, null as isguess
156 FROM search_name_-partition-
157 WHERE name_vector @> isin_token
158 AND ST_DWithin(centroid, point, 0.03)
159 AND search_rank between 16 and 22
160 ORDER BY distance ASC limit 1
168 RAISE EXCEPTION 'Unknown partition %', in_partition;
174 create or replace function getNearestPostcode(in_partition INTEGER, point GEOMETRY)
181 IF in_partition = -partition- THEN
183 FROM location_area_large_-partition- join placex using (place_id)
184 WHERE st_contains(location_area_large_-partition-.geometry, point)
185 AND class = 'place' and type = 'postcode'
186 ORDER BY st_distance(location_area_large_-partition-.centroid, point) ASC limit 1
192 RAISE EXCEPTION 'Unknown partition %', in_partition;
197 create or replace function insertSearchName(
198 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2),
199 in_name_vector INTEGER[], in_nameaddress_vector INTEGER[],
200 in_rank_search INTEGER, in_rank_address INTEGER, in_importance FLOAT,
201 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
205 DELETE FROM search_name WHERE place_id = in_place_id;
206 INSERT INTO search_name values (in_place_id, in_rank_search, in_rank_address, in_importance, in_country_code,
207 in_name_vector, in_nameaddress_vector, in_centroid);
209 IF in_rank_search <= 4 THEN
210 DELETE FROM search_name_country WHERE place_id = in_place_id;
211 IF in_rank_address > 0 THEN
212 INSERT INTO search_name_country values (in_place_id, in_rank_search, in_rank_address,
213 in_name_vector, in_geometry);
219 IF in_partition = -partition- THEN
220 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
221 IF in_rank_address > 0 THEN
222 INSERT INTO search_name_-partition- values (in_place_id, in_rank_search, in_rank_address,
223 in_name_vector, in_geometry);
229 RAISE EXCEPTION 'Unknown partition %', in_partition;
235 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
239 DELETE from search_name WHERE place_id = in_place_id;
240 DELETE from search_name_country WHERE place_id = in_place_id;
243 IF in_partition = -partition- THEN
244 DELETE from search_name_-partition- WHERE place_id = in_place_id;
249 RAISE EXCEPTION 'Unknown partition %', in_partition;
256 create or replace function insertLocationRoad(
257 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
262 IF in_partition = -partition- THEN
263 DELETE FROM location_road_-partition- where place_id = in_place_id;
264 INSERT INTO location_road_-partition- values (in_partition, in_place_id, in_country_code, in_geometry);
269 RAISE EXCEPTION 'Unknown partition %', in_partition;
275 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
280 IF in_partition = -partition- THEN
281 DELETE FROM location_road_-partition- where place_id = in_place_id;
286 RAISE EXCEPTION 'Unknown partition %', in_partition;
293 create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
295 r nearfeature%rowtype;
296 search_diameter FLOAT;
300 IF in_partition = -partition- THEN
301 search_diameter := 0.00005;
302 WHILE search_diameter < 0.1 LOOP
304 SELECT place_id, null, null, null,
305 ST_Distance(geometry, point) as distance, null as isguess
306 FROM location_road_-partition-
307 WHERE ST_DWithin(geometry, point, search_diameter)
308 ORDER BY distance ASC limit 1
313 search_diameter := search_diameter * 2;
319 RAISE EXCEPTION 'Unknown partition %', in_partition;
324 create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
326 r nearfeature%rowtype;
327 search_diameter FLOAT;
333 IF st_geometrytype(line) not in ('ST_LineString') THEN
337 p1 := ST_LineInterpolatePoint(line,0);
338 p2 := ST_LineInterpolatePoint(line,0.5);
339 p3 := ST_LineInterpolatePoint(line,1);
342 IF in_partition = -partition- THEN
343 search_diameter := 0.0005;
344 WHILE search_diameter < 0.01 LOOP
346 SELECT place_id, null, null, null,
347 ST_Distance(geometry, line) as distance, null as isguess
348 FROM location_road_-partition-
349 WHERE ST_DWithin(line, geometry, search_diameter)
350 ORDER BY (ST_distance(geometry, p1)+
351 ST_distance(geometry, p2)+
352 ST_distance(geometry, p3)) ASC limit 1
357 search_diameter := search_diameter * 2;
363 RAISE EXCEPTION 'Unknown partition %', in_partition;