4 """This module contains winterrodeln specific functions that are processing the MediaWiki markup.
7 import xml.etree.ElementTree
9 import mwparserfromhell
10 import wrpylib.wrvalidators
11 import wrpylib.mwmarkup
13 from wrpylib.wrvalidators import LonLat, opt_lonlat_from_str, opt_lonlat_to_str, opt_uint_from_str, opt_uint_to_str, \
14 opt_str_opt_comment_enum_to_str, lift_german_to_str, webauskunft_to_str, cachet_german_to_str, \
15 opt_phone_comment_enum_to_str, lift_german_from_str, GASTHAUSBOX_DICT
18 def sledrun_from_rodelbahnbox(value, sledrun):
19 """Takes a Rodelbahnbox as returned by rodelbahnbox_from_str (that is, an OrderedDict) and
20 updates the sledrun instance with all values present in the Rodelbahnbox. Other values are not
21 updated. Does not validate the arguments."""
22 # sledrun.page_id = None # this field is not updated because it is not present in the RodelbahnBox
23 # sledrun.page_title = None # this field is not updated because it is not present in the RodelbahnBox
24 # sledrun.name_url = None # this field is not updated because it is not present in the RodelbahnBox
25 sledrun.position_longitude, sledrun.position_latitude = value['Position']
26 sledrun.top_longitude, sledrun.top_latitude = value['Position oben']
27 sledrun.top_elevation = value['Höhe oben']
28 sledrun.bottom_longitude, sledrun.bottom_latitude = value['Position unten']
29 sledrun.bottom_elevation = value['Höhe unten']
30 sledrun.length = value['Länge']
31 sledrun.difficulty = value['Schwierigkeit']
32 sledrun.avalanches = value['Lawinen']
33 sledrun.operator = value['Betreiber']
34 sledrun.public_transport = value['Öffentliche Anreise']
35 sledrun.walkup_possible = value['Aufstieg möglich']
36 sledrun.walkup_time = value['Gehzeit']
37 sledrun.walkup_separate, sledrun.walkup_separate_comment = value['Aufstieg getrennt']
38 sledrun.lift = None if value['Aufstiegshilfe'] is None else len(value['Aufstiegshilfe']) > 0
39 sledrun.lift_details = lift_german_to_str(value['Aufstiegshilfe'])
40 sledrun.night_light, sledrun.night_light_comment = value['Beleuchtungsanlage']
41 sledrun.night_light_days, sledrun.night_light_days_comment = value['Beleuchtungstage']
42 sledrun.sled_rental = None if value['Rodelverleih'] is None else len(value['Rodelverleih']) > 0
43 sledrun.sled_rental_comment = opt_str_opt_comment_enum_to_str(value['Rodelverleih'])
44 sledrun.cachet = cachet_german_to_str(value['Gütesiegel'])
45 sledrun.information_web = webauskunft_to_str(value['Webauskunft'])
46 sledrun.information_phone = opt_phone_comment_enum_to_str(value['Telefonauskunft'])
47 sledrun.image = value['Bild']
48 sledrun.show_in_overview = value['In Übersichtskarte']
49 sledrun.forum_id = value['Forumid']
50 # sledrun.under_construction = None # this field is not updated because it is not present in the RodelbahnBox
54 def sledrun_to_rodelbahnbox(sledrun):
55 """Takes a sledrun instance that might come from the database and converts it to a OrderedDict ready
56 to be formatted as RodelbahnBox."""
57 value = collections.OrderedDict()
58 value['Position'] = LonLat(sledrun.position_longitude, sledrun.position_latitude)
59 value['Position oben'] = LonLat(sledrun.top_longitude, sledrun.top_latitude)
60 value['Höhe oben'] = sledrun.top_elevation
61 value['Position unten'] = LonLat(sledrun.bottom_longitude, sledrun.bottom_latitude)
62 value['Höhe unten'] = sledrun.bottom_elevation
63 value['Länge'] = sledrun.length
64 value['Schwierigkeit'] = sledrun.difficulty
65 value['Lawinen'] = sledrun.avalanches
66 value['Betreiber'] = sledrun.operator
67 value['Öffentliche Anreise'] = sledrun.public_transport
68 value['Aufstieg möglich'] = sledrun.walkup_possible
69 value['Gehzeit'] = sledrun.walkup_time
70 value['Aufstieg getrennt'] = sledrun.walkup_separate, sledrun.walkup_separate_comment
71 value['Aufstiegshilfe'] = lift_german_from_str(sledrun.lift_details)
72 value['Beleuchtungsanlage'] = sledrun.night_light, sledrun.night_light_comment
73 value['Beleuchtungstage'] = sledrun.night_light_days, sledrun.night_light_days_comment
74 value['Rodelverleih'] = sledrun.sled_rental, sledrun.sled_rental_comment
75 value['Gütesiegel'] = sledrun.cachet
76 value['Webauskunft'] = sledrun.information_web
77 value['Telefonauskunft'] = sledrun.information_phone
78 value['Bild'] = sledrun.image
79 value['In Übersichtskarte'] = sledrun.show_in_overview
80 value['Forumid'] = sledrun.forum_id
84 def inn_from_gasthausbox(value, inn):
85 """Converts a dict with Gasthausbox properties to a Inn class. Does no validation.
86 value is a dict of properties as returned by gasthausbox_from_str."""
87 # page_id = None # this field is not updated because it is not present in the Gasthausbox
88 # page_title = None # this field is not updated because it is not present in the Gasthausbox
89 def convtodb(value, key):
90 v = GASTHAUSBOX_DICT[key].to_str(value[key])
94 inn.position_longitude, inn.position_latitude = value['Position']
95 inn.position_elevation = value['Höhe']
96 inn.operator = value['Betreiber']
97 inn.seats = value['Sitzplätze']
98 inn.overnight, inn.overnight_comment = value['Übernachtung']
99 inn.smoker_area = None if value['Rauchfrei'] is None else value['Rauchfrei'] < 0.9
100 inn.nonsmoker_area = None if value['Rauchfrei'] is None else value['Rauchfrei'] > 0.1
101 inn.sled_rental, inn.sled_rental_comment = value['Rodelverleih']
102 inn.mobile_provider = convtodb(value, 'Handyempfang')
103 inn.homepage = convtodb(value, 'Homepage')
104 inn.email_list = convtodb(value, 'E-Mail')
105 inn.phone_list = convtodb(value, 'Telefon')
106 inn.image = value['Bild']
107 inn.sledding_list = convtodb(value, 'Rodelbahnen')
108 # under_construction = None # this field is not updated because it is not present in the GasthausBox
112 def inn_to_gasthausbox(inn):
113 """Converts an inn class to a dict of Gasthausbox properties. value is an Inn instance."""
114 def convfromdb(value, key):
115 v = '' if value is None else value
116 return GASTHAUSBOX_DICT[key].from_str(v)
117 value = collections.OrderedDict()
118 value['Position'] = LonLat(inn.position_longitude, inn.position_latitude)
119 value['Höhe'] = inn.position_elevation
120 value['Betreiber'] = inn.operator
121 value['Sitzplätze'] = inn.seats
122 value['Übernachtung'] = (inn.overnight, inn.overnight_comment)
123 value['Rauchfrei'] = {(False, True): 0.0, (True, True): 0.5, (True, False): 1.0}.get((inn.nonsmoker_area, inn.smoker_area), None)
124 value['Rodelverleih'] = (inn.sled_rental, inn.sled_rental_comment)
125 value['Handyempfang'] = convfromdb(inn.mobile_provider, 'Handyempfang')
126 value['Homepage'] = convfromdb(inn.homepage, 'Homepage')
127 value['E-Mail'] = convfromdb(inn.email_list, 'E-Mail')
128 value['Telefon'] = convfromdb(inn.phone_list, 'Telefon')
129 value['Bild'] = inn.image
130 value['Rodelbahnen'] = convfromdb(inn.sledding_list, 'Rodelbahnen')
134 def lonlat_ele_from_template(template):
135 """Template is a mwparserfromhell.nodes.template.Template instance. Returns (lonlat, ele)."""
136 lonlat = opt_lonlat_from_str(template.params[0].strip())
137 ele = opt_uint_from_str(template.params[1].strip())
141 def latlon_ele_to_template(lonlat_ele, name):
142 lonlat, ele = lonlat_ele
143 template = mwparserfromhell.nodes.template.Template(name)
144 template.add(1, opt_lonlat_to_str(lonlat))
145 template.add(2, opt_uint_to_str(ele))
146 wrpylib.mwmarkup.format_template_oneline(template)
150 class ParseError(RuntimeError):
151 """Exception used by some of the functions"""
155 def parse_wrmap_coordinates(coords):
156 """gets a string coordinates and returns an array of lon/lat coordinate pairs, e.g.
160 [[11.87, 47.12], [11.70, 47.13]]"""
163 for match in re.finditer(r'\s*(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*', coords):
164 if match.start() != pos:
166 result.append([float(match.groups()[1]), float(match.groups()[0])])
169 if pos == len(coords):
171 raise RuntimeError('Wrong coordinate format: {}'.format(coords))
174 WRMAP_POINT_TYPES = ['gasthaus', 'haltestelle', 'parkplatz', 'achtung', 'foto', 'verleih', 'punkt']
175 WRMAP_LINE_TYPES = ['rodelbahn', 'gehweg', 'alternative', 'lift', 'anfahrt', 'linie']
178 def parse_wrmap(wikitext):
179 """Parses the '<wrmap ...>content</wrmap>' of the Winterrodeln wrmap extension.
180 If wikitext does not contain the <wrmap> tag or if the <wrmap> tag contains
181 invalid formatted lines, a ParseError is raised.
182 Use wrpylib.mwmarkup.find_tag(wikitext, 'wrmap') to find the wrmap tag within an arbitrary
183 wikitext before using this function.
185 :param wikitext: wikitext containing only the template. Example:
188 <wrmap lat="47.2417134" lon="11.21408895" zoom="14" width="700" height="400">
189 <gasthaus name="Rosskogelhütte" wiki="Rosskogelhütte">47.240689 11.190454</gasthaus>
190 <parkplatz>47.245789 11.238971</parkplatz>
191 <haltestelle name="Oberperfuss Rangger Köpfl Lift">47.245711 11.238283</haltestelle>
199 :returns: GeoJSON as nested Python datatype
203 wrmap_xml = xml.etree.ElementTree.fromstring(wikitext)
204 except xml.etree.ElementTree.ParseError as e:
205 row, column = e.position
206 raise ParseError("XML parse error on row {}, column {}: {}".format(row, column, e))
207 if wrmap_xml.tag not in ['wrmap', 'wrgmap']:
208 raise ParseError('No valid tag name')
210 # convert XML to geojson (http://www.geojson.org/geojson-spec.html)
212 for feature in wrmap_xml:
213 # determine feature type
214 is_point = feature.tag in WRMAP_POINT_TYPES
215 is_line = feature.tag in WRMAP_LINE_TYPES
216 if (not is_point and not is_line):
217 raise ParseError('Unknown element <{}>.'.format(feature.tag))
221 properties = {'type': feature.tag}
222 allowed_properties = {'name', 'wiki'}
223 wrong_properties = set(feature.attrib.keys()) - allowed_properties
224 if len(wrong_properties) > 0:
225 raise ParseError("The attribute '{}' is not allowed at <{}>.".format(list(wrong_properties)[0], feature.tag))
226 properties.update(feature.attrib)
227 coordinates = parse_wrmap_coordinates(feature.text)
228 if len(coordinates) != 1:
229 raise ParseError('The element <{}> has to have exactly one coordinate pair.'.format(feature.tag))
230 json_features.append({
232 'geometry': {'type': 'Point', 'coordinates': coordinates[0]},
233 'properties': properties})
237 properties = {'type': feature.tag}
238 allowed_properties = {'farbe', 'dicke'}
239 wrong_properties = set(feature.attrib.keys()) - allowed_properties
240 if len(wrong_properties) > 0:
241 raise ParseError("The attribute '{}' is not allowed at <{}>.".format(list(wrong_properties)[0], feature.tag))
242 if 'farbe' in feature.attrib:
243 if not re.match('#[0-9a-fA-F]{6}$', feature.attrib['farbe']):
244 raise ParseError('The attribute "farbe" has to have a format like "#a0bb43".')
245 properties['strokeColor'] = feature.attrib['farbe'] # e.g. #a200b7
246 if 'dicke' in feature.attrib:
248 properties['strokeWidth'] = int(feature.attrib['dicke']) # e.g. 6
250 raise ParseError('The attribute "dicke" has to be an integer.')
251 json_features.append({
253 'geometry': {'type': 'LineString', 'coordinates': parse_wrmap_coordinates(feature.text)},
254 'properties': properties})
258 for k, v in wrmap_xml.attrib.items():
259 if k in ['lat', 'lon']:
261 properties[k] = float(v)
263 raise ParseError('Attribute "{}" has to be a float value.'.format(k))
264 elif k in ['zoom', 'width', 'height']:
266 properties[k] = int(v)
268 raise ParseError('Attribute "{}" has to be an integer value.'.format(k))
270 raise ParseError('Unknown attribute "{}".'.format(k))
273 'type': 'FeatureCollection',
274 'features': json_features,
275 'properties': properties}
280 def create_wrmap_coordinates(coords):
283 result.append('{:.6f} N {:.6f} E'.format(coord[1], coord[0]))
284 return '\n'.join(result)
287 def create_wrmap(geojson):
288 """Creates a <wrmap> wikitext from geojson (as python types)."""
289 wrmap_xml = xml.etree.ElementTree.Element('wrmap')
290 wrmap_xml.text = '\n\n'
291 for k, v in geojson['properties'].items():
292 if k in ['lon', 'lat']:
293 wrmap_xml.attrib[k] = '{:.6f}'.format(v)
295 wrmap_xml.attrib[k] = str(v)
297 assert geojson['type'] == 'FeatureCollection'
298 json_features = geojson['features']
299 last_json_feature = None
300 for json_feature in json_features:
301 feature_xml = xml.etree.ElementTree.SubElement(wrmap_xml, json_feature['properties']['type'])
302 geo = json_feature['geometry']
303 if geo['type'] == 'Point':
304 feature_xml.text = create_wrmap_coordinates([geo['coordinates']])
305 if last_json_feature is not None:
306 last_json_feature.tail = '\n'
308 if last_json_feature is not None:
309 last_json_feature.tail = '\n\n'
310 feature_xml.text = '\n' + create_wrmap_coordinates(geo['coordinates']) + '\n'
311 last_json_feature = feature_xml
312 feature_xml.attrib = json_feature['properties']
313 del feature_xml.attrib['type']
315 if last_json_feature is not None:
316 last_json_feature.tail = '\n\n'
317 return xml.etree.ElementTree.tostring(wrmap_xml, encoding='utf-8').decode('utf-8')