3 # geo.py is a python module with no dependencies on extra packages,
4 # providing some convenience functions for working with geographic
7 # Copyright (C) 2010 Maximilian Hoegner <hp.maxi@hoegners.de>
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ### Part one - Functions for dealing with points on a sphere ###
25 ### Part two - A tolerant parser for position strings ###
30 """ A parser class using regular expressions. """
34 self
.raw_patterns
= {}
37 def add(self
, name
, pattern
, virtual
=False):
38 """ Adds a new named pattern (regular expression) that can reference previously added patterns by %(pattern_name)s.
39 Virtual patterns can be used to make expressions more compact but don't show up in the parse tree. """
40 self
.raw_patterns
[name
] = "(?:" + pattern
+ ")"
41 self
.virtual
[name
] = virtual
44 self
.patterns
[name
] = ("(?:" + pattern
+ ")") % self
.patterns
46 raise (Exception, "Unknown pattern name: %s" % str(e
))
48 def parse(self
, pattern_name
, text
):
49 """ Parses 'text' with pattern 'pattern_name' and returns parse tree """
51 # build pattern with subgroups
54 for s
in re
.finditer("%\(.*?\)s", self
.raw_patterns
[pattern_name
]):
55 subpattern_name
= s
.group()[2:-2]
56 if not self
.virtual
[subpattern_name
]:
57 sub_dict
[subpattern_name
] = "(" + self
.patterns
[
58 subpattern_name
] + ")"
59 subpattern_names
.append(subpattern_name
)
61 sub_dict
[subpattern_name
] = self
.patterns
[subpattern_name
]
63 pattern
= "^" + (self
.raw_patterns
[pattern_name
] % sub_dict
) + "$"
66 m
= re
.match(pattern
, text
)
71 # build tree recursively by parsing subgroups
74 for i
in range(len(subpattern_names
)):
75 text_part
= m
.group(i
+ 1)
76 if not text_part
== None:
77 subpattern
= subpattern_names
[i
]
78 tree
[subpattern
] = self
.parse(subpattern
, text_part
)
83 position_parser
= Parser()
84 position_parser
.add("direction_ns", r
"[NSns]")
85 position_parser
.add("direction_ew", r
"[EOWeow]")
86 position_parser
.add("decimal_separator", r
"[\.,]", True)
87 position_parser
.add("sign", r
"[+-]")
89 position_parser
.add("nmea_style_degrees", r
"[0-9]{2,}")
90 position_parser
.add("nmea_style_minutes",
91 r
"[0-9]{2}(?:%(decimal_separator)s[0-9]*)?")
93 "nmea_style", r
"%(sign)s?\s*%(nmea_style_degrees)s%(nmea_style_minutes)s")
97 r
"[0-9]+(?:%(decimal_separator)s[0-9]*)?|%(decimal_separator)s[0-9]+")
99 position_parser
.add("plain_degrees", r
"(?:%(sign)s\s*)?%(number)s")
101 position_parser
.add("degree_symbol", r
"°", True)
102 position_parser
.add("minutes_symbol", r
"'|′|`|´", True)
103 position_parser
.add("seconds_symbol",
104 r
"%(minutes_symbol)s%(minutes_symbol)s|″|\"",
106 position_parser.add("degrees
", r"%(number)s\s
*%(degree_symbol)s")
107 position_parser.add("minutes
", r"%(number)s\s
*%(minutes_symbol)s")
108 position_parser.add("seconds
", r"%(number)s\s
*%(seconds_symbol)s")
110 "degree_coordinates
",
111 "(?
:%(sign)s\s
*)?
%(degrees)s(?
:[+\s
]*%(minutes)s)?
(?
:[+\s
]*%(seconds)s)?|
(?
:%(sign)s\s
*)%(minutes)s(?
:[+\s
]*%(seconds)s)?|
(?
:%(sign)s\s
*)%(seconds)s"
116 r"%(nmea_style)s|
%(plain_degrees)s|
%(degree_coordinates)s")
119 r"%(nmea_style)s|
%(plain_degrees)s|
%(degree_coordinates)s")
123 \s*%(direction_ns)s\s*%(coordinates_ns)s[,;\s]*%(direction_ew)s\s*%(coordinates_ew)s\s*|\
124 \s*%(direction_ew)s\s*%(coordinates_ew)s[,;\s]*%(direction_ns)s\s*%(coordinates_ns)s\s*|\
125 \s*%(coordinates_ns)s\s*%(direction_ns)s[,;\s]*%(coordinates_ew)s\s*%(direction_ew)s\s*|\
126 \s*%(coordinates_ew)s\s*%(direction_ew)s[,;\s]*%(coordinates_ns)s\s*%(direction_ns)s\s*|\
127 \s*%(coordinates_ns)s[,;\s]+%(coordinates_ew)s\s*\
132 """ Takes appropriate branch of parse tree and returns float. """
133 s = b["TEXT
"].replace(",", ".")
137 def get_coordinate(b):
138 """ Takes appropriate branch of the parse tree and returns degrees as a float. """
142 if b.get("nmea_style
"):
143 if b["nmea_style
"].get("nmea_style_degrees
"):
144 r += get_number(b["nmea_style
"]["nmea_style_degrees
"])
145 if b["nmea_style
"].get("nmea_style_minutes
"):
146 r += get_number(b["nmea_style
"]["nmea_style_minutes
"]) / 60.
147 if b["nmea_style
"].get(
148 "sign
") and b["nmea_style
"]["sign
"]["TEXT
"] == "-":
150 elif b.get("plain_degrees
"):
151 r += get_number(b["plain_degrees
"]["number
"])
152 if b["plain_degrees
"].get(
153 "sign
") and b["plain_degrees
"]["sign
"]["TEXT
"] == "-":
155 elif b.get("degree_coordinates
"):
156 if b["degree_coordinates
"].get("degrees
"):
157 r += get_number(b["degree_coordinates
"]["degrees
"]["number
"])
158 if b["degree_coordinates
"].get("minutes
"):
159 r += get_number(b["degree_coordinates
"]["minutes
"]["number
"]) / 60.
160 if b["degree_coordinates
"].get("seconds
"):
162 b["degree_coordinates
"]["seconds
"]["number
"]) / 3600.
163 if b["degree_coordinates
"].get(
164 "sign
") and b["degree_coordinates
"]["sign
"]["TEXT
"] == "-":
170 def parse_position(s):
171 """ Takes a (utf8-encoded) string describing a position and returns a tuple of floats for latitude and longitude in degrees.
172 Tries to be as tolerant as possible with input. Returns None if parsing doesn't succeed. """
174 parse_tree = position_parser.parse("position
", s)
175 if parse_tree == None: return None
179 "direction_ns
") and parse_tree["direction_ns
"]["TEXT
"] in ("S
",
185 "direction_ew
") and parse_tree["direction_ew
"]["TEXT
"] in ("W
",
189 lat = lat_sign * get_coordinate(parse_tree["coordinates_ns
"])
190 lon = lon_sign * get_coordinate(parse_tree["coordinates_ew
"])