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 GROUP BY place_id, keywords, rank_address, rank_search, isguess, postcode, centroid
58 RAISE EXCEPTION 'Unknown partition %', in_partition;
61 LANGUAGE plpgsql STABLE;
63 CREATE OR REPLACE FUNCTION get_places_for_addr_tags(in_partition SMALLINT,
65 address HSTORE, country TEXT)
66 RETURNS SETOF nearfeaturecentr
69 r nearfeaturecentr%rowtype;
73 SELECT (get_addr_tag_rank(key, country)).*, key, name FROM
74 (SELECT skeys(address) as key, svals(address) as name) x
76 IF item.from_rank is null THEN
81 IF in_partition = -partition- THEN
82 SELECT place_id, keywords, rank_address, rank_search,
83 min(ST_Distance(feature, centroid)) as distance,
84 isguess, postcode, centroid INTO r
85 FROM location_area_large_-partition-
86 WHERE geometry && ST_Expand(feature, item.extent)
87 AND rank_address between item.from_rank and item.to_rank
88 AND word_ids_from_name(item.name) && keywords
89 GROUP BY place_id, keywords, rank_address, rank_search, isguess, postcode, centroid
90 ORDER BY bool_or(ST_Intersects(geometry, feature)), distance LIMIT 1;
91 IF r.place_id is null THEN
92 -- If we cannot find a place for the term, just return the
93 -- search term for the given name. That ensures that the address
94 -- element can still be searched for, even though it will not be
96 RETURN NEXT ROW(null, addr_ids_from_name(item.name), null, null,
97 null, null, null, null)::nearfeaturecentr;
105 RAISE EXCEPTION 'Unknown partition %', in_partition;
109 LANGUAGE plpgsql STABLE;
111 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT, in_rank_search INTEGER) RETURNS BOOLEAN AS $$
115 IF in_rank_search <= 4 THEN
116 DELETE from location_area_country WHERE place_id = in_place_id;
121 IF in_partition = -partition- THEN
122 DELETE from location_area_large_-partition- WHERE place_id = in_place_id;
127 RAISE EXCEPTION 'Unknown partition %', in_partition;
134 create or replace function insertLocationAreaLarge(
135 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_keywords INTEGER[],
136 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN, postcode TEXT,
137 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
140 IF in_rank_address = 0 THEN
144 IF in_rank_search <= 4 and not in_estimate THEN
145 INSERT INTO location_area_country (place_id, country_code, geometry)
146 values (in_place_id, in_country_code, in_geometry);
151 IF in_partition = -partition- THEN
152 INSERT INTO location_area_large_-partition- (partition, place_id, country_code, keywords, rank_search, rank_address, isguess, postcode, centroid, geometry)
153 values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, postcode, in_centroid, in_geometry);
158 RAISE EXCEPTION 'Unknown partition %', in_partition;
164 CREATE OR REPLACE FUNCTION getNearestNamedRoadPlaceId(in_partition INTEGER,
166 isin_token INTEGER[])
174 IF in_partition = -partition- THEN
175 SELECT place_id FROM search_name_-partition-
177 WHERE name_vector && isin_token
178 AND centroid && ST_Expand(point, 0.015)
179 AND address_rank between 26 and 27
180 ORDER BY ST_Distance(centroid, point) ASC limit 1;
185 RAISE EXCEPTION 'Unknown partition %', in_partition;
188 LANGUAGE plpgsql STABLE;
190 CREATE OR REPLACE FUNCTION getNearestNamedPlacePlaceId(in_partition INTEGER,
192 isin_token INTEGER[])
200 IF in_partition = -partition- THEN
203 FROM search_name_-partition-
204 WHERE name_vector && isin_token
205 AND centroid && ST_Expand(point, 0.04)
206 AND address_rank between 16 and 25
207 ORDER BY ST_Distance(centroid, point) ASC limit 1;
212 RAISE EXCEPTION 'Unknown partition %', in_partition;
215 LANGUAGE plpgsql STABLE;
217 create or replace function insertSearchName(
218 in_partition INTEGER, in_place_id BIGINT, in_name_vector INTEGER[],
219 in_rank_search INTEGER, in_rank_address INTEGER, in_geometry GEOMETRY)
220 RETURNS BOOLEAN AS $$
224 IF in_partition = -partition- THEN
225 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
226 IF in_rank_address > 0 THEN
227 INSERT INTO search_name_-partition- (place_id, address_rank, name_vector, centroid)
228 values (in_place_id, in_rank_address, in_name_vector, in_geometry);
234 RAISE EXCEPTION 'Unknown partition %', in_partition;
240 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
244 IF in_partition = -partition- THEN
245 DELETE from search_name_-partition- WHERE place_id = in_place_id;
250 RAISE EXCEPTION 'Unknown partition %', in_partition;
257 create or replace function insertLocationRoad(
258 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
263 IF in_partition = -partition- THEN
264 DELETE FROM location_road_-partition- where place_id = in_place_id;
265 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
266 values (in_partition, in_place_id, in_country_code, in_geometry);
271 RAISE EXCEPTION 'Unknown partition %', in_partition;
277 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
282 IF in_partition = -partition- THEN
283 DELETE FROM location_road_-partition- where place_id = in_place_id;
288 RAISE EXCEPTION 'Unknown partition %', in_partition;
295 CREATE OR REPLACE FUNCTION getNearestRoadPlaceId(in_partition INTEGER, point GEOMETRY)
300 search_diameter FLOAT;
304 IF in_partition = -partition- THEN
305 search_diameter := 0.00005;
306 WHILE search_diameter < 0.1 LOOP
308 SELECT place_id FROM location_road_-partition-
309 WHERE ST_DWithin(geometry, point, search_diameter)
310 ORDER BY ST_Distance(geometry, point) ASC limit 1
314 search_diameter := search_diameter * 2;
320 RAISE EXCEPTION 'Unknown partition %', in_partition;
323 LANGUAGE plpgsql STABLE;
325 CREATE OR REPLACE FUNCTION getNearestParallelRoadFeature(in_partition INTEGER,
331 search_diameter FLOAT;
337 IF ST_GeometryType(line) not in ('ST_LineString') THEN
341 p1 := ST_LineInterpolatePoint(line,0);
342 p2 := ST_LineInterpolatePoint(line,0.5);
343 p3 := ST_LineInterpolatePoint(line,1);
346 IF in_partition = -partition- THEN
347 search_diameter := 0.0005;
348 WHILE search_diameter < 0.01 LOOP
350 SELECT place_id FROM location_road_-partition-
351 WHERE ST_DWithin(line, geometry, search_diameter)
352 ORDER BY (ST_distance(geometry, p1)+
353 ST_distance(geometry, p2)+
354 ST_distance(geometry, p3)) ASC limit 1
358 search_diameter := search_diameter * 2;
364 RAISE EXCEPTION 'Unknown partition %', in_partition;
367 LANGUAGE plpgsql STABLE;