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;
58 -- Compute a base address rank from the extent of the given geometry.
60 -- This is all simple guess work. We don't need particularly good estimates
61 -- here. This just avoids to have very high ranked address parts in features
62 -- that span very large areas (or vice versa).
63 CREATE OR REPLACE FUNCTION geometry_to_rank(search_rank SMALLINT, geometry GEOMETRY, country_code TEXT)
69 IF ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') THEN
70 area := ST_Area(geometry);
71 ELSIF ST_GeometryType(geometry) in ('ST_LineString','ST_MultiLineString') THEN
72 area := (ST_Length(geometry)^2) * 0.1;
77 -- adjust for the fact that countries come in different sizes
78 IF country_code IN ('ca', 'au', 'ru') THEN
80 ELSIF country_code IN ('br', 'kz', 'cn', 'us', 'ne', 'gb', 'za', 'sa', 'id', 'eh', 'ml', 'tm') THEN
82 ELSIF country_code IN ('bo', 'ar', 'sd', 'mn', 'in', 'et', 'cd', 'mz', 'ly', 'cl', 'zm') THEN
90 ELSIF area > 0.01 THEN
92 ELSIF area > 0.001 THEN
94 ELSIF area > 0.0001 THEN
96 ELSIF area > 0.000005 THEN
103 LANGUAGE plpgsql IMMUTABLE;
106 -- Guess a ranking for postcodes from country and postcode format.
107 CREATE OR REPLACE FUNCTION get_postcode_rank(country_code VARCHAR(2), postcode TEXT,
108 OUT rank_search SMALLINT,
109 OUT rank_address SMALLINT)
116 postcode := upper(postcode);
118 IF country_code = 'gb' THEN
119 IF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9][A-Z][A-Z])$' THEN
122 ELSEIF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9])$' THEN
125 ELSEIF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z])$' THEN
130 ELSEIF country_code = 'sg' THEN
131 IF postcode ~ '^([0-9]{6})$' THEN
136 ELSEIF country_code = 'de' THEN
137 IF postcode ~ '^([0-9]{5})$' THEN
143 -- Guess at the postcode format and coverage (!)
144 IF postcode ~ '^[A-Z0-9]{1,5}$' THEN -- Probably too short to be very local
148 -- Does it look splitable into and area and local code?
149 part := substring(postcode from '^([- :A-Z0-9]+)([- :][A-Z0-9]+)$');
151 IF part IS NOT NULL THEN
154 ELSEIF postcode ~ '^[- :A-Z0-9]{6,}$' THEN
163 LANGUAGE plpgsql IMMUTABLE;
166 -- Get standard search and address rank for an object.
168 -- \param country Two-letter country code where the object is in.
169 -- \param extended_type OSM type (N, W, R) or area type (A).
170 -- \param place_class Class (or tag key) of object.
171 -- \param place_type Type (or tag value) of object.
172 -- \param admin_level Value of admin_level tag.
173 -- \param is_major If true, boost search rank by one.
174 -- \param postcode Value of addr:postcode tag.
175 -- \param[out] search_rank Computed search rank.
176 -- \param[out] address_rank Computed address rank.
178 CREATE OR REPLACE FUNCTION compute_place_rank(country VARCHAR(2),
179 extended_type VARCHAR(1),
180 place_class TEXT, place_type TEXT,
181 admin_level SMALLINT,
184 OUT search_rank SMALLINT,
185 OUT address_rank SMALLINT)
190 IF place_class in ('place','boundary')
191 and place_type in ('postcode','postal_code')
193 SELECT * INTO search_rank, address_rank
194 FROM get_postcode_rank(country, postcode);
195 ELSEIF extended_type = 'N' AND place_class = 'highway' THEN
198 ELSEIF place_class = 'landuse' AND extended_type != 'A' THEN
202 IF place_class = 'boundary' and place_type = 'administrative' THEN
203 classtype = place_type || admin_level::TEXT;
205 classtype = place_type;
208 SELECT l.rank_search, l.rank_address INTO search_rank, address_rank
209 FROM address_levels l
210 WHERE (l.country_code = country or l.country_code is NULL)
211 AND l.class = place_class AND (l.type = classtype or l.type is NULL)
212 ORDER BY l.country_code, l.class, l.type LIMIT 1;
214 IF search_rank is NULL OR address_rank is NULL THEN
219 -- some postcorrections
220 IF place_class = 'waterway' AND extended_type = 'R' THEN
221 -- Slightly promote waterway relations so that they are processed
222 -- before their members.
223 search_rank := search_rank - 1;
227 search_rank := search_rank - 1;
232 LANGUAGE plpgsql IMMUTABLE;
234 CREATE OR REPLACE FUNCTION get_addr_tag_rank(key TEXT, country TEXT,
235 OUT from_rank SMALLINT,
236 OUT to_rank SMALLINT,
246 (SELECT l.rank_search, l.rank_address FROM address_levels l
247 WHERE (l.country_code = country or l.country_code is NULL)
248 AND l.class = 'place' AND l.type = key
249 ORDER BY l.country_code LIMIT 1) r
250 WHERE rank_address > 0
252 extent := reverse_place_diameter(ranks.rank_search);
254 IF ranks.rank_address <= 4 THEN
257 ELSEIF ranks.rank_address <= 9 THEN
260 ELSEIF ranks.rank_address <= 12 THEN
263 ELSEIF ranks.rank_address <= 16 THEN
266 ELSEIF ranks.rank_address <= 21 THEN
269 ELSEIF ranks.rank_address <= 24 THEN
279 LANGUAGE plpgsql IMMUTABLE;