Browse Source

Work around ElementTree namespace brokenness.

debian-sid
Tom Payne 14 years ago
parent
commit
f3700ae901
  1. 16
      igc2kmz/gpx.py
  2. 26
      igc2kmz/task.py
  3. 41
      igc2kmz/xc.py

16
igc2kmz/gpx.py

@ -16,6 +16,7 @@
from datetime import datetime
import re
try:
from xml.etree.cElementTree import parse
except ImportError:
@ -25,7 +26,6 @@ from coord import Coord
from track import Track
GPX_NAMESPACE = 'http://www.topografix.com/GPX/1/1'
GPX_DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
@ -38,7 +38,7 @@ class gpx_tag(object):
attrs = {
'creator': 'http://github.com/twpayne/igc2kmz/wikis',
'version': '1.1',
'xmlns': GPX_NAMESPACE,
'xmlns': 'http://www.topografix.com/GPX/1/1',
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation': 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd',
}
@ -56,13 +56,13 @@ class GPX(object):
self.filename = file.name
except AttributeError:
self.filename = '(unknown)'
element = parse(file)
namespace = re.match('\{(.*)\}', element.getroot().tag).group(1)
ele_tag_name = '{%s}ele' % namespace
time_tag_name = '{%s}time' % namespace
self.coords = []
ele_tag_name = '{%s}ele' % GPX_NAMESPACE
time_tag_name = '{%s}time' % GPX_NAMESPACE
for trkpt in parse(file).findall('/{%s}trk/{%s}trkseg/{%s}trkpt'
% (GPX_NAMESPACE,
GPX_NAMESPACE,
GPX_NAMESPACE)):
for trkpt in element.findall('/{%s}trk/{%s}trkseg/{%s}trkpt'
% (namespace, namespace, namespace)):
lat = float(trkpt.get('lat'))
lon = float(trkpt.get('lon'))
ele_tag = trkpt.find(ele_tag_name)

26
igc2kmz/task.py

@ -18,6 +18,7 @@
from __future__ import with_statement
import datetime
import re
try:
from xml.etree.cElementTree import ElementTree, TreeBuilder, parse
except ImportError:
@ -25,7 +26,7 @@ except ImportError:
from coord import Coord
from etree import tag
from gpx import GPX_DATETIME_FORMAT, GPX_NAMESPACE
from gpx import GPX_DATETIME_FORMAT
class Turnpoint(object):
@ -80,25 +81,25 @@ class Turnpoint(object):
return tb
@classmethod
def from_element(cls, element):
name = element.findtext('{%s}name' % GPX_NAMESPACE).encode('utf_8')
desc_tag = element.find('{%s}desc' % GPX_NAMESPACE)
def from_element(cls, element, namespace):
name = element.findtext('{%s}name' % namespace).encode('utf_8')
desc_tag = element.find('{%s}desc' % namespace)
desc = None if desc_tag is None else desc_tag.text.encode('utf_8')
lat = float(element.get('lat'))
lon = float(element.get('lon'))
ele_tag = element.find('{%s}ele' % GPX_NAMESPACE)
ele_tag = element.find('{%s}ele' % namespace)
ele = 0 if ele_tag is None else int(ele_tag.text)
time_tag = element.find('{%s}time' % GPX_NAMESPACE)
time_tag = element.find('{%s}time' % namespace)
if time_tag is None:
dt = None
else:
dt = datetime.datetime.strptime(time_tag.text, GPX_DATETIME_FORMAT)
coord = Coord.deg(lat, lon, ele, dt)
radius_tag = element.find('{%s}extensions/{%s}radius'
% (GPX_NAMESPACE, GPX_NAMESPACE))
% (namespace, namespace))
radius = 400 if radius_tag is None else int(radius_tag.text)
enter = element.find('{%s}extensions/{%s}exit'
% (GPX_NAMESPACE, GPX_NAMESPACE)) is None
% (namespace, namespace)) is None
return cls(name, coord, radius, enter, desc)
@ -121,14 +122,15 @@ class Task(object):
return self.build_tree(TreeBuilder()).close()
@classmethod
def from_element(cls, element):
name_tag = element.find('{%s}name' % GPX_NAMESPACE)
def from_element(cls, element, namespace):
name_tag = element.find('{%s}name' % namespace)
name = None if name_tag is None else name_tag.text.encode('utf_8')
rtepts = element.findall('{%s}rtept' % GPX_NAMESPACE)
rtepts = element.findall('{%s}rtept' % namespace)
tps = map(Turnpoint.from_element, rtepts)
return cls(name, tps)
@classmethod
def from_file(cls, file):
element = parse(file)
return cls.from_element(element.find('/{%s}rte' % GPX_NAMESPACE))
namespace = re.match('\{(.*)\}', element.getroot().tag).group(1)
return cls.from_element(element.find('/{%s}rte' % namespace), element)

