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);
157 IF in_partition = -partition- THEN
158 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
159 IF in_rank_address > 0 THEN
160 INSERT INTO search_name_-partition- (place_id, search_rank, address_rank, name_vector, centroid)
161 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
167 RAISE EXCEPTION 'Unknown partition %', in_partition;
173 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
177 DELETE from search_name WHERE place_id = in_place_id;
180 IF in_partition = -partition- THEN
181 DELETE from search_name_-partition- WHERE place_id = in_place_id;
186 RAISE EXCEPTION 'Unknown partition %', in_partition;
193 create or replace function insertLocationRoad(
194 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
199 IF in_partition = -partition- THEN
200 DELETE FROM location_road_-partition- where place_id = in_place_id;
201 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
202 values (in_partition, in_place_id, in_country_code, in_geometry);
207 RAISE EXCEPTION 'Unknown partition %', in_partition;
213 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
218 IF in_partition = -partition- THEN
219 DELETE FROM location_road_-partition- where place_id = in_place_id;
224 RAISE EXCEPTION 'Unknown partition %', in_partition;
231 create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
233 r nearfeature%rowtype;
234 search_diameter FLOAT;
238 IF in_partition = -partition- THEN
239 search_diameter := 0.00005;
240 WHILE search_diameter < 0.1 LOOP
242 SELECT place_id, null, null, null,
243 ST_Distance(geometry, point) as distance, null as isguess
244 FROM location_road_-partition-
245 WHERE ST_DWithin(geometry, point, search_diameter)
246 ORDER BY distance ASC limit 1
251 search_diameter := search_diameter * 2;
257 RAISE EXCEPTION 'Unknown partition %', in_partition;
262 create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
264 r nearfeature%rowtype;
265 search_diameter FLOAT;
271 IF st_geometrytype(line) not in ('ST_LineString') THEN
275 p1 := ST_LineInterpolatePoint(line,0);
276 p2 := ST_LineInterpolatePoint(line,0.5);
277 p3 := ST_LineInterpolatePoint(line,1);
280 IF in_partition = -partition- THEN
281 search_diameter := 0.0005;
282 WHILE search_diameter < 0.01 LOOP
284 SELECT place_id, null, null, null,
285 ST_Distance(geometry, line) as distance, null as isguess
286 FROM location_road_-partition-
287 WHERE ST_DWithin(line, geometry, search_diameter)
288 ORDER BY (ST_distance(geometry, p1)+
289 ST_distance(geometry, p2)+
290 ST_distance(geometry, p3)) ASC limit 1
295 search_diameter := search_diameter * 2;
301 RAISE EXCEPTION 'Unknown partition %', in_partition;