1 create or replace function getNearFeatures(in_partition INTEGER, feature 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(feature, centroid)) as distance, isguess, centroid FROM (
10 SELECT * FROM location_area_large_-partition- WHERE ST_Intersects(geometry, feature) and rank_search < maxrank
12 SELECT * FROM location_area_country WHERE ST_Intersects(geometry, feature) 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(feature, 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 (partition, place_id, country_code, keywords, rank_search, rank_address, isguess, centroid, geometry)
69 values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, in_centroid, in_geometry);
74 IF in_partition = -partition- THEN
75 INSERT INTO location_area_large_-partition- (partition, place_id, country_code, keywords, rank_search, rank_address, isguess, centroid, geometry)
76 values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, in_centroid, in_geometry);
81 RAISE EXCEPTION 'Unknown partition %', in_partition;
87 create or replace function getNearestNamedFeature(in_partition INTEGER, point GEOMETRY, maxrank INTEGER, isin_token INTEGER) RETURNS setof nearfeature AS $$
89 r nearfeature%rowtype;
93 IF in_partition = -partition- THEN
95 SELECT place_id, name_vector, address_rank, search_rank,
96 ST_Distance(centroid, point) as distance, null as isguess
97 FROM search_name_-partition-
98 WHERE name_vector @> ARRAY[isin_token]
99 AND search_rank < maxrank
101 SELECT place_id, name_vector, address_rank, search_rank,
102 ST_Distance(centroid, point) as distance, null as isguess
103 FROM search_name_country
104 WHERE name_vector @> ARRAY[isin_token]
105 AND search_rank < maxrank
106 ORDER BY distance ASC limit 1
114 RAISE EXCEPTION 'Unknown partition %', in_partition;
119 create or replace function getNearestNamedRoadFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER[])
120 RETURNS setof nearfeature AS $$
122 r nearfeature%rowtype;
126 IF in_partition = -partition- THEN
128 SELECT place_id, name_vector, address_rank, search_rank,
129 ST_Distance(centroid, point) as distance, null as isguess
130 FROM search_name_-partition-
131 WHERE name_vector @> isin_token
132 AND ST_DWithin(centroid, point, 0.01)
133 AND search_rank between 26 and 27
134 ORDER BY distance ASC limit 1
142 RAISE EXCEPTION 'Unknown partition %', in_partition;
147 create or replace function getNearestNamedPlaceFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER[])
148 RETURNS setof nearfeature AS $$
150 r nearfeature%rowtype;
154 IF in_partition = -partition- THEN
156 SELECT place_id, name_vector, address_rank, search_rank,
157 ST_Distance(centroid, point) as distance, null as isguess
158 FROM search_name_-partition-
159 WHERE name_vector @> isin_token
160 AND ST_DWithin(centroid, point, 0.03)
161 AND search_rank between 16 and 22
162 ORDER BY distance ASC limit 1
170 RAISE EXCEPTION 'Unknown partition %', in_partition;
176 create or replace function getNearestPostcode(in_partition INTEGER, point GEOMETRY)
183 IF in_partition = -partition- THEN
185 FROM location_area_large_-partition- join placex using (place_id)
186 WHERE st_contains(location_area_large_-partition-.geometry, point)
187 AND class = 'place' and type = 'postcode'
188 ORDER BY st_distance(location_area_large_-partition-.centroid, point) ASC limit 1
194 RAISE EXCEPTION 'Unknown partition %', in_partition;
199 create or replace function insertSearchName(
200 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2),
201 in_name_vector INTEGER[], in_nameaddress_vector INTEGER[],
202 in_rank_search INTEGER, in_rank_address INTEGER, in_importance FLOAT,
203 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
207 DELETE FROM search_name WHERE place_id = in_place_id;
208 INSERT INTO search_name (place_id, search_rank, address_rank, importance, country_code, name_vector, nameaddress_vector, centroid)
209 values (in_place_id, in_rank_search, in_rank_address, in_importance, in_country_code, in_name_vector, in_nameaddress_vector, in_centroid);
211 IF in_rank_search <= 4 THEN
212 DELETE FROM search_name_country WHERE place_id = in_place_id;
213 IF in_rank_address > 0 THEN
214 INSERT INTO search_name_country (place_id, search_rank, address_rank, name_vector, centroid)
215 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
221 IF in_partition = -partition- THEN
222 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
223 IF in_rank_address > 0 THEN
224 INSERT INTO search_name_-partition- (place_id, search_rank, address_rank, name_vector, centroid)
225 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
231 RAISE EXCEPTION 'Unknown partition %', in_partition;
237 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
241 DELETE from search_name WHERE place_id = in_place_id;
242 DELETE from search_name_country WHERE place_id = in_place_id;
245 IF in_partition = -partition- THEN
246 DELETE from search_name_-partition- WHERE place_id = in_place_id;
251 RAISE EXCEPTION 'Unknown partition %', in_partition;
258 create or replace function insertLocationRoad(
259 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
264 IF in_partition = -partition- THEN
265 DELETE FROM location_road_-partition- where place_id = in_place_id;
266 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
267 values (in_partition, in_place_id, in_country_code, in_geometry);
272 RAISE EXCEPTION 'Unknown partition %', in_partition;
278 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
283 IF in_partition = -partition- THEN
284 DELETE FROM location_road_-partition- where place_id = in_place_id;
289 RAISE EXCEPTION 'Unknown partition %', in_partition;
296 create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
298 r nearfeature%rowtype;
299 search_diameter FLOAT;
303 IF in_partition = -partition- THEN
304 search_diameter := 0.00005;
305 WHILE search_diameter < 0.1 LOOP
307 SELECT place_id, null, null, null,
308 ST_Distance(geometry, point) as distance, null as isguess
309 FROM location_road_-partition-
310 WHERE ST_DWithin(geometry, point, search_diameter)
311 ORDER BY distance ASC limit 1
316 search_diameter := search_diameter * 2;
322 RAISE EXCEPTION 'Unknown partition %', in_partition;
327 create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
329 r nearfeature%rowtype;
330 search_diameter FLOAT;
336 IF st_geometrytype(line) not in ('ST_LineString') THEN
340 p1 := ST_LineInterpolatePoint(line,0);
341 p2 := ST_LineInterpolatePoint(line,0.5);
342 p3 := ST_LineInterpolatePoint(line,1);
345 IF in_partition = -partition- THEN
346 search_diameter := 0.0005;
347 WHILE search_diameter < 0.01 LOOP
349 SELECT place_id, null, null, null,
350 ST_Distance(geometry, line) as distance, null as isguess
351 FROM location_road_-partition-
352 WHERE ST_DWithin(line, geometry, search_diameter)
353 ORDER BY (ST_distance(geometry, p1)+
354 ST_distance(geometry, p2)+
355 ST_distance(geometry, p3)) ASC limit 1
360 search_diameter := search_diameter * 2;
366 RAISE EXCEPTION 'Unknown partition %', in_partition;