Implemented parse_wrmap_coordinates.
[philipp/winterrodeln/wrpylib.git] / wrpylib / wrmwmarkup.py
index 9dbebe9fd91f2a8bb78df1a6e930728037f95aaf..c968b20d4005453f620b2d32bd33599c74117db4 100644 (file)
@@ -373,20 +373,17 @@ def parse_wrmap_coordinates(coords):
     47.13 N 11.70 E
     ->
     [[11.87, 47.12], [11.70, 47.13]]'''
-    '''
-        $matches = array();
-        $num_matches = preg_match_all('/\s*(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*/', $input, $matches);
-        $result = array();
-        for ($i=0; $i!=$num_matches; ++$i) {
-            $result[] = array(floatval($matches[2][$i]), floatval($matches[1][$i]));
-        }
-        if (implode($matches[0]) != $input) throw new Exception('Falsches Koordinatenformat: ' . $input);
-        return $result;
-    }
-
-    Think of using re.sub(pattern, repl, string, count=0, flags=0)
-    '''
-    return [[0, 0]] # TODO
+    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):