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)
24 AND rank_search < maxrank AND rank_address < maxrank
25 GROUP BY place_id, keywords, rank_address, rank_search, isguess, postcode, centroid
26 ORDER BY rank_address, isin_tokens && keywords desc, isguess asc,
27 ST_Distance(feature, centroid) *
29 WHEN rank_address = 16 AND rank_search = 15 THEN 0.2 -- capital city
30 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25 -- city
31 WHEN rank_address = 16 AND rank_search = 17 THEN 0.5 -- town
32 ELSE 1 END ASC -- everything else
40 RAISE EXCEPTION 'Unknown partition %', in_partition;
43 LANGUAGE plpgsql STABLE;
45 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT, in_rank_search INTEGER) RETURNS BOOLEAN AS $$
49 IF in_rank_search <= 4 THEN
50 DELETE from location_area_country WHERE place_id = in_place_id;
55 IF in_partition = -partition- THEN
56 DELETE from location_area_large_-partition- WHERE place_id = in_place_id;
61 RAISE EXCEPTION 'Unknown partition %', in_partition;
68 create or replace function insertLocationAreaLarge(
69 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_keywords INTEGER[],
70 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN, postcode TEXT,
71 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
74 IF in_rank_address = 0 THEN
78 IF in_rank_search <= 4 and not in_estimate THEN
79 INSERT INTO location_area_country (place_id, country_code, geometry)
80 values (in_place_id, in_country_code, in_geometry);
85 IF in_partition = -partition- THEN
86 INSERT INTO location_area_large_-partition- (partition, place_id, country_code, keywords, rank_search, rank_address, isguess, postcode, centroid, geometry)
87 values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, postcode, in_centroid, in_geometry);
92 RAISE EXCEPTION 'Unknown partition %', in_partition;
98 CREATE OR REPLACE FUNCTION getNearestNamedRoadPlaceId(in_partition INTEGER,
100 isin_token INTEGER[])
108 IF in_partition = -partition- THEN
109 SELECT place_id FROM search_name_-partition-
111 WHERE name_vector && isin_token
112 AND centroid && ST_Expand(point, 0.015)
113 AND search_rank between 26 and 27
114 ORDER BY ST_Distance(centroid, point) ASC limit 1;
119 RAISE EXCEPTION 'Unknown partition %', in_partition;
122 LANGUAGE plpgsql STABLE;
124 CREATE OR REPLACE FUNCTION getNearestNamedPlacePlaceId(in_partition INTEGER,
126 isin_token INTEGER[])
134 IF in_partition = -partition- THEN
137 FROM search_name_-partition-
138 WHERE name_vector && isin_token
139 AND centroid && ST_Expand(point, 0.04)
140 AND search_rank between 16 and 22
141 ORDER BY ST_Distance(centroid, point) ASC limit 1;
146 RAISE EXCEPTION 'Unknown partition %', in_partition;
149 LANGUAGE plpgsql STABLE;
152 create or replace function insertSearchName(
153 in_partition INTEGER, in_place_id BIGINT, in_name_vector INTEGER[],
154 in_rank_search INTEGER, in_rank_address INTEGER, in_geometry GEOMETRY)
155 RETURNS BOOLEAN AS $$
159 IF in_partition = -partition- THEN
160 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
161 IF in_rank_address > 0 THEN
162 INSERT INTO search_name_-partition- (place_id, search_rank, address_rank, name_vector, centroid)
163 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
169 RAISE EXCEPTION 'Unknown partition %', in_partition;
175 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
179 IF in_partition = -partition- THEN
180 DELETE from search_name_-partition- WHERE place_id = in_place_id;
185 RAISE EXCEPTION 'Unknown partition %', in_partition;
192 create or replace function insertLocationRoad(
193 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
198 IF in_partition = -partition- THEN
199 DELETE FROM location_road_-partition- where place_id = in_place_id;
200 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
201 values (in_partition, in_place_id, in_country_code, in_geometry);
206 RAISE EXCEPTION 'Unknown partition %', in_partition;
212 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
217 IF in_partition = -partition- THEN
218 DELETE FROM location_road_-partition- where place_id = in_place_id;
223 RAISE EXCEPTION 'Unknown partition %', in_partition;
230 CREATE OR REPLACE FUNCTION getNearestRoadPlaceId(in_partition INTEGER, point GEOMETRY)
235 search_diameter FLOAT;
239 IF in_partition = -partition- THEN
240 search_diameter := 0.00005;
241 WHILE search_diameter < 0.1 LOOP
243 SELECT place_id FROM location_road_-partition-
244 WHERE ST_DWithin(geometry, point, search_diameter)
245 ORDER BY ST_Distance(geometry, point) ASC limit 1
249 search_diameter := search_diameter * 2;
255 RAISE EXCEPTION 'Unknown partition %', in_partition;
258 LANGUAGE plpgsql STABLE;
260 CREATE OR REPLACE FUNCTION getNearestParallelRoadFeature(in_partition INTEGER,
266 search_diameter FLOAT;
272 IF ST_GeometryType(line) not in ('ST_LineString') THEN
276 p1 := ST_LineInterpolatePoint(line,0);
277 p2 := ST_LineInterpolatePoint(line,0.5);
278 p3 := ST_LineInterpolatePoint(line,1);
281 IF in_partition = -partition- THEN
282 search_diameter := 0.0005;
283 WHILE search_diameter < 0.01 LOOP
285 SELECT place_id FROM location_road_-partition-
286 WHERE ST_DWithin(line, geometry, search_diameter)
287 ORDER BY (ST_distance(geometry, p1)+
288 ST_distance(geometry, p2)+
289 ST_distance(geometry, p3)) ASC limit 1
293 search_diameter := search_diameter * 2;
299 RAISE EXCEPTION 'Unknown partition %', in_partition;
302 LANGUAGE plpgsql STABLE;