1 -- Functions related to search and address ranks
3 -- Return an approximate search radius according to the search rank.
4 CREATE OR REPLACE FUNCTION reverse_place_diameter(rank_search SMALLINT)
8 IF rank_search <= 4 THEN
10 ELSIF rank_search <= 8 THEN
12 ELSIF rank_search <= 12 THEN
14 ELSIF rank_search <= 17 THEN
16 ELSIF rank_search <= 18 THEN
18 ELSIF rank_search <= 19 THEN
25 LANGUAGE plpgsql IMMUTABLE;
28 -- Return an approximate update radius according to the search rank.
29 CREATE OR REPLACE FUNCTION update_place_diameter(rank_search SMALLINT)
34 IF rank_search = 11 or rank_search = 5 THEN
36 -- anything higher than city is effectively ignored (polygon required)
37 ELSIF rank_search < 16 THEN
39 ELSIF rank_search < 18 THEN
41 ELSIF rank_search < 20 THEN
43 ELSIF rank_search = 21 THEN
45 ELSIF rank_search < 24 THEN
47 ELSIF rank_search < 26 THEN
49 ELSIF rank_search < 28 THEN
56 LANGUAGE plpgsql IMMUTABLE;
59 -- Guess a ranking for postcodes from country and postcode format.
60 CREATE OR REPLACE FUNCTION get_postcode_rank(country_code VARCHAR(2), postcode TEXT,
61 OUT rank_search SMALLINT,
62 OUT rank_address SMALLINT)
69 postcode := upper(postcode);
71 IF country_code = 'gb' THEN
72 IF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9][A-Z][A-Z])$' THEN
75 ELSEIF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9])$' THEN
78 ELSEIF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z])$' THEN
83 ELSEIF country_code = 'sg' THEN
84 IF postcode ~ '^([0-9]{6})$' THEN
89 ELSEIF country_code = 'de' THEN
90 IF postcode ~ '^([0-9]{5})$' THEN
96 -- Guess at the postcode format and coverage (!)
97 IF postcode ~ '^[A-Z0-9]{1,5}$' THEN -- Probably too short to be very local
101 -- Does it look splitable into and area and local code?
102 part := substring(postcode from '^([- :A-Z0-9]+)([- :][A-Z0-9]+)$');
104 IF part IS NOT NULL THEN
107 ELSEIF postcode ~ '^[- :A-Z0-9]{6,}$' THEN
116 LANGUAGE plpgsql IMMUTABLE;
119 -- Get standard search and address rank for an object.
121 -- \param country Two-letter country code where the object is in.
122 -- \param extended_type OSM type (N, W, R) or area type (A).
123 -- \param place_class Class (or tag key) of object.
124 -- \param place_type Type (or tag value) of object.
125 -- \param admin_level Value of admin_level tag.
126 -- \param is_major If true, boost search rank by one.
127 -- \param postcode Value of addr:postcode tag.
128 -- \param[out] search_rank Computed search rank.
129 -- \param[out] address_rank Computed address rank.
131 CREATE OR REPLACE FUNCTION compute_place_rank(country VARCHAR(2),
132 extended_type VARCHAR(1),
133 place_class TEXT, place_type TEXT,
134 admin_level SMALLINT,
137 OUT search_rank SMALLINT,
138 OUT address_rank SMALLINT)
143 IF place_class in ('place','boundary')
144 and place_type in ('postcode','postal_code')
146 SELECT * INTO search_rank, address_rank
147 FROM get_postcode_rank(country, postcode);
149 IF NOT extended_type = 'A' THEN
152 ELSEIF extended_type = 'N' AND place_class = 'highway' THEN
155 ELSEIF place_class = 'landuse' AND extended_type != 'A' THEN
159 IF place_class = 'boundary' and place_type = 'administrative' THEN
160 classtype = place_type || admin_level::TEXT;
162 classtype = place_type;
165 SELECT l.rank_search, l.rank_address INTO search_rank, address_rank
166 FROM address_levels l
167 WHERE (l.country_code = country or l.country_code is NULL)
168 AND l.class = place_class AND (l.type = classtype or l.type is NULL)
169 ORDER BY l.country_code, l.class, l.type LIMIT 1;
171 IF search_rank is NULL THEN
175 IF address_rank is NULL THEN
179 -- some postcorrections
180 IF place_class = 'waterway' AND extended_type = 'R' THEN
181 -- Slightly promote waterway relations so that they are processed
182 -- before their members.
183 search_rank := search_rank - 1;
187 search_rank := search_rank - 1;
192 LANGUAGE plpgsql IMMUTABLE;