Implemented parse_wrmap_coordinates.
[philipp/winterrodeln/wrpylib.git] / wrpylib / wrmwmarkup.py
index 3392e4e10cd59d763c7aeb0adb77413d02ce7c0b..c968b20d4005453f620b2d32bd33599c74117db4 100644 (file)
@@ -367,8 +367,23 @@ def parse_googlemap(wikitext):
     return {'lon': center[0], 'lat': center[1], 'zoom': zoom}, json
 
 
-def parse_wrmap_coordinates(coordinates_str):
-    return [[0, 0]] # TODO
+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):
@@ -453,11 +468,11 @@ def parse_wrmap(wikitext):
 
     # attributes # TODO: check
     attributes = {}
-    attributes['latitude']  = float(wrmap_xml.attrib.get('lat', 47.267648)) # center lat
-    attributes['longitude'] = float(wrmap_xml.attrib.get('lon', 11.404655)) # center lon
-    attributes['zoom']      = int(wrmap_xml.attrib.get('zoom', 10))         # Google Zoom Level
-    attributes['width']     = int(wrmap_xml.attrib['width']) if wrmap_xml.attrib.has_key('width') else None # None corresponds to 100%
-    attributes['height']    = int(wrmap_xml.attrib.get('height', 450))      # map height in px
+    attributes['lat']    = float(wrmap_xml.attrib.get('lat', 47.267648)) # center lat
+    attributes['lon']    = float(wrmap_xml.attrib.get('lon', 11.404655)) # center lon
+    attributes['zoom']   = int(wrmap_xml.attrib.get('zoom', 10))         # Google Zoom Level
+    attributes['width']  = int(wrmap_xml.attrib['width']) if wrmap_xml.attrib.has_key('width') else None # None corresponds to 100%
+    attributes['height'] = int(wrmap_xml.attrib.get('height', 450))      # map height in px
     # show_sledruns = (wrmap_xml.tag == 'wrgmap')
 
     return attributes, json