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)
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;
81 ELSIF area > 0.01 THEN
83 ELSIF area > 0.001 THEN
85 ELSIF area > 0.0001 THEN
87 ELSIF area > 0.000005 THEN
94 LANGUAGE plpgsql IMMUTABLE;
97 -- Guess a ranking for postcodes from country and postcode format.
98 CREATE OR REPLACE FUNCTION get_postcode_rank(country_code VARCHAR(2), postcode TEXT,
99 OUT rank_search SMALLINT,
100 OUT rank_address SMALLINT)
107 postcode := upper(postcode);
109 IF country_code = 'gb' THEN
110 IF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9][A-Z][A-Z])$' THEN
113 ELSEIF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9])$' THEN
116 ELSEIF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z])$' THEN
121 ELSEIF country_code = 'sg' THEN
122 IF postcode ~ '^([0-9]{6})$' THEN
127 ELSEIF country_code = 'de' THEN
128 IF postcode ~ '^([0-9]{5})$' THEN
134 -- Guess at the postcode format and coverage (!)
135 IF postcode ~ '^[A-Z0-9]{1,5}$' THEN -- Probably too short to be very local
139 -- Does it look splitable into and area and local code?
140 part := substring(postcode from '^([- :A-Z0-9]+)([- :][A-Z0-9]+)$');
142 IF part IS NOT NULL THEN
145 ELSEIF postcode ~ '^[- :A-Z0-9]{6,}$' THEN
154 LANGUAGE plpgsql IMMUTABLE;
157 -- Get standard search and address rank for an object.
159 -- \param country Two-letter country code where the object is in.
160 -- \param extended_type OSM type (N, W, R) or area type (A).
161 -- \param place_class Class (or tag key) of object.
162 -- \param place_type Type (or tag value) of object.
163 -- \param admin_level Value of admin_level tag.
164 -- \param is_major If true, boost search rank by one.
165 -- \param postcode Value of addr:postcode tag.
166 -- \param[out] search_rank Computed search rank.
167 -- \param[out] address_rank Computed address rank.
169 CREATE OR REPLACE FUNCTION compute_place_rank(country VARCHAR(2),
170 extended_type VARCHAR(1),
171 place_class TEXT, place_type TEXT,
172 admin_level SMALLINT,
175 OUT search_rank SMALLINT,
176 OUT address_rank SMALLINT)
181 IF place_class in ('place','boundary')
182 and place_type in ('postcode','postal_code')
184 SELECT * INTO search_rank, address_rank
185 FROM get_postcode_rank(country, postcode);
186 ELSEIF extended_type = 'N' AND place_class = 'highway' THEN
189 ELSEIF place_class = 'landuse' AND extended_type != 'A' THEN
193 IF place_class = 'boundary' and place_type = 'administrative' THEN
194 classtype = place_type || admin_level::TEXT;
196 classtype = place_type;
199 SELECT l.rank_search, l.rank_address INTO search_rank, address_rank
200 FROM address_levels l
201 WHERE (l.country_code = country or l.country_code is NULL)
202 AND l.class = place_class AND (l.type = classtype or l.type is NULL)
203 ORDER BY l.country_code, l.class, l.type LIMIT 1;
205 IF search_rank is NULL THEN
209 IF address_rank is NULL THEN
213 -- some postcorrections
214 IF place_class = 'waterway' AND extended_type = 'R' THEN
215 -- Slightly promote waterway relations so that they are processed
216 -- before their members.
217 search_rank := search_rank - 1;
221 search_rank := search_rank - 1;
226 LANGUAGE plpgsql IMMUTABLE;