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 (SELECT in_place_id, in_country_code, geom
129 FROM split_geometry(in_geometry) as geom);
133 {% for partition in db.partitions %}
134 IF in_partition = {{ partition }} THEN
135 INSERT INTO location_area_large_{{ partition }} (partition, place_id, country_code, keywords, rank_search, rank_address, isguess, postcode, centroid, geometry)
136 (SELECT in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, postcode, in_centroid, geom
137 FROM split_geometry(in_geometry) as geom);
142 RAISE EXCEPTION 'Unknown partition %', in_partition;
148 CREATE OR REPLACE FUNCTION getNearestNamedRoadPlaceId(in_partition INTEGER,
156 IF not token_has_addr_street(token_info) THEN
160 {% for partition in db.partitions %}
161 IF in_partition = {{ partition }} THEN
162 SELECT place_id FROM search_name_{{ partition }}
164 WHERE token_matches_street(token_info, name_vector)
165 AND centroid && ST_Expand(point, 0.015)
166 AND address_rank between 26 and 27
167 ORDER BY ST_Distance(centroid, point) ASC limit 1;
172 RAISE EXCEPTION 'Unknown partition %', in_partition;
175 LANGUAGE plpgsql STABLE;
177 CREATE OR REPLACE FUNCTION getNearestNamedPlacePlaceId(in_partition INTEGER,
185 IF not token_has_addr_place(token_info) THEN
189 {% for partition in db.partitions %}
190 IF in_partition = {{ partition }} THEN
193 FROM search_name_{{ partition }}
194 WHERE token_matches_place(token_info, name_vector)
195 AND centroid && ST_Expand(point, 0.04)
196 AND address_rank between 16 and 25
197 ORDER BY ST_Distance(centroid, point) ASC limit 1;
202 RAISE EXCEPTION 'Unknown partition %', in_partition;
205 LANGUAGE plpgsql STABLE;
207 create or replace function insertSearchName(
208 in_partition INTEGER, in_place_id BIGINT, in_name_vector INTEGER[],
209 in_rank_search INTEGER, in_rank_address INTEGER, in_geometry GEOMETRY)
210 RETURNS BOOLEAN AS $$
213 {% for partition in db.partitions %}
214 IF in_partition = {{ partition }} THEN
215 DELETE FROM search_name_{{ partition }} values WHERE place_id = in_place_id;
216 IF in_rank_address > 0 THEN
217 INSERT INTO search_name_{{ partition }} (place_id, address_rank, name_vector, centroid)
218 values (in_place_id, in_rank_address, in_name_vector, in_geometry);
224 RAISE EXCEPTION 'Unknown partition %', in_partition;
230 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
233 {% for partition in db.partitions %}
234 IF in_partition = {{ partition }} THEN
235 DELETE from search_name_{{ partition }} WHERE place_id = in_place_id;
240 RAISE EXCEPTION 'Unknown partition %', in_partition;
247 create or replace function insertLocationRoad(
248 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
252 {% for partition in db.partitions %}
253 IF in_partition = {{ partition }} THEN
254 DELETE FROM location_road_{{ partition }} where place_id = in_place_id;
255 INSERT INTO location_road_{{ partition }} (partition, place_id, country_code, geometry)
256 values (in_partition, in_place_id, in_country_code, in_geometry);
261 RAISE EXCEPTION 'Unknown partition %', in_partition;
267 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
271 {% for partition in db.partitions %}
272 IF in_partition = {{ partition }} THEN
273 DELETE FROM location_road_{{ partition }} where place_id = in_place_id;
278 RAISE EXCEPTION 'Unknown partition %', in_partition;
285 CREATE OR REPLACE FUNCTION getNearestRoadPlaceId(in_partition INTEGER, point GEOMETRY)
290 search_diameter FLOAT;
293 {% for partition in db.partitions %}
294 IF in_partition = {{ partition }} THEN
295 search_diameter := 0.00005;
296 WHILE search_diameter < 0.1 LOOP
298 SELECT place_id FROM location_road_{{ partition }}
299 WHERE ST_DWithin(geometry, point, search_diameter)
300 ORDER BY ST_Distance(geometry, point) ASC limit 1
304 search_diameter := search_diameter * 2;
310 RAISE EXCEPTION 'Unknown partition %', in_partition;
313 LANGUAGE plpgsql STABLE;
315 CREATE OR REPLACE FUNCTION getNearestParallelRoadFeature(in_partition INTEGER,
321 search_diameter FLOAT;
327 IF ST_GeometryType(line) not in ('ST_LineString') THEN
331 p1 := ST_LineInterpolatePoint(line,0);
332 p2 := ST_LineInterpolatePoint(line,0.5);
333 p3 := ST_LineInterpolatePoint(line,1);
335 {% for partition in db.partitions %}
336 IF in_partition = {{ partition }} THEN
337 search_diameter := 0.0005;
338 WHILE search_diameter < 0.01 LOOP
340 SELECT place_id FROM location_road_{{ partition }}
341 WHERE ST_DWithin(line, geometry, search_diameter)
342 ORDER BY (ST_distance(geometry, p1)+
343 ST_distance(geometry, p2)+
344 ST_distance(geometry, p3)) ASC limit 1
348 search_diameter := search_diameter * 2;
354 RAISE EXCEPTION 'Unknown partition %', in_partition;
357 LANGUAGE plpgsql STABLE;