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, postcode, 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, postcode, 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, postcode TEXT,
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, postcode, centroid, geometry)
76 values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, postcode, 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.015)
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 insertSearchName(
177 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2),
178 in_name_vector INTEGER[], in_nameaddress_vector INTEGER[],
179 in_rank_search INTEGER, in_rank_address INTEGER, in_importance FLOAT,
180 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
184 DELETE FROM search_name WHERE place_id = in_place_id;
185 INSERT INTO search_name (place_id, search_rank, address_rank, importance, country_code, name_vector, nameaddress_vector, centroid)
186 values (in_place_id, in_rank_search, in_rank_address, in_importance, in_country_code, in_name_vector, in_nameaddress_vector, in_centroid);
188 IF in_rank_search <= 4 THEN
189 DELETE FROM search_name_country WHERE place_id = in_place_id;
190 IF in_rank_address > 0 THEN
191 INSERT INTO search_name_country (place_id, search_rank, address_rank, name_vector, centroid)
192 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
198 IF in_partition = -partition- THEN
199 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
200 IF in_rank_address > 0 THEN
201 INSERT INTO search_name_-partition- (place_id, search_rank, address_rank, name_vector, centroid)
202 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
208 RAISE EXCEPTION 'Unknown partition %', in_partition;
214 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
218 DELETE from search_name WHERE place_id = in_place_id;
219 DELETE from search_name_country WHERE place_id = in_place_id;
222 IF in_partition = -partition- THEN
223 DELETE from search_name_-partition- WHERE place_id = in_place_id;
228 RAISE EXCEPTION 'Unknown partition %', in_partition;
235 create or replace function insertLocationRoad(
236 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
241 IF in_partition = -partition- THEN
242 DELETE FROM location_road_-partition- where place_id = in_place_id;
243 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
244 values (in_partition, in_place_id, in_country_code, in_geometry);
249 RAISE EXCEPTION 'Unknown partition %', in_partition;
255 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
260 IF in_partition = -partition- THEN
261 DELETE FROM location_road_-partition- where place_id = in_place_id;
266 RAISE EXCEPTION 'Unknown partition %', in_partition;
273 create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
275 r nearfeature%rowtype;
276 search_diameter FLOAT;
280 IF in_partition = -partition- THEN
281 search_diameter := 0.00005;
282 WHILE search_diameter < 0.1 LOOP
284 SELECT place_id, null, null, null,
285 ST_Distance(geometry, point) as distance, null as isguess
286 FROM location_road_-partition-
287 WHERE ST_DWithin(geometry, point, search_diameter)
288 ORDER BY distance ASC limit 1
293 search_diameter := search_diameter * 2;
299 RAISE EXCEPTION 'Unknown partition %', in_partition;
304 create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
306 r nearfeature%rowtype;
307 search_diameter FLOAT;
313 IF st_geometrytype(line) not in ('ST_LineString') THEN
317 p1 := ST_LineInterpolatePoint(line,0);
318 p2 := ST_LineInterpolatePoint(line,0.5);
319 p3 := ST_LineInterpolatePoint(line,1);
322 IF in_partition = -partition- THEN
323 search_diameter := 0.0005;
324 WHILE search_diameter < 0.01 LOOP
326 SELECT place_id, null, null, null,
327 ST_Distance(geometry, line) as distance, null as isguess
328 FROM location_road_-partition-
329 WHERE ST_DWithin(line, geometry, search_diameter)
330 ORDER BY (ST_distance(geometry, p1)+
331 ST_distance(geometry, p2)+
332 ST_distance(geometry, p3)) ASC limit 1
337 search_diameter := search_diameter * 2;
343 RAISE EXCEPTION 'Unknown partition %', in_partition;