]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/commitdiff
Continued (and finished if I have not made errors) the coordinate calculator.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Wed, 25 Nov 2009 22:52:44 +0000 (22:52 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Wed, 25 Nov 2009 22:52:44 +0000 (22:52 +0000)
git-svn-id: http://www.winterrodeln.org/svn/servermediawiki/trunk/wradmin@532 7aebc617-e5e2-0310-91dc-80fb5f6d2477

wradmin/wradmin/controllers/coordtool.py
wradmin/wradmin/model/validators.py
wradmin/wradmin/templates/coordtool.html

index 7abcdee7ae9000bd12806bbb5047686984498d05..b60203e15d48766deae1a07f82e8148d1121a1ba 100644 (file)
@@ -5,6 +5,7 @@ import logging
 
 from pylons import request, response, session, tmpl_context as c
 from pylons.controllers.util import abort, redirect_to
+import formencode
 
 from wradmin.lib.base import BaseController, render
 import wradmin.model.validators
@@ -23,17 +24,28 @@ class CoordtoolController(BaseController):
         no_height = request.POST.has_key('no_height')
         simplify = request.POST.has_key('simplify')
         swap_latlon = request.POST.has_key('swap_latlon')
-        no_geoformat = request.POST.has_key('no_geoformat')
-        no_gpxformat = request.POST.has_key('no_gpxformat')
-        no_gmapsformat = request.POST.has_key('no_gmapsformat')
+        c.no_geoformat = request.POST.has_key('no_geoformat')
+        c.no_gpxformat = request.POST.has_key('no_gpxformat')
+        c.no_gmapsformat = request.POST.has_key('no_gmapsformat')
+        c.no_geocachingformat = request.POST.has_key('no_geocachingformat')
         
         if input is None or len(input.strip()) == 0: 
             c.result = None
             return redirect_to(controller='coordtool', action='index')
         
         geo = wradmin.model.validators.MultiGeo()
-        c.result = geo.to_python(input)
+        try: c.result = geo.to_python(input)
+        except formencode.Invalid, e:
+            session['flash'] = unicode(e)
+            session.save()
+            return redirect_to(controller='coordtool', action='index')
+        
+        if swap_latlon:
+            c.result = [(latitude, longitude, elevation) for (longitude, latitude, elevation) in c.result]
+        
         c.geo_winterrodeln = wradmin.model.validators.MultiGeo(output_format = geo.FORMAT_WINTERRODELN)
+        c.geo_gmapplugin = wradmin.model.validators.MultiGeo(output_format = geo.FORMAT_GMAPPLUGIN)
+        c.geo_gpx = wradmin.model.validators.MultiGeo(output_format = geo.FORMAT_GPX)
         c.geo_geocaching = wradmin.model.validators.MultiGeo(output_format = geo.FORMAT_GEOCACHING)
         
         return render('coordtool.html')
\ No newline at end of file
index 25c81d9bb2fba9cb4e2a215b8586292ec9a47d43..9f555e854481166ede3aec4578630655ce54b212 100644 (file)
@@ -1,8 +1,9 @@
 # -*- coding: iso-8859-15 -*-
-
 import formencode
 import datetime
 import re
+import xml.dom.minidom as minidom
+from xml.parsers.expat import ExpatError
 
 
 class GermanBool(formencode.FancyValidator):
@@ -113,7 +114,7 @@ class Geo(formencode.FancyValidator):
 
 
 class MultiGeo(formencode.FancyValidator):
-    "Formats multiple coordinates, even in multiple lines to [(latitude, longitude, height), ...] or [(latitude, longitude, None), ...] tuplets."
+    "Formats multiple coordinates, even in multiple lines to [(latitude, longitude, elevation), ...] or [(latitude, longitude, None), ...] tuplets."
     
     # Valid for input_format
     FORMAT_GUESS = 0         # guesses the input format; default for input_format
@@ -143,8 +144,13 @@ class MultiGeo(formencode.FancyValidator):
         result = []
         for line in lines:
             if input_format == self.FORMAT_GUESS or input_format == self.FORMAT_GEOCACHING:
-                pass
-                
+                r = re.match(u'N ?(\d+)° ?(\d+\.\d+) +E ?(\d+)° ?(\d+\.\d+)', line)
+                if not r is None:
+                    g = r.groups()
+                    result.append((float(g[0]) + float(g[1])/60, float(g[2]) + float(g[3])/60, None))
+                    last_input_format = self.FORMAT_WINTERRODELN
+                    continue
+                    
             if input_format == self.FORMAT_GUESS or input_format == self.FORMAT_WINTERRODELN:
                 r = re.match(u'(\d+\.\d+) N (\d+\.\d+) E', line)
                 if not r is None:
@@ -152,13 +158,27 @@ class MultiGeo(formencode.FancyValidator):
                     last_input_format = self.FORMAT_WINTERRODELN
                     continue
                 
-            if input_format == self.FORMAT_GUESS or input_format == FORMAT_GMAPPLUGIN:
-                pass
+            if input_format == self.FORMAT_GUESS or input_format == self.FORMAT_GMAPPLUGIN:
+                r = re.match(u'(\d+\.\d+), ?(\d+\.\d+)', line)
+                if not r is None:
+                    result.append((float(r.groups()[0]), float(r.groups()[1]), None))
+                    last_input_format = self.FORMAT_GMAPPLUGIN
+                    continue
                 
             if input_format == self.FORMAT_GUESS or input_format == self.FORMAT_GPX:
-                pass
-            
-            raise formencode.Invalid(u"Coordinates '%s' have no valid format" % value, value, state)
+                try:
+                    xml = minidom.parseString(line)
+                    coord = xml.documentElement
+                    lat = float(coord.getAttribute('lat'))
+                    lon = float(coord.getAttribute('lon'))
+                    try: ele = float(coord.childNodes[0].childNodes[0].nodeValue)
+                    except (IndexError, ValueError): ele = None
+                    result.append((lat, lon, ele))
+                    last_input_format = self.FORMAT_GPX
+                    continue
+                except (ExpatError, IndexError, ValueError): pass
+
+            raise formencode.Invalid(u"Coordinates '%s' have no known format" % line, value, state)
             
         return result
     
@@ -173,11 +193,12 @@ class MultiGeo(formencode.FancyValidator):
             elif output_format == self.FORMAT_WINTERRODELN:
                 result.append(u'%.6f N %.6f E' % (latitude, longitude))
 
-            elif output_format == FORMAT_GMAPPLUGIN:
-                pass
+            elif output_format == self.FORMAT_GMAPPLUGIN:
+                result.append(u'%.6f, %.6f' % (latitude, longitude))
                 
             elif output_format == self.FORMAT_GPX:
-                pass
+                if not height is None: result.append(u'<trkpt lat="%.6f" lon="%.6f"><ele>%.2f</ele></trkpt>' % (latitude, longitude, height))
+                else: result.append(u'<trkpt lat="%.6f" lon="%.6f"/>' % (latitude, longitude))
             
             else:
                 raise formencode.Invalid(u"output_format %d is not recognized" % output_format, value, state) # Shouldn't it be an other type of runtime error?
index 9a8d9abfc1b85d4dfd2163ce78dfef564661ea97..3e9243e162ed75adef43915c6ca227a529fa7dfe 100644 (file)
 
 <p py:if="not c.result">Werkzeug für die Koordinatenumrechnung. Das Quellformat braucht nicht angegeben zu werden, es sollte automatisch erkannt werden. Wenn mehr als eine Koordinate gleichzeitig umgerechnet werden soll, dann bitte pro Zeile eine Koordinate angeben.</p>
 
-<h3 py:if="c.result">Winterrodeln-Format</h3>
-<p py:if="c.result">
+<h3 py:if="c.result and not c.no_geoformat">Winterrodeln-Format</h3>
+<p py:if="c.result and not c.no_geoformat">
     <py:for each="line in c.result">${c.geo_winterrodeln.from_python([line])}<br/></py:for>
 </p>
 
-<h3 py:if="c.result">Geocaching-Format</h3>
-<p py:if="c.result">
+<h3 py:if="c.result and not c.no_gmapsformat">Google Maps Plugin Format</h3>
+<p py:if="c.result and not c.no_gmapsformat">
+    <py:for each="line in c.result">${c.geo_gmapplugin.from_python([line])}<br/></py:for>
+</p>
+
+<h3 py:if="c.result and not c.no_gpxformat">GPX Format</h3>
+<p py:if="c.result and not c.no_gpxformat">
+    <py:for each="line in c.result">${c.geo_gpx.from_python([line])}<br/></py:for>
+</p>
+
+<h3 py:if="c.result and not c.no_geocachingformat">Geocaching-Format</h3>
+<p py:if="c.result and not c.no_geocachingformat">
     <py:for each="line in c.result">${c.geo_geocaching.from_python([line])}<br/></py:for>
 </p>
 
 <form action="${h.url_for(controller='coordtool', action='convert')}" method="post">
 <table>
     <tr><th></th><th>Beispiel</th></tr>
-       <tr><td><textarea name="input" cols="70" rows="10"/></td>
+       <tr><td><textarea name="input" cols="80" rows="10"/></td>
     <td>
         <p>47.222134 N 11.467211 E<br/>
            47.222143 N 11.467209 E<br/>
         <p>N 47° 14.912 E 011° 27.432<br/>
            N 47° 14.925 E 011° 27.439<br/>
            N 47° 14.936 E 011° 27.440</p>
+        <p>47.232922, 11.452239<br/>
+           47.233008, 11.452201<br/>
+           47.233810, 11.452150</p>
     </td></tr>
     <tr><td><input type="checkbox" name="no_height" />Höhe weglassen</td><td></td></tr>
-    <tr><td><input type="checkbox" name="simplify" /> Weg vereinfachen</td><td></td></tr>
+    <tr><td><input type="checkbox" name="simplify" /> Weg vereinfachen <em>(noch nicht implementiert)</em></td><td></td></tr>
     <tr><td><input type="checkbox" name="swap_latlon" />Geogr. Länge und Breite vertauschen</td><td></td></tr>
     <tr><td><input type="checkbox" name="no_geoformat" /> Zielformat <strong>&lt;geo&gt;</strong> auslassen</td><td>47.222134 N 11.467211 E</td></tr>
     <tr><td><input type="checkbox" name="no_gpxformat" /> Zielformat <strong>&lt;GPX&gt;</strong> auslassen</td><td>&lt;trkpt lat="47.181289" lon="11.408827"&gt;&lt;ele&gt;1090.57&lt;/ele&gt;&lt;/trkpt&gt;</td></tr>
     <tr><td><input type="checkbox" name="no_gmapsformat" /> Zielformat <strong>Google Maps Plugin</strong> auslassen</td><td>47.232922, 11.452239</td></tr>
+    <tr><td><input type="checkbox" name="no_geocachingformat" /> Zielformat <strong>Geocaching</strong> auslassen</td><td>N 47° 14.029 E 011° 27.129</td></tr>
     <tr><td><input type="submit" value="Konvertieren"/></td><td></td></tr>
 </table>
 </form>