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
120 CREATE OR REPLACE FUNCTION compute_place_rank(country VARCHAR(2),
122 place_class TEXT, place_type TEXT,
123 admin_level SMALLINT,
124 is_area BOOLEAN, is_major BOOLEAN,
126 OUT search_rank SMALLINT,
127 OUT address_rank SMALLINT)
132 IF place_class in ('place','boundary')
133 and place_type in ('postcode','postal_code')
135 SELECT * INTO search_rank, address_rank
136 FROM get_postcode_rank(country, postcode);
141 ELSEIF osm_type = 'N' AND place_class = 'highway' THEN
144 ELSEIF place_class = 'landuse' AND NOT is_area THEN
148 IF place_class = 'boundary' and place_type = 'administrative' THEN
149 classtype = place_type || admin_level::TEXT;
151 classtype = place_type;
154 SELECT l.rank_search, l.rank_address INTO search_rank, address_rank
155 FROM address_levels l
156 WHERE (l.country_code = country or l.country_code is NULL)
157 AND l.class = place_class AND (l.type = classtype or l.type is NULL)
158 ORDER BY l.country_code, l.class, l.type LIMIT 1;
160 IF search_rank is NULL THEN
164 IF address_rank is NULL THEN
168 -- some postcorrections
169 IF place_class = 'waterway' AND osm_type = 'R' THEN
170 -- Slightly promote waterway relations so that they are processed
171 -- before their members.
172 search_rank := search_rank - 1;
176 search_rank := search_rank - 1;
181 LANGUAGE plpgsql IMMUTABLE;