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
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
# -*- 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):
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
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:
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
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?
<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><geo></strong> auslassen</td><td>47.222134 N 11.467211 E</td></tr>
<tr><td><input type="checkbox" name="no_gpxformat" /> Zielformat <strong><GPX></strong> auslassen</td><td><trkpt lat="47.181289" lon="11.408827"><ele>1090.57</ele></trkpt></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>