def tcx_parser(fh):
it = ElementTree.iterparse(fh, events=('start','end'))
# look for the start TrainingCenterDatabase tag to fail fast
for event, elem in it:
if event == 'start' and elem.tag.endswith('}TrainingCenterDatabase'):
break
else:
raise ValueError('Not a tcx file: %s' % fh.name)
# do the main parse
for event, elem in it:
if event == 'end' and elem.tag.endswith('}Trackpoint'):
latlon = None
elev = np.nan
time = None
for child in elem:
tag_name = child.tag.rsplit('}', 1)[1]
if tag_name == 'Time':
time = child.text
elif tag_name == 'AltitudeMeters':
elev = float(child.text)
elif tag_name == 'Position':
vals = dict((c.tag.rsplit('}', 1)[1], float(c.text)) for c in child)
latlon = (vals['LatitudeDegrees'], vals['LongitudeDegrees'])
if latlon is not None:
yield latlon, time, elev
elem.clear()
评论列表
文章目录