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
10 FROM location_area_large_-partition-
11 WHERE ST_Intersects(geometry, feature) and rank_search < maxrank
12 GROUP BY place_id, keywords, rank_address, rank_search, isguess, postcode, centroid
13 ORDER BY rank_address, isin_tokens && keywords desc, isguess asc,
14 ST_Distance(feature, centroid) *
16 WHEN rank_address = 16 AND rank_search = 15 THEN 0.2 -- capital city
17 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25 -- city
18 WHEN rank_address = 16 AND rank_search = 17 THEN 0.5 -- town
19 ELSE 1 END ASC -- everything else
27 RAISE EXCEPTION 'Unknown partition %', in_partition;
32 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT, in_rank_search INTEGER) RETURNS BOOLEAN AS $$
36 IF in_rank_search <= 4 THEN
37 DELETE from location_area_country WHERE place_id = in_place_id;
42 IF in_partition = -partition- THEN
43 DELETE from location_area_large_-partition- WHERE place_id = in_place_id;
48 RAISE EXCEPTION 'Unknown partition %', in_partition;
55 create or replace function insertLocationAreaLarge(
56 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_keywords INTEGER[],
57 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN, postcode TEXT,
58 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
61 IF in_rank_address = 0 THEN
65 IF in_rank_search <= 4 and not in_estimate THEN
66 INSERT INTO location_area_country (place_id, country_code, geometry)
67 values (in_place_id, in_country_code, in_geometry);
72 IF in_partition = -partition- THEN
73 INSERT INTO location_area_large_-partition- (partition, place_id, country_code, keywords, rank_search, rank_address, isguess, postcode, centroid, geometry)
74 values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, postcode, in_centroid, in_geometry);
79 RAISE EXCEPTION 'Unknown partition %', in_partition;
85 create or replace function getNearestNamedRoadFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER[])
86 RETURNS setof nearfeature AS $$
88 r nearfeature%rowtype;
92 IF in_partition = -partition- THEN
94 SELECT place_id, name_vector, address_rank, search_rank,
95 ST_Distance(centroid, point) as distance, null as isguess
96 FROM search_name_-partition-
97 WHERE name_vector && isin_token
98 AND centroid && ST_Expand(point, 0.015)
99 AND search_rank between 26 and 27
100 ORDER BY distance ASC limit 1
108 RAISE EXCEPTION 'Unknown partition %', in_partition;
113 create or replace function getNearestNamedPlaceFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER[])
114 RETURNS setof nearfeature AS $$
116 r nearfeature%rowtype;
120 IF in_partition = -partition- THEN
122 SELECT place_id, name_vector, address_rank, search_rank,
123 ST_Distance(centroid, point) as distance, null as isguess
124 FROM search_name_-partition-
125 WHERE name_vector && isin_token
126 AND centroid && ST_Expand(point, 0.04)
127 AND search_rank between 16 and 22
128 ORDER BY distance ASC limit 1
136 RAISE EXCEPTION 'Unknown partition %', in_partition;
142 create or replace function insertSearchName(
143 in_partition INTEGER, in_place_id BIGINT, in_name_vector INTEGER[],
144 in_rank_search INTEGER, in_rank_address INTEGER, in_geometry GEOMETRY)
145 RETURNS BOOLEAN AS $$
149 IF in_partition = -partition- THEN
150 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
151 IF in_rank_address > 0 THEN
152 INSERT INTO search_name_-partition- (place_id, search_rank, address_rank, name_vector, centroid)
153 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
159 RAISE EXCEPTION 'Unknown partition %', in_partition;
165 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
169 IF in_partition = -partition- THEN
170 DELETE from search_name_-partition- WHERE place_id = in_place_id;
175 RAISE EXCEPTION 'Unknown partition %', in_partition;
182 create or replace function insertLocationRoad(
183 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
188 IF in_partition = -partition- THEN
189 DELETE FROM location_road_-partition- where place_id = in_place_id;
190 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
191 values (in_partition, in_place_id, in_country_code, in_geometry);
196 RAISE EXCEPTION 'Unknown partition %', in_partition;
202 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
207 IF in_partition = -partition- THEN
208 DELETE FROM location_road_-partition- where place_id = in_place_id;
213 RAISE EXCEPTION 'Unknown partition %', in_partition;
220 create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
222 r nearfeature%rowtype;
223 search_diameter FLOAT;
227 IF in_partition = -partition- THEN
228 search_diameter := 0.00005;
229 WHILE search_diameter < 0.1 LOOP
231 SELECT place_id, null, null, null,
232 ST_Distance(geometry, point) as distance, null as isguess
233 FROM location_road_-partition-
234 WHERE ST_DWithin(geometry, point, search_diameter)
235 ORDER BY distance ASC limit 1
240 search_diameter := search_diameter * 2;
246 RAISE EXCEPTION 'Unknown partition %', in_partition;
251 create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
253 r nearfeature%rowtype;
254 search_diameter FLOAT;
260 IF st_geometrytype(line) not in ('ST_LineString') THEN
264 p1 := ST_LineInterpolatePoint(line,0);
265 p2 := ST_LineInterpolatePoint(line,0.5);
266 p3 := ST_LineInterpolatePoint(line,1);
269 IF in_partition = -partition- THEN
270 search_diameter := 0.0005;
271 WHILE search_diameter < 0.01 LOOP
273 SELECT place_id, null, null, null,
274 ST_Distance(geometry, line) as distance, null as isguess
275 FROM location_road_-partition-
276 WHERE ST_DWithin(line, geometry, search_diameter)
277 ORDER BY (ST_distance(geometry, p1)+
278 ST_distance(geometry, p2)+
279 ST_distance(geometry, p3)) ASC limit 1
284 search_diameter := search_diameter * 2;
290 RAISE EXCEPTION 'Unknown partition %', in_partition;