forked from giggls/openstreetmap-carto-de
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosm_tag2num.sql
62 lines (50 loc) · 1.95 KB
/
osm_tag2num.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Generate a floating point number from a numeric OSM tag
Unfortunately this contains a lot of heuristic :(
This function is handy for the generation of a numeric pseudo column
in a database view.
(c) 2016 Sven Geggus <[email protected]>
*/
CREATE or REPLACE FUNCTION osm_tag2num(tag text) RETURNS REAL AS $$
DECLARE
num real;
feet boolean;
BEGIN
feet=false;
/* remove potential crap inside parentheses */
tag=regexp_replace(tag, '\(.*\)', '', 'gi');
/* remove leading or trailing whitespace */
tag=regexp_replace(tag, '^\s+(.*)\s+$', '\1', 'g');
/* check if unit is given in feet and convert later*/
if (right(tag,2) = 'ft') THEN feet=true; END IF;
if (right(tag,3) = 'ft.') THEN feet=true; END IF;
if (right(tag,4) = 'feet') THEN feet=true; END IF;
if (right(tag,1) = '′') THEN feet=true; END IF;
if (right(tag,1) = '''') THEN feet=true; END IF;
/* general assumption:
<alphanumeric_string>.<somenumber> should be interpreted as
<somenumber> not 0.<somenumber>
<alphanumeric_string> .<somenumber> should be interpreted as
0.<somenumber>
So get just get rid of the dot in strings of the form
<alphanumeric_string>.<somenumber>
Example:
ca.5m
*/
tag=regexp_replace(tag, '([[:alpha:]])\.([0-9])', '\1 \2', 'gi');
/* remove the remaining leading and trailing garbage */
tag=regexp_replace(tag, '^[[:alpha:]:~ ><]*\.?? *([^ [:alpha:]]*) *[ [:alpha:]′''\.]*$', '\1', 'gi');
/* , seems to be used more often in its german form as a
decimal mark rather than as a thousands separator so let's
asume that this is always the case */
tag=replace(tag, ',', '.');
BEGIN
num=tag::real;
EXCEPTION WHEN OTHERS THEN
-- RAISE NOTICE 'Invalid integer value: "%". Returning NULL.', tag;
num=NULL;
END;
/* convert feet to meters */
if feet THEN num=0.3048*num; END IF;
return num;
END;
$$ LANGUAGE 'plpgsql' IMMUTABLE;