Now the width and height of the map are parsed from <googlemap> as well.
[philipp/winterrodeln/wrpylib.git] / wrpylib / wrmwmarkup.py
index 62bf20a4e1f0030f129be282568f42ee190e8348..bcd230f5eac640642fc7d090abd43e30215d13a0 100644 (file)
@@ -1,14 +1,23 @@
-#!/usr/bin/python2.6
+#!/usr/bin/python2.7
 # -*- coding: iso-8859-15 -*-
 # $Id$
 # $HeadURL$
 """This module contains winterrodeln specific functions that are prcocessing the MediaWiki markup.
 """
 import re
+import xml.etree.ElementTree
 import formencode
 import wrpylib.wrvalidators
 import wrpylib.mwmarkup
 
+WRMAP_POINT_TYPES = ['gasthaus', 'haltestelle', 'parkplatz', 'achtung', 'punkt']
+WRMAP_LINE_TYPES = ['rodelbahn', 'gehweg', 'alternative', 'lift', 'anfahrt', 'linie']
+
+
+class ParseError(RuntimeError):
+    """Exception used by some of the functions"""
+    pass
+
 
 def _conv(fnct, value, fieldname):
     """Internal function.
@@ -314,3 +323,202 @@ def find_all_templates(wikitext, find_func):
             result = (start, end) + result[2:]
     return results
 
+
+def googlemap_to_wrmap(attributes, coords, paths):
+    """Converts the output of parse_googlemap to the GeoJSON format wrmap uses.
+    :returns: (GeoJSON as nested Python datatypes)
+    """
+    json_features = []
+
+    # point
+    for point in coords:
+        lon, lat, symbol, title = point
+        properties = {'type': 'punkt' if symbol is None else symbol.lower()}
+        if title is not None: properties['name'] = title
+        json_features.append({
+            'type': 'Feature',
+            'geometry': {'type': 'Point', 'coordinates': [lon, lat]},
+            'properties': properties})
+        
+    # path
+    for path in paths:
+        style, entries = path
+        style = style.lower()
+        PATH_TYPES = {u'6#ff014e9a': u'rodelbahn', u'6#ffe98401': u'gehweg', u'6#ff7f7fff': u'alternative', u'3#ff000000': u'lift', u'3#ffe1e100': u'anfahrt'}
+        if PATH_TYPES.has_key(style):
+            properties = {'type': PATH_TYPES[style]}
+        else:
+            properties = {'type': 'line'}
+            properties['dicke'] = style[0]
+            properties['farbe'] = style[4:]
+        json_features.append({
+            'type': 'Feature',
+            'geometry': {
+                'type': 'LineString',
+                'coordinates': [[lon, lat] for lon, lat, symbol, title in entries]},
+            'properties': properties})
+
+    geojson = {
+            'type': 'FeatureCollection',
+            'features': json_features,
+            'properties': attributes}
+    return geojson
+
+
+def parse_wrmap_coordinates(coords):
+    '''gets a string coordinates and returns an array of lon/lat coordinate pairs, e.g.
+    47.12 N 11.87 E
+    47.13 N 11.70 E
+    ->
+    [[11.87, 47.12], [11.70, 47.13]]'''
+    result = []
+    pos = 0
+    for match in re.finditer(r'\s*(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*', coords):
+        if match.start() != pos:
+            break
+        result.append([float(match.groups()[1]), float(match.groups()[0])])
+        pos = match.end()
+    else:
+        if pos == len(coords):
+            return result
+    raise RuntimeError('Wrong coordinate format: {}'.format(coords))
+
+
+def parse_wrmap(wikitext):
+    """Parses the (unicode) u'<wrmap ...>content</wrmap>' of the Winterrodeln wrmap extension.
+    If wikitext does not contain the <wrmap> tag or if the <wrmap> tag contains 
+    invalid formatted lines, a ParseError is raised.
+    Use wrpylib.mwmarkup.find_tag(wikitext, 'wrmap') to find the wrmap tag within an arbitrary
+    wikitext before using this function.
+
+    :param wikitext: wikitext containing only the template. Example:
+
+    wikitext = u'''
+    <wrmap lat="47.2417134" lon="11.21408895" zoom="14" width="700" height="400">
+    <gasthaus name="Rosskogelhütte" wiki="Rosskogelhütte">47.240689 11.190454</gasthaus>
+    <parkplatz>47.245789 11.238971</parkplatz>
+    <haltestelle name="Oberperfuss Rangger Köpfl Lift">47.245711 11.238283</haltestelle>
+    <rodelbahn>
+        47.238587 11.203360
+        47.244951 11.230868
+        47.245470 11.237853
+    </rodelbahn>
+    </wrmap>
+    '''
+    :returns: GeoJSON as nested Python datatype
+    """
+    # parse XML
+    try:
+        wrmap_xml = xml.etree.ElementTree.fromstring(wikitext.encode('utf-8'))
+    except xml.etree.ElementTree.ParseError as e:
+        row, column = e.position
+        raise ParseError("XML parse error on row {}, column {}: {}".format(row, column, e))
+    if wrmap_xml.tag not in ['wrmap', 'wrgmap']:
+        raise ParseError('No valid tag name')
+
+    # convert XML to geojson (http://www.geojson.org/geojson-spec.html)
+    json_features = []
+    for feature in wrmap_xml:
+        # determine feature type
+        is_point = feature.tag in WRMAP_POINT_TYPES
+        is_line = feature.tag in WRMAP_LINE_TYPES
+        if (not is_point and not is_line):
+            raise ParseError('Unknown element <{}>.'.format(feature.tag))
+
+        # point
+        if is_point:
+            properties = {'type': feature.tag}
+            allowed_properties = set(['name', 'wiki'])
+            wrong_properties = set(feature.attrib.keys()) - allowed_properties
+            if len(wrong_properties) > 0:
+                raise ParseError("The attribute '{}' is not allowed at <{}>.".format(list(wrong_properties)[0], feature.tag))
+            properties.update(feature.attrib)
+            coordinates = parse_wrmap_coordinates(feature.text)
+            if len(coordinates) != 1:
+                raise ParseError('The element <{}> has to have exactly one coordinate pair.'.format(feature.tag))
+            json_features.append({
+                'type': 'Feature',
+                'geometry': {'type': 'Point', 'coordinates': coordinates[0]},
+                'properties': properties})
+
+        # line
+        if is_line:
+            properties = {'type': feature.tag}
+            allowed_properties = set(['farbe', 'dicke'])
+            wrong_properties = set(feature.attrib.keys()) - allowed_properties
+            if len(wrong_properties) > 0:
+                raise ParseError("The attribute '{}' is not allowed at <{}>.".format(list(wrong_properties)[0], feature.tag))
+            if feature.attrib.has_key('farbe'): 
+                if not re.match('#[0-9a-fA-F]{6}$', feature.attrib['farbe']):
+                    raise ParseError('The attribute "farbe" has to have a format like "#a0bb43".')
+                properties['strokeColor'] = feature.attrib['farbe'] # e.g. #a200b7
+            if feature.attrib.has_key('dicke'):
+                try:
+                    properties['strokeWidth'] = int(feature.attrib['dicke']) # e.g. 6
+                except ValueError:
+                    raise ParseError('The attribute "dicke" has to be an integer.')
+            json_features.append({
+                'type': 'Feature',
+                'geometry': {'type': 'LineString', 'coordinates': parse_wrmap_coordinates(feature.text)},
+                'properties': properties})
+
+    # attributes
+    properties = {}
+    for k, v in wrmap_xml.attrib.iteritems():
+        if k in ['lat', 'lon']:
+            try:
+                properties[k] = float(v)
+            except ValueError:
+                raise ParseError('Attribute "{}" has to be a float value.'.format(k))
+        elif k in ['zoom', 'width', 'height']:
+            try:
+                properties[k] = int(v)
+            except ValueError:
+                raise ParseError('Attribute "{}" has to be an integer value.'.format(k))
+        else:
+            raise ParseError('Unknown attribute "{}".'.format(k))
+
+    geojson = {
+        'type': 'FeatureCollection',
+        'features': json_features,
+        'properties': properties}
+
+    return geojson
+
+
+def create_wrmap_coordinates(coords):
+    result = []
+    for coord in coords:
+        result.append('{:.6f} N {:.6f} E'.format(coord[1], coord[0]))
+    return '\n'.join(result)
+
+def create_wrmap(geojson):
+    """Creates a <wrmap> wikitext from geojson (as python types)."""
+    wrmap_xml = xml.etree.ElementTree.Element('wrmap')
+    wrmap_xml.text = '\n\n'
+    for k, v in geojson['properties'].iteritems():
+        wrmap_xml.attrib[k] = str(v)
+
+    assert geojson['type'] == 'FeatureCollection'
+    json_features = geojson['features']
+    last_json_feature = None
+    for json_feature in json_features:
+        feature_xml = xml.etree.ElementTree.SubElement(wrmap_xml, json_feature['properties']['type'])
+        geo = json_feature['geometry']
+        if geo['type'] == 'Point':
+            feature_xml.text = create_wrmap_coordinates([geo['coordinates']])
+            if last_json_feature is not None:
+                last_json_feature.tail = '\n'
+        else:
+            if last_json_feature is not None:
+                last_json_feature.tail = '\n\n'
+            feature_xml.text = '\n' + create_wrmap_coordinates(geo['coordinates']) + '\n'
+        last_json_feature = feature_xml
+        feature_xml.attrib = json_feature['properties']
+        del feature_xml.attrib['type']
+
+    if last_json_feature is not None:
+        last_json_feature.tail = '\n\n'
+    return xml.etree.ElementTree.tostring(wrmap_xml, encoding='utf-8').decode('utf-8')
+