1 DROP TYPE IF EXISTS nearfeaturecentr CASCADE;
2 CREATE TYPE nearfeaturecentr AS (
13 create or replace function getNearFeatures(in_partition INTEGER, feature GEOMETRY, maxrank INTEGER, isin_tokens INT[]) RETURNS setof nearfeaturecentr AS $$
15 r nearfeaturecentr%rowtype;
19 IF in_partition = -partition- THEN
21 SELECT place_id, keywords, rank_address, rank_search, min(ST_Distance(feature, centroid)) as distance, isguess, postcode, centroid
22 FROM location_area_large_-partition-
23 WHERE ST_Intersects(geometry, feature) and rank_search < maxrank
24 GROUP BY place_id, keywords, rank_address, rank_search, isguess, postcode, centroid
25 ORDER BY rank_address, isin_tokens && keywords desc, isguess asc,
26 ST_Distance(feature, centroid) *
28 WHEN rank_address = 16 AND rank_search = 15 THEN 0.2 -- capital city
29 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25 -- city
30 WHEN rank_address = 16 AND rank_search = 17 THEN 0.5 -- town
31 ELSE 1 END ASC -- everything else
39 RAISE EXCEPTION 'Unknown partition %', in_partition;
42 LANGUAGE plpgsql STABLE;
44 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT, in_rank_search INTEGER) RETURNS BOOLEAN AS $$
48 IF in_rank_search <= 4 THEN
49 DELETE from location_area_country WHERE place_id = in_place_id;
54 IF in_partition = -partition- THEN
55 DELETE from location_area_large_-partition- WHERE place_id = in_place_id;
60 RAISE EXCEPTION 'Unknown partition %', in_partition;
67 create or replace function insertLocationAreaLarge(
68 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_keywords INTEGER[],
69 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN, postcode TEXT,
70 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
73 IF in_rank_address = 0 THEN
77 IF in_rank_search <= 4 and not in_estimate THEN
78 INSERT INTO location_area_country (place_id, country_code, geometry)
79 values (in_place_id, in_country_code, in_geometry);
84 IF in_partition = -partition- THEN
85 INSERT INTO location_area_large_-partition- (partition, place_id, country_code, keywords, rank_search, rank_address, isguess, postcode, centroid, geometry)
86 values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, postcode, in_centroid, in_geometry);
91 RAISE EXCEPTION 'Unknown partition %', in_partition;
97 CREATE OR REPLACE FUNCTION getNearestNamedRoadPlaceId(in_partition INTEGER,
107 IF in_partition = -partition- THEN
108 SELECT place_id FROM search_name_-partition-
110 WHERE name_vector && isin_token
111 AND centroid && ST_Expand(point, 0.015)
112 AND search_rank between 26 and 27
113 ORDER BY ST_Distance(centroid, point) ASC limit 1;
118 RAISE EXCEPTION 'Unknown partition %', in_partition;
121 LANGUAGE plpgsql STABLE;
123 CREATE OR REPLACE FUNCTION getNearestNamedPlacePlaceId(in_partition INTEGER,
125 isin_token INTEGER[])
133 IF in_partition = -partition- THEN
136 FROM search_name_-partition-
137 WHERE name_vector && isin_token
138 AND centroid && ST_Expand(point, 0.04)
139 AND search_rank between 16 and 22
140 ORDER BY ST_Distance(centroid, point) ASC limit 1;
145 RAISE EXCEPTION 'Unknown partition %', in_partition;
148 LANGUAGE plpgsql STABLE;
151 create or replace function insertSearchName(
152 in_partition INTEGER, in_place_id BIGINT, in_name_vector INTEGER[],
153 in_rank_search INTEGER, in_rank_address INTEGER, in_geometry GEOMETRY)
154 RETURNS BOOLEAN AS $$
158 IF in_partition = -partition- THEN
159 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
160 IF in_rank_address > 0 THEN
161 INSERT INTO search_name_-partition- (place_id, search_rank, address_rank, name_vector, centroid)
162 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
168 RAISE EXCEPTION 'Unknown partition %', in_partition;
174 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
178 IF in_partition = -partition- THEN
179 DELETE from search_name_-partition- WHERE place_id = in_place_id;
184 RAISE EXCEPTION 'Unknown partition %', in_partition;
191 create or replace function insertLocationRoad(
192 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
197 IF in_partition = -partition- THEN
198 DELETE FROM location_road_-partition- where place_id = in_place_id;
199 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
200 values (in_partition, in_place_id, in_country_code, in_geometry);
205 RAISE EXCEPTION 'Unknown partition %', in_partition;
211 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
216 IF in_partition = -partition- THEN
217 DELETE FROM location_road_-partition- where place_id = in_place_id;
222 RAISE EXCEPTION 'Unknown partition %', in_partition;
229 CREATE OR REPLACE FUNCTION getNearestRoadPlaceId(in_partition INTEGER, point GEOMETRY)
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 FROM location_road_-partition-
243 WHERE ST_DWithin(geometry, point, search_diameter)
244 ORDER BY ST_Distance(geometry, point) ASC limit 1
248 search_diameter := search_diameter * 2;
254 RAISE EXCEPTION 'Unknown partition %', in_partition;
257 LANGUAGE plpgsql STABLE;
259 CREATE OR REPLACE FUNCTION getNearestParallelRoadFeature(in_partition INTEGER,
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 FROM location_road_-partition-
285 WHERE ST_DWithin(line, geometry, search_diameter)
286 ORDER BY (ST_distance(geometry, p1)+
287 ST_distance(geometry, p2)+
288 ST_distance(geometry, p3)) ASC limit 1
292 search_diameter := search_diameter * 2;
298 RAISE EXCEPTION 'Unknown partition %', in_partition;
301 LANGUAGE plpgsql STABLE;