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.04)
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_name_vector INTEGER[],
146 in_rank_search INTEGER, in_rank_address INTEGER, in_geometry GEOMETRY)
147 RETURNS BOOLEAN AS $$
151 IF in_partition = -partition- THEN
152 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
153 IF in_rank_address > 0 THEN
154 INSERT INTO search_name_-partition- (place_id, search_rank, address_rank, name_vector, centroid)
155 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
161 RAISE EXCEPTION 'Unknown partition %', in_partition;
167 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
171 IF in_partition = -partition- THEN
172 DELETE from search_name_-partition- WHERE place_id = in_place_id;
177 RAISE EXCEPTION 'Unknown partition %', in_partition;
184 create or replace function insertLocationRoad(
185 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
190 IF in_partition = -partition- THEN
191 DELETE FROM location_road_-partition- where place_id = in_place_id;
192 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
193 values (in_partition, in_place_id, in_country_code, in_geometry);
198 RAISE EXCEPTION 'Unknown partition %', in_partition;
204 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
209 IF in_partition = -partition- THEN
210 DELETE FROM location_road_-partition- where place_id = in_place_id;
215 RAISE EXCEPTION 'Unknown partition %', in_partition;
222 create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
224 r nearfeature%rowtype;
225 search_diameter FLOAT;
229 IF in_partition = -partition- THEN
230 search_diameter := 0.00005;
231 WHILE search_diameter < 0.1 LOOP
233 SELECT place_id, null, null, null,
234 ST_Distance(geometry, point) as distance, null as isguess
235 FROM location_road_-partition-
236 WHERE ST_DWithin(geometry, point, search_diameter)
237 ORDER BY distance ASC limit 1
242 search_diameter := search_diameter * 2;
248 RAISE EXCEPTION 'Unknown partition %', in_partition;
253 create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
255 r nearfeature%rowtype;
256 search_diameter FLOAT;
262 IF st_geometrytype(line) not in ('ST_LineString') THEN
266 p1 := ST_LineInterpolatePoint(line,0);
267 p2 := ST_LineInterpolatePoint(line,0.5);
268 p3 := ST_LineInterpolatePoint(line,1);
271 IF in_partition = -partition- THEN
272 search_diameter := 0.0005;
273 WHILE search_diameter < 0.01 LOOP
275 SELECT place_id, null, null, null,
276 ST_Distance(geometry, line) as distance, null as isguess
277 FROM location_road_-partition-
278 WHERE ST_DWithin(line, geometry, search_diameter)
279 ORDER BY (ST_distance(geometry, p1)+
280 ST_distance(geometry, p2)+
281 ST_distance(geometry, p3)) ASC limit 1
286 search_diameter := search_diameter * 2;
292 RAISE EXCEPTION 'Unknown partition %', in_partition;