Started to implement function create_wrmap.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Thu, 15 Aug 2013 21:11:08 +0000 (21:11 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Thu, 15 Aug 2013 21:11:08 +0000 (21:11 +0000)
git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@1521 7aebc617-e5e2-0310-91dc-80fb5f6d2477

tests/test_wrmwmarkup.py
wrpylib/wrmwmarkup.py

index d467cb0612ea348fa4b03006941b20d6fa9ec6d9..6523643e231546454eed110f38fd67825cf25de8 100644 (file)
@@ -127,5 +127,50 @@ def test_parse_wrmap():
 
 
 def test_create_wrmap():
-    pass
+    attributes = {'lon': 11.21408895, 'lat': 47.2417134, 'zoom': 14, 'width': 700, 'height': 400}
+    geojson = {
+        'type': 'FeatureCollection',
+        'features':
+            [{
+                'type': 'feature',
+                'geometry': {
+                    'type': 'Point',
+                    'coordinates': [11.190454, 47.240689]},
+                'properties': {'type': 'inn', 'name': u'Rosskogelhütte', 'wiki': u'Rosskogelhütte'}
+            }, {
+                'type': 'feature',
+                'geometry': {
+                    'type': 'Point',
+                    'coordinates': [11.238971, 47.245789]},
+                'properties': {'type': 'carpark'}
+            }, {
+                'type': 'feature',
+                'geometry': {
+                    'type': 'Point',
+                    'coordinates': [11.238283, 47.245711]},
+                'properties': {'type': 'busstop', 'name': u'Oberperfuss Rangger Köpfl Lift'}
+            }, {
+                'type': 'feature',
+                'geometry': {
+                    'type': 'LineString',
+                    'coordinates': [
+                        [11.203360, 47.238587],
+                        [11.230868, 47.244951],
+                        [11.237853, 47.245470]]},
+                'properties': {}
+            }]
+    }
+
+    wikitext = wrpylib.wrmwmarkup.create_wrmap(attributes, geojson)
+    assert 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>'''
 
index 28ac1538887800f07a344dd78b8482759af5f7cf..371f904832779f7e2eed3c91c4d0f919e0298b38 100644 (file)
@@ -10,6 +10,8 @@ import formencode
 import wrpylib.wrvalidators
 import wrpylib.mwmarkup
 
+WRMAP_POINT_TYPE = {'gasthaus': 'inn', 'haltestelle': 'busstop', 'parkplatz': 'carpark', 'achtung': 'attention', 'punkt': 'point'}
+WRMAP_LINE_TYPE = {'rodelbahn': 'sledrun', 'gehweg': 'walk', 'alternative': 'alternative', 'lift': 'lift', 'linie': 'line'}
 
 def _conv(fnct, value, fieldname):
     """Internal function.
@@ -363,8 +365,8 @@ def parse_googlemap(wikitext):
                 'coordinates': [[lon, lat] for lon, lat, symbol, title in entries]},
             'properties': properties})
 
-    json = {'type': 'FeatureCollection', 'features': json_features}
-    return {'lon': center[0], 'lat': center[1], 'zoom': zoom}, json
+    geojson = {'type': 'FeatureCollection', 'features': json_features}
+    return {'lon': center[0], 'lat': center[1], 'zoom': zoom}, geojson
 
 
 def parse_wrmap_coordinates(coords):
@@ -417,18 +419,16 @@ def parse_wrmap(wikitext):
 
     # convert XML to geojson (http://www.geojson.org/geojson-spec.html)
     json_features = []
-    point_type = {'gasthaus': 'inn', 'haltestelle': 'busstop', 'parkplatz': 'carpark', 'achtung': 'attention', 'punkt': 'point'}
-    line_type = {'rodelbahn': 'sledrun', 'gehweg': 'walk', 'alternative': 'alternative', 'lift': 'lift', 'linie': 'line'}
     for feature in wrmap_xml:
         # determine feature type
-        is_point = point_type.has_key(feature.tag)
-        is_line = line_type.has_key(feature.tag)
+        is_point = WRMAP_POINT_TYPE.has_key(feature.tag)
+        is_line = WRMAP_LINE_TYPE.has_key(feature.tag)
         if (not is_point and not is_line):
             raise RuntimeError('Unknown element <{}>.'.format(feature.tag))
 
         # point
         if is_point:
-            properties = {'type': point_type[feature.tag]}
+            properties = {'type': WRMAP_POINT_TYPE[feature.tag]}
             allowed_properties = set(['name', 'wiki'])
             wrong_properties = set(feature.attrib.keys()) - allowed_properties
             if len(wrong_properties) > 0:
@@ -444,7 +444,7 @@ def parse_wrmap(wikitext):
 
         # line
         if is_line:
-            properties = {'type': line_type[feature.tag]}
+            properties = {'type': WRMAP_LINE_TYPE[feature.tag]}
             allowed_properties = set(['farbe', 'dicke'])
             wrong_properties = set(feature.attrib.keys()) - allowed_properties
             if len(wrong_properties) > 0:
@@ -463,7 +463,7 @@ def parse_wrmap(wikitext):
                 'geometry': {'type': 'LineString', 'coordinates': parse_wrmap_coordinates(feature.text)},
                 'properties': properties})
 
-    json = {
+    geojson = {
         'type': 'FeatureCollection',
         'features': json_features}
 
@@ -491,11 +491,35 @@ def parse_wrmap(wikitext):
         raise RuntimeError('Attribute "height" has to be an integer value.')
     # show_sledruns = (wrmap_xml.tag == 'wrgmap')
 
-    return attributes, json
+    return attributes, geojson
 
 
-def create_wrmap(geojson):
-    """Creates a <wrmap> wikitext from geojson."""
-    pass
-
+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(attributes, geojson):
+    """Creates a <wrmap> wikitext from geojson (as python types)."""
+    wrmap_xml = xml.etree.ElementTree.Element('wrmap')
+    wrmap_xml.attrib = attributes
+
+    assert geojson['type'] == 'FeatureCollection'
+    json_features = geojson['features']
+    for json_feature in json_features:
+        geo = json_feature['geometry']
+        if geo['type'] == 'Point':
+            wrmap_type = dict(zip(WRMAP_POINT_TYPE.values(), WRMAP_POINT_TYPE.keys()))
+            feature_xml = xml.etree.ElementTree.SubElement(wrmap_xml, wrmap_type[json_feature['properties']['type']])
+            feature_xml.text = create_wrmap_coordinates([geo['coordinates']])
+            feature_xml.attrib = json_feature['properties']
+        if geo['type'] == 'LineString':
+            wrmap_type = dict(zip(WRMAP_LINE_TYPE.values(), WRMAP_LINE_TYPE.keys()))
+            feature_xml = xml.etree.ElementTree.SubElement(wrmap_xml, wrmap_type[json_feature['properties']['type']])
+            feature_xml.text = create_wrmap_coordinates(geo['coordinates'])
+            feature_xml.attrib = json_feature['properties']
+
+    return xml.etree.ElementTree.tostring(wrmap_xml)