Cleanup: strip trailing space, remove BOM
[blender-addons.git] / sun_position / geo.py
blob59c27e3926e125487004e36aa6f8e0102a40c162
1 #!/usr/bin/env python
3 # geo.py is a python module with no dependencies on extra packages,
4 # providing some convenience functions for working with geographic
5 # coordinates
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 ###
26 import re
29 class Parser:
30 """ A parser class using regular expressions. """
32 def __init__(self):
33 self.patterns = {}
34 self.raw_patterns = {}
35 self.virtual = {}
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
43 try:
44 self.patterns[name] = ("(?:" + pattern + ")") % self.patterns
45 except KeyError as e:
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
52 sub_dict = {}
53 subpattern_names = []
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)
60 else:
61 sub_dict[subpattern_name] = self.patterns[subpattern_name]
63 pattern = "^" + (self.raw_patterns[pattern_name] % sub_dict) + "$"
65 # do matching
66 m = re.match(pattern, text)
68 if m == None:
69 return None
71 # build tree recursively by parsing subgroups
72 tree = {"TEXT": text}
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)
80 return tree
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]*)?")
92 position_parser.add(
93 "nmea_style", r"%(sign)s?\s*%(nmea_style_degrees)s%(nmea_style_minutes)s")
95 position_parser.add(
96 "number",
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|″|\"",
105 True)
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")
109 position_parser.add(
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"
114 position_parser.add(
115 "coordinates_ns",
116 r"%(nmea_style)s|%(plain_degrees)s|%(degree_coordinates)s")
117 position_parser.add(
118 "coordinates_ew",
119 r"%(nmea_style)s|%(plain_degrees)s|%(degree_coordinates)s")
121 position_parser.add(
122 "position", """\
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*\
128 """)
131 def get_number(b):
132 """ Takes appropriate branch of parse tree and returns float. """
133 s = b["TEXT"].replace(",", ".")
134 return float(s)
137 def get_coordinate(b):
138 """ Takes appropriate branch of the parse tree and returns degrees as a float. """
140 r = 0.
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"] == "-":
149 r *= -1.
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"] == "-":
154 r *= -1.
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"):
161 r += get_number(
162 b["degree_coordinates"]["seconds"]["number"]) / 3600.
163 if b["degree_coordinates"].get(
164 "sign") and b["degree_coordinates"]["sign"]["TEXT"] == "-":
165 r *= -1.
167 return r
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
177 lat_sign = +1.
178 if parse_tree.get(
179 "direction_ns") and parse_tree["direction_ns"]["TEXT"] in ("S",
180 "s"):
181 lat_sign = -1.
183 lon_sign = +1.
184 if parse_tree.get(
185 "direction_ew") and parse_tree["direction_ew"]["TEXT"] in ("W",
186 "w"):
187 lon_sign = -1.
189 lat = lat_sign * get_coordinate(parse_tree["coordinates_ns"])
190 lon = lon_sign * get_coordinate(parse_tree["coordinates_ew"])
192 return lat, lon