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 -- Functions related to search and address ranks
10 -- Return an approximate search radius according to the search rank.
11 CREATE OR REPLACE FUNCTION reverse_place_diameter(rank_search SMALLINT)
15 IF rank_search <= 4 THEN
17 ELSIF rank_search <= 8 THEN
19 ELSIF rank_search <= 12 THEN
21 ELSIF rank_search <= 17 THEN
23 ELSIF rank_search <= 18 THEN
25 ELSIF rank_search <= 19 THEN
32 LANGUAGE plpgsql IMMUTABLE;
35 -- Return an approximate update radius according to the search rank.
36 CREATE OR REPLACE FUNCTION update_place_diameter(rank_search SMALLINT)
41 IF rank_search = 11 or rank_search = 5 THEN
43 -- anything higher than city is effectively ignored (polygon required)
44 ELSIF rank_search < 16 THEN
46 ELSIF rank_search < 18 THEN
48 ELSIF rank_search < 20 THEN
50 ELSIF rank_search = 21 THEN
52 ELSIF rank_search < 24 THEN
54 ELSIF rank_search < 26 THEN
56 ELSIF rank_search < 28 THEN
63 LANGUAGE plpgsql IMMUTABLE;
65 -- Compute a base address rank from the extent of the given geometry.
67 -- This is all simple guess work. We don't need particularly good estimates
68 -- here. This just avoids to have very high ranked address parts in features
69 -- that span very large areas (or vice versa).
70 CREATE OR REPLACE FUNCTION geometry_to_rank(search_rank SMALLINT, geometry GEOMETRY, country_code TEXT)
76 IF ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') THEN
77 area := ST_Area(geometry);
78 ELSIF ST_GeometryType(geometry) in ('ST_LineString','ST_MultiLineString') THEN
79 area := (ST_Length(geometry)^2) * 0.1;
84 -- adjust for the fact that countries come in different sizes
85 IF country_code IN ('ca', 'au', 'ru') THEN
87 ELSIF country_code IN ('br', 'kz', 'cn', 'us', 'ne', 'gb', 'za', 'sa', 'id', 'eh', 'ml', 'tm') THEN
89 ELSIF country_code IN ('bo', 'ar', 'sd', 'mn', 'in', 'et', 'cd', 'mz', 'ly', 'cl', 'zm') THEN
97 ELSIF area > 0.01 THEN
99 ELSIF area > 0.001 THEN
101 ELSIF area > 0.0001 THEN
103 ELSIF area > 0.000005 THEN
110 LANGUAGE plpgsql IMMUTABLE;
113 -- Guess a ranking for postcodes from country and postcode format.
114 CREATE OR REPLACE FUNCTION get_postcode_rank(country_code VARCHAR(2), postcode TEXT,
115 OUT rank_search SMALLINT,
116 OUT rank_address SMALLINT)
123 postcode := upper(postcode);
125 IF country_code = 'gb' THEN
126 IF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9][A-Z][A-Z])$' THEN
129 ELSEIF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9])$' THEN
132 ELSEIF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z])$' THEN
137 ELSEIF country_code = 'sg' THEN
138 IF postcode ~ '^([0-9]{6})$' THEN
143 ELSEIF country_code = 'de' THEN
144 IF postcode ~ '^([0-9]{5})$' THEN
150 -- Guess at the postcode format and coverage (!)
151 IF postcode ~ '^[A-Z0-9]{1,5}$' THEN -- Probably too short to be very local
155 -- Does it look splitable into and area and local code?
156 part := substring(postcode from '^([- :A-Z0-9]+)([- :][A-Z0-9]+)$');
158 IF part IS NOT NULL THEN
161 ELSEIF postcode ~ '^[- :A-Z0-9]{6,}$' THEN
170 LANGUAGE plpgsql IMMUTABLE;
173 -- Get standard search and address rank for an object.
175 -- \param country Two-letter country code where the object is in.
176 -- \param extended_type OSM type (N, W, R) or area type (A).
177 -- \param place_class Class (or tag key) of object.
178 -- \param place_type Type (or tag value) of object.
179 -- \param admin_level Value of admin_level tag.
180 -- \param is_major If true, boost search rank by one.
181 -- \param postcode Value of addr:postcode tag.
182 -- \param[out] search_rank Computed search rank.
183 -- \param[out] address_rank Computed address rank.
185 CREATE OR REPLACE FUNCTION compute_place_rank(country VARCHAR(2),
186 extended_type VARCHAR(1),
187 place_class TEXT, place_type TEXT,
188 admin_level SMALLINT,
191 OUT search_rank SMALLINT,
192 OUT address_rank SMALLINT)
197 IF place_class in ('place','boundary')
198 and place_type in ('postcode','postal_code')
200 SELECT * INTO search_rank, address_rank
201 FROM get_postcode_rank(country, postcode);
202 ELSEIF extended_type = 'N' AND place_class = 'highway' THEN
205 ELSEIF place_class = 'landuse' AND extended_type != 'A' THEN
209 IF place_class = 'boundary' and place_type = 'administrative' THEN
210 classtype = place_type || admin_level::TEXT;
212 classtype = place_type;
215 SELECT l.rank_search, l.rank_address INTO search_rank, address_rank
216 FROM address_levels l
217 WHERE (l.country_code = country or l.country_code is NULL)
218 AND l.class = place_class AND (l.type = classtype or l.type is NULL)
219 ORDER BY l.country_code, l.class, l.type LIMIT 1;
221 IF search_rank is NULL OR address_rank is NULL THEN
226 -- some postcorrections
227 IF place_class = 'waterway' AND extended_type = 'R' THEN
228 -- Slightly promote waterway relations so that they are processed
229 -- before their members.
230 search_rank := search_rank - 1;
234 search_rank := search_rank - 1;
239 LANGUAGE plpgsql IMMUTABLE;
241 CREATE OR REPLACE FUNCTION get_addr_tag_rank(key TEXT, country TEXT,
242 OUT from_rank SMALLINT,
243 OUT to_rank SMALLINT,
253 (SELECT l.rank_search, l.rank_address FROM address_levels l
254 WHERE (l.country_code = country or l.country_code is NULL)
255 AND l.class = 'place' AND l.type = key
256 ORDER BY l.country_code LIMIT 1) r
257 WHERE rank_address > 0
259 extent := reverse_place_diameter(ranks.rank_search);
261 IF ranks.rank_address <= 4 THEN
264 ELSEIF ranks.rank_address <= 9 THEN
267 ELSEIF ranks.rank_address <= 12 THEN
270 ELSEIF ranks.rank_address <= 16 THEN
273 ELSEIF ranks.rank_address <= 21 THEN
276 ELSEIF ranks.rank_address <= 24 THEN
286 LANGUAGE plpgsql IMMUTABLE;
289 CREATE OR REPLACE FUNCTION weigh_search(search_vector INT[],
291 weight_vectors FLOAT[],
299 FOREACH terms IN ARRAY term_vectors
301 IF search_vector @> terms::INTEGER[] THEN
302 RETURN weight_vectors[pos];
309 LANGUAGE plpgsql IMMUTABLE;