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 PARALLEL SAFE;
 
  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 PARALLEL SAFE;
 
  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
 
  91   ELSIF country_code IN ('sg', 'ws', 'st', 'kn') THEN
 
  93   ELSIF country_code IN ('dm', 'mt', 'lc', 'gg', 'sc', 'nr') THEN
 
 101   ELSIF area > 0.01 THEN
 
 103   ELSIF area > 0.001 THEN
 
 105   ELSIF area > 0.0001 THEN
 
 107   ELSIF area > 0.000005 THEN
 
 114 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
 
 117 -- Guess a ranking for postcodes from country and postcode format.
 
 118 CREATE OR REPLACE FUNCTION get_postcode_rank(country_code VARCHAR(2), postcode TEXT,
 
 119                                              OUT rank_search SMALLINT,
 
 120                                              OUT rank_address SMALLINT)
 
 127     postcode := upper(postcode);
 
 129     IF country_code = 'gb' THEN
 
 130         IF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9][A-Z][A-Z])$' THEN
 
 133         ELSEIF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9])$' THEN
 
 136         ELSEIF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z])$' THEN
 
 141     ELSEIF country_code = 'sg' THEN
 
 142         IF postcode ~ '^([0-9]{6})$' THEN
 
 147     ELSEIF country_code = 'de' THEN
 
 148         IF postcode ~ '^([0-9]{5})$' THEN
 
 154         -- Guess at the postcode format and coverage (!)
 
 155         IF postcode ~ '^[A-Z0-9]{1,5}$' THEN -- Probably too short to be very local
 
 159             -- Does it look splitable into and area and local code?
 
 160             part := substring(postcode from '^([- :A-Z0-9]+)([- :][A-Z0-9]+)$');
 
 162             IF part IS NOT NULL THEN
 
 165             ELSEIF postcode ~ '^[- :A-Z0-9]{6,}$' THEN
 
 174 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
 
 177 -- Get standard search and address rank for an object.
 
 179 -- \param country        Two-letter country code where the object is in.
 
 180 -- \param extended_type  OSM type (N, W, R) or area type (A).
 
 181 -- \param place_class    Class (or tag key) of object.
 
 182 -- \param place_type     Type (or tag value) of object.
 
 183 -- \param admin_level    Value of admin_level tag.
 
 184 -- \param is_major       If true, boost search rank by one.
 
 185 -- \param postcode       Value of addr:postcode tag.
 
 186 -- \param[out] search_rank   Computed search rank.
 
 187 -- \param[out] address_rank  Computed address rank.
 
 189 CREATE OR REPLACE FUNCTION compute_place_rank(country VARCHAR(2),
 
 190                                               extended_type VARCHAR(1),
 
 191                                               place_class TEXT, place_type TEXT,
 
 192                                               admin_level SMALLINT,
 
 195                                               OUT search_rank SMALLINT,
 
 196                                               OUT address_rank SMALLINT)
 
 201   IF place_class in ('place','boundary')
 
 202      and place_type in ('postcode','postal_code')
 
 204     SELECT * INTO search_rank, address_rank
 
 205       FROM get_postcode_rank(country, postcode);
 
 206   ELSEIF extended_type = 'N' AND place_class = 'highway' THEN
 
 209   ELSEIF place_class = 'landuse' AND extended_type != 'A' THEN
 
 213     IF place_class = 'boundary' and place_type = 'administrative' THEN
 
 214       classtype = place_type || admin_level::TEXT;
 
 216       classtype = place_type;
 
 219     SELECT l.rank_search, l.rank_address INTO search_rank, address_rank
 
 220       FROM address_levels l
 
 221      WHERE (l.country_code = country or l.country_code is NULL)
 
 222            AND l.class = place_class AND (l.type = classtype or l.type is NULL)
 
 223      ORDER BY l.country_code, l.class, l.type LIMIT 1;
 
 225     IF search_rank is NULL OR address_rank is NULL THEN
 
 230     -- some postcorrections
 
 231     IF place_class = 'waterway' AND extended_type = 'R' THEN
 
 232         -- Slightly promote waterway relations so that they are processed
 
 233         -- before their members.
 
 234         search_rank := search_rank - 1;
 
 238       search_rank := search_rank - 1;
 
 243 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
 
 245 CREATE OR REPLACE FUNCTION get_addr_tag_rank(key TEXT, country TEXT,
 
 246                                              OUT from_rank SMALLINT,
 
 247                                              OUT to_rank SMALLINT,
 
 257       (SELECT l.rank_search, l.rank_address FROM address_levels l
 
 258         WHERE (l.country_code = country or l.country_code is NULL)
 
 259                AND l.class = 'place' AND l.type = key
 
 260         ORDER BY l.country_code LIMIT 1) r
 
 261       WHERE rank_address > 0
 
 263     extent := reverse_place_diameter(ranks.rank_search);
 
 265     IF ranks.rank_address <= 4 THEN
 
 268     ELSEIF ranks.rank_address <= 9 THEN
 
 271     ELSEIF ranks.rank_address <= 12 THEN
 
 274     ELSEIF ranks.rank_address <= 16 THEN
 
 277     ELSEIF ranks.rank_address <= 21 THEN
 
 280     ELSEIF ranks.rank_address <= 24 THEN
 
 290 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
 
 293 CREATE OR REPLACE FUNCTION weigh_search(search_vector INT[],
 
 302     SELECT * FROM json_array_elements(rankings::JSON)
 
 304     IF true = ALL(SELECT x::int = ANY(search_vector) FROM json_array_elements_text(rank->1) as x) THEN
 
 305       RETURN (rank->>0)::float;
 
 311 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;