From 850dd7ca473eaeafdb3cd9538aa4f015dfa04d18 Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 25 Nov 2009 22:52:44 +0000 Subject: [PATCH] Continued (and finished if I have not made errors) the coordinate calculator. git-svn-id: http://www.winterrodeln.org/svn/servermediawiki/trunk/wradmin@532 7aebc617-e5e2-0310-91dc-80fb5f6d2477 --- wradmin/wradmin/controllers/coordtool.py | 20 ++++++++--- wradmin/wradmin/model/validators.py | 45 +++++++++++++++++------- wradmin/wradmin/templates/coordtool.html | 26 ++++++++++---- 3 files changed, 69 insertions(+), 22 deletions(-) diff --git a/wradmin/wradmin/controllers/coordtool.py b/wradmin/wradmin/controllers/coordtool.py index 7abcdee..b60203e 100644 --- a/wradmin/wradmin/controllers/coordtool.py +++ b/wradmin/wradmin/controllers/coordtool.py @@ -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 diff --git a/wradmin/wradmin/model/validators.py b/wradmin/wradmin/model/validators.py index 25c81d9..9f555e8 100644 --- a/wradmin/wradmin/model/validators.py +++ b/wradmin/wradmin/model/validators.py @@ -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'%.2f' % (latitude, longitude, height)) + else: result.append(u'' % (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? diff --git a/wradmin/wradmin/templates/coordtool.html b/wradmin/wradmin/templates/coordtool.html index 9a8d9ab..3e9243e 100644 --- a/wradmin/wradmin/templates/coordtool.html +++ b/wradmin/wradmin/templates/coordtool.html @@ -16,20 +16,30 @@

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.

-

Winterrodeln-Format

-

+

Winterrodeln-Format

+

${c.geo_winterrodeln.from_python([line])}

-

Geocaching-Format

-

+

Google Maps Plugin Format

+

+ ${c.geo_gmapplugin.from_python([line])}
+

+ +

GPX Format

+

+ ${c.geo_gpx.from_python([line])}
+

+ +

Geocaching-Format

+

${c.geo_geocaching.from_python([line])}

-
Beispiel