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 getNearestNamedRoadPlaceId(in_partition INTEGER,
95 IF in_partition = -partition- THEN
96 SELECT place_id FROM search_name_-partition-
98 WHERE name_vector && isin_token
99 AND centroid && ST_Expand(point, 0.015)
100 AND search_rank between 26 and 27
101 ORDER BY ST_Distance(centroid, point) ASC limit 1;
106 RAISE EXCEPTION 'Unknown partition %', in_partition;
109 LANGUAGE plpgsql STABLE;
111 CREATE OR REPLACE FUNCTION getNearestNamedPlacePlaceId(in_partition INTEGER,
113 isin_token INTEGER[])
121 IF in_partition = -partition- THEN
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 ST_Distance(centroid, point) ASC limit 1;
133 RAISE EXCEPTION 'Unknown partition %', in_partition;
139 create or replace function insertSearchName(
140 in_partition INTEGER, in_place_id BIGINT, in_name_vector INTEGER[],
141 in_rank_search INTEGER, in_rank_address INTEGER, in_geometry GEOMETRY)
142 RETURNS BOOLEAN AS $$
146 IF in_partition = -partition- THEN
147 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
148 IF in_rank_address > 0 THEN
149 INSERT INTO search_name_-partition- (place_id, search_rank, address_rank, name_vector, centroid)
150 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
156 RAISE EXCEPTION 'Unknown partition %', in_partition;
162 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
166 IF in_partition = -partition- THEN
167 DELETE from search_name_-partition- WHERE place_id = in_place_id;
172 RAISE EXCEPTION 'Unknown partition %', in_partition;
179 create or replace function insertLocationRoad(
180 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
185 IF in_partition = -partition- THEN
186 DELETE FROM location_road_-partition- where place_id = in_place_id;
187 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
188 values (in_partition, in_place_id, in_country_code, in_geometry);
193 RAISE EXCEPTION 'Unknown partition %', in_partition;
199 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
204 IF in_partition = -partition- THEN
205 DELETE FROM location_road_-partition- where place_id = in_place_id;
210 RAISE EXCEPTION 'Unknown partition %', in_partition;
217 create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
219 r nearfeature%rowtype;
220 search_diameter FLOAT;
224 IF in_partition = -partition- THEN
225 search_diameter := 0.00005;
226 WHILE search_diameter < 0.1 LOOP
228 SELECT place_id, null, null, null,
229 ST_Distance(geometry, point) as distance, null as isguess
230 FROM location_road_-partition-
231 WHERE ST_DWithin(geometry, point, search_diameter)
232 ORDER BY distance ASC limit 1
237 search_diameter := search_diameter * 2;
243 RAISE EXCEPTION 'Unknown partition %', in_partition;
248 create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
250 r nearfeature%rowtype;
251 search_diameter FLOAT;
257 IF st_geometrytype(line) not in ('ST_LineString') THEN
261 p1 := ST_LineInterpolatePoint(line,0);
262 p2 := ST_LineInterpolatePoint(line,0.5);
263 p3 := ST_LineInterpolatePoint(line,1);
266 IF in_partition = -partition- THEN
267 search_diameter := 0.0005;
268 WHILE search_diameter < 0.01 LOOP
270 SELECT place_id, null, null, null,
271 ST_Distance(geometry, line) as distance, null as isguess
272 FROM location_road_-partition-
273 WHERE ST_DWithin(line, geometry, search_diameter)
274 ORDER BY (ST_distance(geometry, p1)+
275 ST_distance(geometry, p2)+
276 ST_distance(geometry, p3)) ASC limit 1
281 search_diameter := search_diameter * 2;
287 RAISE EXCEPTION 'Unknown partition %', in_partition;