1 -- SPDX-License-Identifier: GPL-2.0-only
3 -- This file is part of Nominatim. (https://nominatim.org)
5 -- Copyright (C) 2022 by the Nominatim developer community.
6 -- For a full list of authors see the git log.
8 DROP TYPE IF EXISTS nearfeaturecentr CASCADE;
9 CREATE TYPE nearfeaturecentr AS (
12 rank_address smallint,
20 CREATE OR REPLACE function getNearFeatures(in_partition INTEGER, feature GEOMETRY,
21 feature_centroid GEOMETRY,
23 RETURNS setof nearfeaturecentr AS $$
25 r nearfeaturecentr%rowtype;
28 {% for partition in db.partitions %}
29 IF in_partition = {{ partition }} THEN
31 SELECT place_id, keywords, rank_address, rank_search,
32 CASE WHEN isguess THEN ST_Distance(feature, centroid)
33 ELSE min(ST_Distance(feature_centroid, geometry))
34 -- tie breaker when distance is the same (i.e. way is on boundary)
35 + 0.00001 * ST_Distance(feature, centroid)
37 isguess, postcode, centroid
38 FROM location_area_large_{{ partition }}
39 WHERE geometry && feature
40 AND CASE WHEN ST_Dimension(feature) = 0
41 THEN _ST_Covers(geometry, feature)
42 WHEN ST_Dimension(feature) = 2
43 THEN ST_Relate(geometry, feature, 'T********')
44 ELSE ST_NPoints(ST_Intersection(geometry, feature)) > 1
46 AND rank_address < maxrank
47 -- Postcodes currently still use rank_search to define for which
48 -- features they are relevant.
49 AND not (rank_address in (5, 11) and rank_search > 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;
64 CREATE OR REPLACE FUNCTION get_address_place(in_partition SMALLINT, feature GEOMETRY,
65 from_rank SMALLINT, to_rank SMALLINT,
66 extent FLOAT, token_info JSONB, key TEXT)
67 RETURNS nearfeaturecentr
70 r nearfeaturecentr%rowtype;
72 {% for partition in db.partitions %}
73 IF in_partition = {{ partition }} THEN
74 SELECT place_id, keywords, rank_address, rank_search,
75 min(ST_Distance(feature, centroid)) as distance,
76 isguess, postcode, centroid INTO r
77 FROM location_area_large_{{ partition }}
78 WHERE geometry && ST_Expand(feature, extent)
79 AND rank_address between from_rank and to_rank
80 AND token_matches_address(token_info, key, keywords)
81 GROUP BY place_id, keywords, rank_address, rank_search, isguess, postcode, centroid
82 ORDER BY bool_or(ST_Intersects(geometry, feature)) DESC, distance LIMIT 1;
87 RAISE EXCEPTION 'Unknown partition %', in_partition;
90 LANGUAGE plpgsql STABLE;
93 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT, in_rank_search INTEGER) RETURNS BOOLEAN AS $$
97 IF in_rank_search <= 4 THEN
98 DELETE from location_area_country WHERE place_id = in_place_id;
102 {% for partition in db.partitions %}
103 IF in_partition = {{ partition }} THEN
104 DELETE from location_area_large_{{ partition }} WHERE place_id = in_place_id;
109 RAISE EXCEPTION 'Unknown partition %', in_partition;
116 create or replace function insertLocationAreaLarge(
117 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_keywords INTEGER[],
118 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN, postcode TEXT,
119 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
122 IF in_rank_address = 0 THEN
126 IF in_rank_search <= 4 and not in_estimate THEN
127 INSERT INTO location_area_country (place_id, country_code, geometry)
128 values (in_place_id, in_country_code, in_geometry);
132 {% for partition in db.partitions %}
133 IF in_partition = {{ partition }} THEN
134 INSERT INTO location_area_large_{{ partition }} (partition, place_id, country_code, keywords, rank_search, rank_address, isguess, postcode, centroid, geometry)
135 values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, postcode, in_centroid, in_geometry);
140 RAISE EXCEPTION 'Unknown partition %', in_partition;
146 CREATE OR REPLACE FUNCTION getNearestNamedRoadPlaceId(in_partition INTEGER,
154 IF not token_has_addr_street(token_info) THEN
158 {% for partition in db.partitions %}
159 IF in_partition = {{ partition }} THEN
160 SELECT place_id FROM search_name_{{ partition }}
162 WHERE token_matches_street(token_info, name_vector)
163 AND centroid && ST_Expand(point, 0.015)
164 AND address_rank between 26 and 27
165 ORDER BY ST_Distance(centroid, point) ASC limit 1;
170 RAISE EXCEPTION 'Unknown partition %', in_partition;
173 LANGUAGE plpgsql STABLE;
175 CREATE OR REPLACE FUNCTION getNearestNamedPlacePlaceId(in_partition INTEGER,
183 IF not token_has_addr_place(token_info) THEN
187 {% for partition in db.partitions %}
188 IF in_partition = {{ partition }} THEN
191 FROM search_name_{{ partition }}
192 WHERE token_matches_place(token_info, name_vector)
193 AND centroid && ST_Expand(point, 0.04)
194 AND address_rank between 16 and 25
195 ORDER BY ST_Distance(centroid, point) ASC limit 1;
200 RAISE EXCEPTION 'Unknown partition %', in_partition;
203 LANGUAGE plpgsql STABLE;
205 create or replace function insertSearchName(
206 in_partition INTEGER, in_place_id BIGINT, in_name_vector INTEGER[],
207 in_rank_search INTEGER, in_rank_address INTEGER, in_geometry GEOMETRY)
208 RETURNS BOOLEAN AS $$
211 {% for partition in db.partitions %}
212 IF in_partition = {{ partition }} THEN
213 DELETE FROM search_name_{{ partition }} values WHERE place_id = in_place_id;
214 IF in_rank_address > 0 THEN
215 INSERT INTO search_name_{{ partition }} (place_id, address_rank, name_vector, centroid)
216 values (in_place_id, in_rank_address, in_name_vector, in_geometry);
222 RAISE EXCEPTION 'Unknown partition %', in_partition;
228 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
231 {% for partition in db.partitions %}
232 IF in_partition = {{ partition }} THEN
233 DELETE from search_name_{{ partition }} WHERE place_id = in_place_id;
238 RAISE EXCEPTION 'Unknown partition %', in_partition;
245 create or replace function insertLocationRoad(
246 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
250 {% for partition in db.partitions %}
251 IF in_partition = {{ partition }} THEN
252 DELETE FROM location_road_{{ partition }} where place_id = in_place_id;
253 INSERT INTO location_road_{{ partition }} (partition, place_id, country_code, geometry)
254 values (in_partition, in_place_id, in_country_code, in_geometry);
259 RAISE EXCEPTION 'Unknown partition %', in_partition;
265 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
269 {% for partition in db.partitions %}
270 IF in_partition = {{ partition }} THEN
271 DELETE FROM location_road_{{ partition }} where place_id = in_place_id;
276 RAISE EXCEPTION 'Unknown partition %', in_partition;
283 CREATE OR REPLACE FUNCTION getNearestRoadPlaceId(in_partition INTEGER, point GEOMETRY)
288 search_diameter FLOAT;
291 {% for partition in db.partitions %}
292 IF in_partition = {{ partition }} THEN
293 search_diameter := 0.00005;
294 WHILE search_diameter < 0.1 LOOP
296 SELECT place_id FROM location_road_{{ partition }}
297 WHERE ST_DWithin(geometry, point, search_diameter)
298 ORDER BY ST_Distance(geometry, point) ASC limit 1
302 search_diameter := search_diameter * 2;
308 RAISE EXCEPTION 'Unknown partition %', in_partition;
311 LANGUAGE plpgsql STABLE;
313 CREATE OR REPLACE FUNCTION getNearestParallelRoadFeature(in_partition INTEGER,
319 search_diameter FLOAT;
325 IF ST_GeometryType(line) not in ('ST_LineString') THEN
329 p1 := ST_LineInterpolatePoint(line,0);
330 p2 := ST_LineInterpolatePoint(line,0.5);
331 p3 := ST_LineInterpolatePoint(line,1);
333 {% for partition in db.partitions %}
334 IF in_partition = {{ partition }} THEN
335 search_diameter := 0.0005;
336 WHILE search_diameter < 0.01 LOOP
338 SELECT place_id FROM location_road_{{ partition }}
339 WHERE ST_DWithin(line, geometry, search_diameter)
340 ORDER BY (ST_distance(geometry, p1)+
341 ST_distance(geometry, p2)+
342 ST_distance(geometry, p3)) ASC limit 1
346 search_diameter := search_diameter * 2;
352 RAISE EXCEPTION 'Unknown partition %', in_partition;
355 LANGUAGE plpgsql STABLE;