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;