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.03)
128 AND search_rank between 22 and 27
129 ORDER BY distance ASC limit 1
137 RAISE EXCEPTION 'Unknown partition %', in_partition;
142 create or replace function getNearestPostcode(in_partition INTEGER, point GEOMETRY)
149 IF in_partition = -partition- THEN
151 FROM location_area_large_-partition- join placex using (place_id)
152 WHERE st_contains(location_area_large_-partition-.geometry, point)
153 AND class = 'place' and type = 'postcode'
154 ORDER BY st_distance(location_area_large_-partition-.centroid, point) ASC limit 1
160 RAISE EXCEPTION 'Unknown partition %', in_partition;
165 create or replace function insertSearchName(
166 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2),
167 in_name_vector INTEGER[], in_nameaddress_vector INTEGER[],
168 in_rank_search INTEGER, in_rank_address INTEGER, in_importance FLOAT,
169 in_centroid GEOMETRY) RETURNS BOOLEAN AS $$
173 DELETE FROM search_name WHERE place_id = in_place_id;
174 INSERT INTO search_name values (in_place_id, in_rank_search, in_rank_address, in_importance, in_country_code,
175 in_name_vector, in_nameaddress_vector, in_centroid);
177 IF in_rank_search <= 4 THEN
178 DELETE FROM search_name_country WHERE place_id = in_place_id;
179 INSERT INTO search_name_country values (in_place_id, in_rank_search, in_rank_address, in_importance, in_country_code,
180 in_name_vector, in_nameaddress_vector, in_centroid);
185 IF in_partition = -partition- THEN
186 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
187 INSERT INTO search_name_-partition- values (in_place_id, in_rank_search, in_rank_address, 0, in_country_code,
188 in_name_vector, in_nameaddress_vector, in_centroid);
193 RAISE EXCEPTION 'Unknown partition %', in_partition;
199 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
203 DELETE from search_name WHERE place_id = in_place_id;
204 DELETE from search_name_country WHERE place_id = in_place_id;
207 IF in_partition = -partition- THEN
208 DELETE from search_name_-partition- WHERE place_id = in_place_id;
213 RAISE EXCEPTION 'Unknown partition %', in_partition;
220 create or replace function insertLocationRoad(
221 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
226 IF in_partition = -partition- THEN
227 DELETE FROM location_road_-partition- where place_id = in_place_id;
228 INSERT INTO location_road_-partition- values (in_partition, in_place_id, in_country_code, in_geometry);
233 RAISE EXCEPTION 'Unknown partition %', in_partition;
239 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
244 IF in_partition = -partition- THEN
245 DELETE FROM location_road_-partition- where place_id = in_place_id;
250 RAISE EXCEPTION 'Unknown partition %', in_partition;
257 create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
259 r nearfeature%rowtype;
260 search_diameter FLOAT;
264 IF in_partition = -partition- THEN
265 search_diameter := 0.00005;
266 WHILE search_diameter < 0.1 LOOP
268 SELECT place_id, null, null, null,
269 ST_Distance(geometry, point) as distance, null as isguess
270 FROM location_road_-partition-
271 WHERE ST_DWithin(geometry, point, search_diameter)
272 ORDER BY distance ASC limit 1
277 search_diameter := search_diameter * 2;
283 RAISE EXCEPTION 'Unknown partition %', in_partition;
288 create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
290 r nearfeature%rowtype;
291 search_diameter FLOAT;
297 IF st_geometrytype(line) not in ('ST_LineString') THEN
301 p1 := ST_Line_Interpolate_Point(line,0);
302 p2 := ST_Line_Interpolate_Point(line,0.5);
303 p3 := ST_Line_Interpolate_Point(line,1);
306 IF in_partition = -partition- THEN
307 search_diameter := 0.0005;
308 WHILE search_diameter < 0.01 LOOP
310 SELECT place_id, null, null, null,
311 ST_Distance(geometry, line) as distance, null as isguess
312 FROM location_road_-partition-
313 WHERE ST_DWithin(line, geometry, search_diameter)
314 ORDER BY (ST_distance(geometry, p1)+
315 ST_distance(geometry, p2)+
316 ST_distance(geometry, p3)) ASC limit 1
321 search_diameter := search_diameter * 2;
327 RAISE EXCEPTION 'Unknown partition %', in_partition;