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 -- feature intersects geoemtry
21 -- for areas and linestrings they must touch at least along a line
22 CREATE OR REPLACE FUNCTION is_relevant_geometry(de9im TEXT, geom_type TEXT)
26 IF substring(de9im from 1 for 2) != 'FF' THEN
30 IF geom_type = 'ST_Point' THEN
31 RETURN substring(de9im from 4 for 1) = '0';
34 IF geom_type in ('ST_LineString', 'ST_MultiLineString') THEN
35 RETURN substring(de9im from 4 for 1) = '1';
38 RETURN substring(de9im from 4 for 1) = '2';
40 $$ LANGUAGE plpgsql IMMUTABLE;
42 create or replace function getNearFeatures(in_partition INTEGER, feature GEOMETRY, maxrank INTEGER) RETURNS setof nearfeaturecentr AS $$
44 r nearfeaturecentr%rowtype;
47 {% for partition in db.partitions %}
48 IF in_partition = {{ partition }} THEN
50 SELECT place_id, keywords, rank_address, rank_search,
51 min(ST_Distance(feature, centroid)) as distance,
52 isguess, postcode, centroid
53 FROM location_area_large_{{ partition }}
54 WHERE geometry && feature
55 AND is_relevant_geometry(ST_Relate(geometry, feature), ST_GeometryType(feature))
56 AND rank_address < maxrank
57 -- Postcodes currently still use rank_search to define for which
58 -- features they are relevant.
59 AND not (rank_address in (5, 11) and rank_search > maxrank)
60 GROUP BY place_id, keywords, rank_address, rank_search, isguess, postcode, centroid
68 RAISE EXCEPTION 'Unknown partition %', in_partition;
71 LANGUAGE plpgsql STABLE;
74 CREATE OR REPLACE FUNCTION get_address_place(in_partition SMALLINT, feature GEOMETRY,
75 from_rank SMALLINT, to_rank SMALLINT,
76 extent FLOAT, token_info JSONB, key TEXT)
77 RETURNS nearfeaturecentr
80 r nearfeaturecentr%rowtype;
82 {% for partition in db.partitions %}
83 IF in_partition = {{ partition }} THEN
84 SELECT place_id, keywords, rank_address, rank_search,
85 min(ST_Distance(feature, centroid)) as distance,
86 isguess, postcode, centroid INTO r
87 FROM location_area_large_{{ partition }}
88 WHERE geometry && ST_Expand(feature, extent)
89 AND rank_address between from_rank and to_rank
90 AND token_matches_address(token_info, key, keywords)
91 GROUP BY place_id, keywords, rank_address, rank_search, isguess, postcode, centroid
92 ORDER BY bool_or(ST_Intersects(geometry, feature)), distance LIMIT 1;
97 RAISE EXCEPTION 'Unknown partition %', in_partition;
100 LANGUAGE plpgsql STABLE;
103 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT, in_rank_search INTEGER) RETURNS BOOLEAN AS $$
107 IF in_rank_search <= 4 THEN
108 DELETE from location_area_country WHERE place_id = in_place_id;
112 {% for partition in db.partitions %}
113 IF in_partition = {{ partition }} THEN
114 DELETE from location_area_large_{{ partition }} WHERE place_id = in_place_id;
119 RAISE EXCEPTION 'Unknown partition %', in_partition;
126 create or replace function insertLocationAreaLarge(
127 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_keywords INTEGER[],
128 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN, postcode TEXT,
129 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
132 IF in_rank_address = 0 THEN
136 IF in_rank_search <= 4 and not in_estimate THEN
137 INSERT INTO location_area_country (place_id, country_code, geometry)
138 values (in_place_id, in_country_code, in_geometry);
142 {% for partition in db.partitions %}
143 IF in_partition = {{ partition }} THEN
144 INSERT INTO location_area_large_{{ partition }} (partition, place_id, country_code, keywords, rank_search, rank_address, isguess, postcode, centroid, geometry)
145 values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, postcode, in_centroid, in_geometry);
150 RAISE EXCEPTION 'Unknown partition %', in_partition;
156 CREATE OR REPLACE FUNCTION getNearestNamedRoadPlaceId(in_partition INTEGER,
164 IF not token_has_addr_street(token_info) THEN
168 {% for partition in db.partitions %}
169 IF in_partition = {{ partition }} THEN
170 SELECT place_id FROM search_name_{{ partition }}
172 WHERE token_matches_street(token_info, name_vector)
173 AND centroid && ST_Expand(point, 0.015)
174 AND address_rank between 26 and 27
175 ORDER BY ST_Distance(centroid, point) ASC limit 1;
180 RAISE EXCEPTION 'Unknown partition %', in_partition;
183 LANGUAGE plpgsql STABLE;
185 CREATE OR REPLACE FUNCTION getNearestNamedPlacePlaceId(in_partition INTEGER,
193 IF not token_has_addr_place(token_info) THEN
197 {% for partition in db.partitions %}
198 IF in_partition = {{ partition }} THEN
201 FROM search_name_{{ partition }}
202 WHERE token_matches_place(token_info, name_vector)
203 AND centroid && ST_Expand(point, 0.04)
204 AND address_rank between 16 and 25
205 ORDER BY ST_Distance(centroid, point) ASC limit 1;
210 RAISE EXCEPTION 'Unknown partition %', in_partition;
213 LANGUAGE plpgsql STABLE;
215 create or replace function insertSearchName(
216 in_partition INTEGER, in_place_id BIGINT, in_name_vector INTEGER[],
217 in_rank_search INTEGER, in_rank_address INTEGER, in_geometry GEOMETRY)
218 RETURNS BOOLEAN AS $$
221 {% for partition in db.partitions %}
222 IF in_partition = {{ partition }} THEN
223 DELETE FROM search_name_{{ partition }} values WHERE place_id = in_place_id;
224 IF in_rank_address > 0 THEN
225 INSERT INTO search_name_{{ partition }} (place_id, address_rank, name_vector, centroid)
226 values (in_place_id, in_rank_address, in_name_vector, in_geometry);
232 RAISE EXCEPTION 'Unknown partition %', in_partition;
238 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
241 {% for partition in db.partitions %}
242 IF in_partition = {{ partition }} THEN
243 DELETE from search_name_{{ partition }} WHERE place_id = in_place_id;
248 RAISE EXCEPTION 'Unknown partition %', in_partition;
255 create or replace function insertLocationRoad(
256 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
260 {% for partition in db.partitions %}
261 IF in_partition = {{ partition }} THEN
262 DELETE FROM location_road_{{ partition }} where place_id = in_place_id;
263 INSERT INTO location_road_{{ partition }} (partition, place_id, country_code, geometry)
264 values (in_partition, in_place_id, in_country_code, in_geometry);
269 RAISE EXCEPTION 'Unknown partition %', in_partition;
275 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
279 {% for partition in db.partitions %}
280 IF in_partition = {{ partition }} THEN
281 DELETE FROM location_road_{{ partition }} where place_id = in_place_id;
286 RAISE EXCEPTION 'Unknown partition %', in_partition;
293 CREATE OR REPLACE FUNCTION getNearestRoadPlaceId(in_partition INTEGER, point GEOMETRY)
298 search_diameter FLOAT;
301 {% for partition in db.partitions %}
302 IF in_partition = {{ partition }} THEN
303 search_diameter := 0.00005;
304 WHILE search_diameter < 0.1 LOOP
306 SELECT place_id FROM location_road_{{ partition }}
307 WHERE ST_DWithin(geometry, point, search_diameter)
308 ORDER BY ST_Distance(geometry, point) ASC limit 1
312 search_diameter := search_diameter * 2;
318 RAISE EXCEPTION 'Unknown partition %', in_partition;
321 LANGUAGE plpgsql STABLE;
323 CREATE OR REPLACE FUNCTION getNearestParallelRoadFeature(in_partition INTEGER,
329 search_diameter FLOAT;
335 IF ST_GeometryType(line) not in ('ST_LineString') THEN
339 p1 := ST_LineInterpolatePoint(line,0);
340 p2 := ST_LineInterpolatePoint(line,0.5);
341 p3 := ST_LineInterpolatePoint(line,1);
343 {% for partition in db.partitions %}
344 IF in_partition = {{ partition }} THEN
345 search_diameter := 0.0005;
346 WHILE search_diameter < 0.01 LOOP
348 SELECT place_id FROM location_road_{{ partition }}
349 WHERE ST_DWithin(line, geometry, search_diameter)
350 ORDER BY (ST_distance(geometry, p1)+
351 ST_distance(geometry, p2)+
352 ST_distance(geometry, p3)) ASC limit 1
356 search_diameter := search_diameter * 2;
362 RAISE EXCEPTION 'Unknown partition %', in_partition;
365 LANGUAGE plpgsql STABLE;