41
igc2kmz/xc.py

@ -18,6 +18,7 @@
from __future__ import with_statement
from datetime import datetime
import re
try:
from xml.etree.cElementTree import ElementTree, parse
except ImportError:
@ -25,7 +26,7 @@ except ImportError:
from coord import Coord
from etree import tag
from gpx import GPX_DATETIME_FORMAT, GPX_NAMESPACE
from gpx import GPX_DATETIME_FORMAT
class Turnpoint(object):
@ -47,13 +48,13 @@ class Turnpoint(object):
return tb
@classmethod
def from_element(cls, rtept):
name = rtept.findtext('{%s}name' % GPX_NAMESPACE).encode('utf_8')
def from_element(cls, rtept, namespace):
name = rtept.findtext('{%s}name' % namespace).encode('utf_8')
lat = float(rtept.get('lat'))
lon = float(rtept.get('lon'))
ele_tag = rtept.find('{%s}ele' % GPX_NAMESPACE)
ele_tag = rtept.find('{%s}ele' % namespace)
ele = 0 if ele_tag is None else int(ele_tag.text)
time_text = rtept.findtext('{%s}time' % GPX_NAMESPACE)
time_text = rtept.findtext('{%s}time' % namespace)
dt = datetime.strptime(time_text, GPX_DATETIME_FORMAT)
coord = Coord.deg(lat, lon, ele, dt)
return cls(name, coord)
@ -91,21 +92,20 @@ class Route(object):
return tb
@classmethod
def from_element(cls, rte):
name = rte.findtext('{%s}name' % GPX_NAMESPACE).encode('utf_8')
extensions_tag = rte.find('{%s}extensions' % GPX_NAMESPACE)
league_text = extensions_tag.findtext('{%s}league' % GPX_NAMESPACE)
def from_element(cls, rte, namespace):
name = rte.findtext('{%s}name' % namespace).encode('utf_8')
extensions_tag = rte.find('{%s}extensions' % namespace)
league_text = extensions_tag.findtext('{%s}league' % namespace)
league = league_text.encode('utf_8')
distance_text = extensions_tag.findtext('{%s}distance' % GPX_NAMESPACE)
distance_text = extensions_tag.findtext('{%s}distance' % namespace)
distance = float(distance_text)
multiplier_text = extensions_tag.findtext('{%s}multiplier'
% GPX_NAMESPACE)
multiplier_text = extensions_tag.findtext('{%s}multiplier' % namespace)
multiplier = float(multiplier_text)
score = float(extensions_tag.findtext('{%s}score' % GPX_NAMESPACE))
circuit_tag = extensions_tag.find('{%s}circuit' % GPX_NAMESPACE)
score = float(extensions_tag.findtext('{%s}score' % namespace))
circuit_tag = extensions_tag.find('{%s}circuit' % namespace)
circuit = not circuit_tag is None
rtepts = rte.findall('{%s}rtept' % GPX_NAMESPACE)
tps = map(Turnpoint.from_element, rtepts)
rtepts = rte.findall('{%s}rtept' % namespace)
tps = [Turnpoint.from_element(rtept, namespace) for rtept in rtepts]
return cls(name, league, distance, multiplier, score, circuit, tps)
@ -120,12 +120,13 @@ class XC(object):
return tb
@classmethod
def from_element(cls, element):
rtes = element.findall('/{%s}rte' % GPX_NAMESPACE)
routes = map(Route.from_element, rtes)
def from_element(cls, element, namespace):
rtes = element.findall('/{%s}rte' % namespace)
routes = [Route.from_element(rte, namespace) for rte in rtes]
return cls(routes)
@classmethod
def from_file(cls, file):
element = parse(file)
return cls.from_element(element)
namespace = re.match(r'\{(.*)\}', element.getroot().tag).group(1)
return cls.from_element(element, namespace)

Loading…
Cancel
Save