1 DROP TYPE IF EXISTS nearfeaturecentr CASCADE;
2 CREATE TYPE nearfeaturecentr AS (
13 -- feature intersects geoemtry
14 -- for areas and linestrings they must touch at least along a line
15 CREATE OR REPLACE FUNCTION is_relevant_geometry(de9im TEXT, geom_type TEXT)
19 IF substring(de9im from 1 for 2) != 'FF' THEN
23 IF geom_type = 'ST_Point' THEN
24 RETURN substring(de9im from 4 for 1) = '0';
27 IF geom_type in ('ST_LineString', 'ST_MultiLineString') THEN
28 RETURN substring(de9im from 4 for 1) = '1';
31 RETURN substring(de9im from 4 for 1) = '2';
33 $$ LANGUAGE plpgsql IMMUTABLE;
35 create or replace function getNearFeatures(in_partition INTEGER, feature GEOMETRY, maxrank INTEGER) RETURNS setof nearfeaturecentr AS $$
37 r nearfeaturecentr%rowtype;
41 IF in_partition = -partition- THEN
43 SELECT place_id, keywords, rank_address, rank_search,
44 min(ST_Distance(feature, centroid)) as distance,
45 isguess, postcode, centroid
46 FROM location_area_large_-partition-
47 WHERE geometry && feature
48 AND is_relevant_geometry(ST_Relate(geometry, feature), ST_GeometryType(feature))
49 AND rank_address < maxrank
50 -- Postcodes currently still use rank_search to define for which
51 -- features they are relevant.
52 AND not (rank_address in (5, 11) and rank_search > maxrank)
53 GROUP BY place_id, keywords, rank_address, rank_search, isguess, postcode, centroid
61 RAISE EXCEPTION 'Unknown partition %', in_partition;
64 LANGUAGE plpgsql STABLE;
66 CREATE OR REPLACE FUNCTION get_places_for_addr_tags(in_partition SMALLINT,
68 address HSTORE, country TEXT)
69 RETURNS SETOF nearfeaturecentr
72 r nearfeaturecentr%rowtype;
76 SELECT (get_addr_tag_rank(key, country)).*, key, name FROM
77 (SELECT skeys(address) as key, svals(address) as name) x
79 IF item.from_rank is null THEN
84 IF in_partition = -partition- THEN
85 SELECT place_id, keywords, rank_address, rank_search,
86 min(ST_Distance(feature, centroid)) as distance,
87 isguess, postcode, centroid INTO r
88 FROM location_area_large_-partition-
89 WHERE geometry && ST_Expand(feature, item.extent)
90 AND rank_address between item.from_rank and item.to_rank
91 AND word_ids_from_name(item.name) && keywords
92 GROUP BY place_id, keywords, rank_address, rank_search, isguess, postcode, centroid
93 ORDER BY bool_or(ST_Intersects(geometry, feature)), distance LIMIT 1;
94 IF r.place_id is null THEN
95 -- If we cannot find a place for the term, just return the
96 -- search term for the given name. That ensures that the address
97 -- element can still be searched for, even though it will not be
99 RETURN NEXT ROW(null, addr_ids_from_name(item.name), null, null,
100 null, null, null, null)::nearfeaturecentr;
108 RAISE EXCEPTION 'Unknown partition %', in_partition;
112 LANGUAGE plpgsql STABLE;
114 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT, in_rank_search INTEGER) RETURNS BOOLEAN AS $$
118 IF in_rank_search <= 4 THEN
119 DELETE from location_area_country WHERE place_id = in_place_id;
124 IF in_partition = -partition- THEN
125 DELETE from location_area_large_-partition- WHERE place_id = in_place_id;
130 RAISE EXCEPTION 'Unknown partition %', in_partition;
137 create or replace function insertLocationAreaLarge(
138 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_keywords INTEGER[],
139 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN, postcode TEXT,
140 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
143 IF in_rank_address = 0 THEN
147 IF in_rank_search <= 4 and not in_estimate THEN
148 INSERT INTO location_area_country (place_id, country_code, geometry)
149 values (in_place_id, in_country_code, in_geometry);
154 IF in_partition = -partition- THEN
155 INSERT INTO location_area_large_-partition- (partition, place_id, country_code, keywords, rank_search, rank_address, isguess, postcode, centroid, geometry)
156 values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, postcode, in_centroid, in_geometry);
161 RAISE EXCEPTION 'Unknown partition %', in_partition;
167 CREATE OR REPLACE FUNCTION getNearestNamedRoadPlaceId(in_partition INTEGER,
169 isin_token INTEGER[])
177 IF in_partition = -partition- THEN
178 SELECT place_id FROM search_name_-partition-
180 WHERE name_vector && isin_token
181 AND centroid && ST_Expand(point, 0.015)
182 AND address_rank between 26 and 27
183 ORDER BY ST_Distance(centroid, point) ASC limit 1;
188 RAISE EXCEPTION 'Unknown partition %', in_partition;
191 LANGUAGE plpgsql STABLE;
193 CREATE OR REPLACE FUNCTION getNearestNamedPlacePlaceId(in_partition INTEGER,
195 isin_token INTEGER[])
203 IF in_partition = -partition- THEN
206 FROM search_name_-partition-
207 WHERE name_vector && isin_token
208 AND centroid && ST_Expand(point, 0.04)
209 AND address_rank between 16 and 25
210 ORDER BY ST_Distance(centroid, point) ASC limit 1;
215 RAISE EXCEPTION 'Unknown partition %', in_partition;
218 LANGUAGE plpgsql STABLE;
220 create or replace function insertSearchName(
221 in_partition INTEGER, in_place_id BIGINT, in_name_vector INTEGER[],
222 in_rank_search INTEGER, in_rank_address INTEGER, in_geometry GEOMETRY)
223 RETURNS BOOLEAN AS $$
227 IF in_partition = -partition- THEN
228 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
229 IF in_rank_address > 0 THEN
230 INSERT INTO search_name_-partition- (place_id, address_rank, name_vector, centroid)
231 values (in_place_id, in_rank_address, in_name_vector, in_geometry);
237 RAISE EXCEPTION 'Unknown partition %', in_partition;
243 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
247 IF in_partition = -partition- THEN
248 DELETE from search_name_-partition- WHERE place_id = in_place_id;
253 RAISE EXCEPTION 'Unknown partition %', in_partition;
260 create or replace function insertLocationRoad(
261 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
266 IF in_partition = -partition- THEN
267 DELETE FROM location_road_-partition- where place_id = in_place_id;
268 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
269 values (in_partition, in_place_id, in_country_code, in_geometry);
274 RAISE EXCEPTION 'Unknown partition %', in_partition;
280 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
285 IF in_partition = -partition- THEN
286 DELETE FROM location_road_-partition- where place_id = in_place_id;
291 RAISE EXCEPTION 'Unknown partition %', in_partition;
298 CREATE OR REPLACE FUNCTION getNearestRoadPlaceId(in_partition INTEGER, point GEOMETRY)
303 search_diameter FLOAT;
307 IF in_partition = -partition- THEN
308 search_diameter := 0.00005;
309 WHILE search_diameter < 0.1 LOOP
311 SELECT place_id FROM location_road_-partition-
312 WHERE ST_DWithin(geometry, point, search_diameter)
313 ORDER BY ST_Distance(geometry, point) ASC limit 1
317 search_diameter := search_diameter * 2;
323 RAISE EXCEPTION 'Unknown partition %', in_partition;
326 LANGUAGE plpgsql STABLE;
328 CREATE OR REPLACE FUNCTION getNearestParallelRoadFeature(in_partition INTEGER,
334 search_diameter FLOAT;
340 IF ST_GeometryType(line) not in ('ST_LineString') THEN
344 p1 := ST_LineInterpolatePoint(line,0);
345 p2 := ST_LineInterpolatePoint(line,0.5);
346 p3 := ST_LineInterpolatePoint(line,1);
349 IF in_partition = -partition- THEN
350 search_diameter := 0.0005;
351 WHILE search_diameter < 0.01 LOOP
353 SELECT place_id FROM location_road_-partition-
354 WHERE ST_DWithin(line, geometry, search_diameter)
355 ORDER BY (ST_distance(geometry, p1)+
356 ST_distance(geometry, p2)+
357 ST_distance(geometry, p3)) ASC limit 1
361 search_diameter := search_diameter * 2;
367 RAISE EXCEPTION 'Unknown partition %', in_partition;
370 LANGUAGE plpgsql STABLE;