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 getNearestNamedRoadFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER[])
88 RETURNS setof nearfeature AS $$
90 r nearfeature%rowtype;
94 IF in_partition = -partition- THEN
96 SELECT place_id, name_vector, address_rank, search_rank,
97 ST_Distance(centroid, point) as distance, null as isguess
98 FROM search_name_-partition-
99 WHERE name_vector @> isin_token
100 AND ST_DWithin(centroid, point, 0.015)
101 AND search_rank between 26 and 27
102 ORDER BY distance ASC limit 1
110 RAISE EXCEPTION 'Unknown partition %', in_partition;
115 create or replace function getNearestNamedPlaceFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER[])
116 RETURNS setof nearfeature AS $$
118 r nearfeature%rowtype;
122 IF in_partition = -partition- THEN
124 SELECT place_id, name_vector, address_rank, search_rank,
125 ST_Distance(centroid, point) as distance, null as isguess
126 FROM search_name_-partition-
127 WHERE name_vector @> isin_token
128 AND ST_DWithin(centroid, point, 0.03)
129 AND search_rank between 16 and 22
130 ORDER BY distance ASC limit 1
138 RAISE EXCEPTION 'Unknown partition %', in_partition;
144 create or replace function insertSearchName(
145 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2),
146 in_name_vector INTEGER[], in_nameaddress_vector INTEGER[],
147 in_rank_search INTEGER, in_rank_address INTEGER, in_importance FLOAT,
148 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
152 DELETE FROM search_name WHERE place_id = in_place_id;
153 INSERT INTO search_name (place_id, search_rank, address_rank, importance, country_code, name_vector, nameaddress_vector, centroid)
154 values (in_place_id, in_rank_search, in_rank_address, in_importance, in_country_code, in_name_vector, in_nameaddress_vector, in_centroid);
156 IF in_rank_search <= 4 THEN
157 DELETE FROM search_name_country WHERE place_id = in_place_id;
158 IF in_rank_address > 0 THEN
159 INSERT INTO search_name_country (place_id, search_rank, address_rank, name_vector, centroid)
160 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
166 IF in_partition = -partition- THEN
167 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
168 IF in_rank_address > 0 THEN
169 INSERT INTO search_name_-partition- (place_id, search_rank, address_rank, name_vector, centroid)
170 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
176 RAISE EXCEPTION 'Unknown partition %', in_partition;
182 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
186 DELETE from search_name WHERE place_id = in_place_id;
187 DELETE from search_name_country WHERE place_id = in_place_id;
190 IF in_partition = -partition- THEN
191 DELETE from search_name_-partition- WHERE place_id = in_place_id;
196 RAISE EXCEPTION 'Unknown partition %', in_partition;
203 create or replace function insertLocationRoad(
204 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
209 IF in_partition = -partition- THEN
210 DELETE FROM location_road_-partition- where place_id = in_place_id;
211 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
212 values (in_partition, in_place_id, in_country_code, in_geometry);
217 RAISE EXCEPTION 'Unknown partition %', in_partition;
223 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
228 IF in_partition = -partition- THEN
229 DELETE FROM location_road_-partition- where place_id = in_place_id;
234 RAISE EXCEPTION 'Unknown partition %', in_partition;
241 create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
243 r nearfeature%rowtype;
244 search_diameter FLOAT;
248 IF in_partition = -partition- THEN
249 search_diameter := 0.00005;
250 WHILE search_diameter < 0.1 LOOP
252 SELECT place_id, null, null, null,
253 ST_Distance(geometry, point) as distance, null as isguess
254 FROM location_road_-partition-
255 WHERE ST_DWithin(geometry, point, search_diameter)
256 ORDER BY distance ASC limit 1
261 search_diameter := search_diameter * 2;
267 RAISE EXCEPTION 'Unknown partition %', in_partition;
272 create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
274 r nearfeature%rowtype;
275 search_diameter FLOAT;
281 IF st_geometrytype(line) not in ('ST_LineString') THEN
285 p1 := ST_LineInterpolatePoint(line,0);
286 p2 := ST_LineInterpolatePoint(line,0.5);
287 p3 := ST_LineInterpolatePoint(line,1);
290 IF in_partition = -partition- THEN
291 search_diameter := 0.0005;
292 WHILE search_diameter < 0.01 LOOP
294 SELECT place_id, null, null, null,
295 ST_Distance(geometry, line) as distance, null as isguess
296 FROM location_road_-partition-
297 WHERE ST_DWithin(line, geometry, search_diameter)
298 ORDER BY (ST_distance(geometry, p1)+
299 ST_distance(geometry, p2)+
300 ST_distance(geometry, p3)) ASC limit 1
305 search_diameter := search_diameter * 2;
311 RAISE EXCEPTION 'Unknown partition %', in_partition;