def _create_summary(sum): d = OrderedDict(( ('start', format_timestamp(sum.start)), ('end', format_timestamp(sum.end)), ('distance', sum.distance), ('calories', sum.calories), ('ride_time', sum.ride_time), ('altitude_gain', sum.altitude_gain), ('altitude_loss', sum.altitude_loss), )) if sum.speed is not None: d['speed'] = OrderedDict( (('avg', sum.speed.avg), ('max', sum.speed.max))) if sum.heartrate is not None and sum.heartrate.max > 0: d['heartrate'] = OrderedDict( (('avg', sum.heartrate.avg), ('max', sum.heartrate.max))) if sum.cadence is not None and sum.cadence.max > 0: d['cadence'] = OrderedDict( (('avg', sum.cadence.avg), ('max', sum.cadence.max))) if sum.watts is not None and sum.watts.max > 0: d['watts'] = OrderedDict( (('avg', sum.watts.avg), ('max', sum.watts.max))) return d
def _create_summary(sum): d = OrderedDict(( ('start', format_timestamp(sum.start)), ('end', format_timestamp(sum.end)), ('distance', sum.distance), ('calories', sum.calories), ('ride_time', sum.ride_time), ('altitude_gain', sum.altitude_gain), ('altitude_loss', sum.altitude_loss), )) if sum.speed is not None: d['speed'] = OrderedDict((('avg', sum.speed.avg), ('max', sum.speed.max))) if sum.heartrate is not None and sum.heartrate.max > 0: d['heartrate'] = OrderedDict((('avg', sum.heartrate.avg), ('max', sum.heartrate.max))) if sum.cadence is not None and sum.cadence.max > 0: d['cadence'] = OrderedDict((('avg', sum.cadence.avg), ('max', sum.cadence.max))) if sum.watts is not None and sum.watts.max > 0: d['watts'] = OrderedDict((('avg', sum.watts.avg), ('max', sum.watts.max))) return d
def track_to_json(track, pretty=False): out = OrderedDict() out['name'] = track.name out['timestamp'] = format_timestamp(track.timestamp) trackpoints = [] for seg in track.trackpoints: segment = [] for tp in seg: segment.append( OrderedDict(( ('timestamp', format_timestamp(tp.timestamp)), ('latitude', tp.latitude), ('longitude', tp.longitude), ('elevation', tp.elevation), ))) trackpoints.append(segment) out['trackpoints'] = trackpoints logpoints = [] for seg in track.logpoints: segment = [] for lp in seg: d = OrderedDict((('timestamp', format_timestamp(lp.timestamp)), )) if lp.speed is not None: d['speed'] = lp.speed if lp.temperature is not None: d['temperature'] = lp.temperature if lp.airpressure is not None: d['airpressure'] = lp.airpressure if lp.cadence is not None: d['cadence'] = lp.cadence if lp.heartrate is not None: d['heartrate'] = lp.heartrate if lp.watts is not None: d['watts'] = lp.watts segment.append(d) logpoints.append(segment) out['logpoints'] = logpoints laps = [] if track.lap_count > 0: for sum in track.lap_summaries: laps.append(_create_summary(sum)) out['laps'] = laps out['summary'] = _create_summary(track.summary) if pretty: return json.dumps(out, indent=1, separators=(',', ': ')) return json.dumps(out)
def create_lap(sum, parent, ns=tcx_ns): lap = xml.SubElement(parent, ns('Lap')) lap.set(ns('StartTime'), format_timestamp(sum.start)) xml.SubElement(lap, ns('TotalTimeSeconds')).text = \ format(sum.end - sum.start, '.1f') xml.SubElement(lap, ns('DistanceMeters')).text = \ format(sum.distance, '.1f') xml.SubElement(lap, ns('MaximumSpeed')).text = \ format(kph_to_ms(sum.speed.max), '.2f') xml.SubElement(lap, ns('Calories')).text = \ format(sum.calories, 'd') if sum.heartrate is not None and sum.heartrate.max > 0: create_sub_value(lap, ns('AverageHeartRateBpm'), format(sum.heartrate.avg, 'd')) create_sub_value(lap, ns('MaximumHeartRateBpm'), format(sum.heartrate.max, 'd')) xml.SubElement(lap, ns('Intensity')).text = 'Active' if sum.cadence is not None and sum.cadence.max > 0: xml.SubElement(lap, ns('Cadence')).text = format(sum.cadence.avg, 'd') xml.SubElement(lap, ns('TriggerMethod')).text = 'Manual' return lap
def track_to_tcx(track, pretty=False): ns = tcx_ns root = xml.Element(ns('TrainingCenterDatabase')) root.set(xsi_ns('schemaLocation'), ' '.join([ _TCX_NS, _TCX_NS_XSD, _ACT_EXT_NS, _ACT_EXT_NS_XSD])) xml.register_namespace('', _TCX_NS) xml.register_namespace('_ns3', _ACT_EXT_NS) activities = xml.SubElement(root, ns('Activities')) activity = xml.SubElement(activities, ns('Activity')) activity.set(ns('Sport'), 'Biking') xml.SubElement(activity, ns('Id')).text = \ format_timestamp(track.timestamp) create_laps(track, activity, ns) if pretty: indent_element_tree(root, ws=' ') out = xml.tostring(root) # ElementTree doesn't let me set prefix ns3 and a lot of software # seems to be hardcoded to use ns3 so have to use this little hack. out = out.replace('_ns3:', 'ns3:').replace('xmlns:_ns3', 'xmlns:ns3') return "<?xml version='1.0' encoding='utf-8'?>\n" + out
def create_trackpoint(tp, lp, parent, ns=tcx_ns): p = xml.SubElement(parent, ns('Trackpoint')) xml.SubElement(p, ns('Time')).text = \ format_timestamp(tp and tp.timestamp or lp.timestamp) if tp: create_position(tp, p, ns) xml.SubElement(p, ns('AltitudeMeters')).text = format(tp.elevation, '.1f') if lp and lp.heartrate is not None: create_sub_value(p, ns('HeartRateBpm'), format(lp.heartrate, 'd')) if lp and lp.cadence is not None: xml.SubElement(p, ns('Cadence')).text = format(lp.cadence, 'd') if lp and lp.speed > 0: create_tpx(lp, p)
def create_trackpoint(tp, lp, parent, ns=tcx_ns): p = xml.SubElement(parent, ns('Trackpoint')) xml.SubElement(p, ns('Time')).text = \ format_timestamp(tp and tp.timestamp or lp.timestamp) if tp: create_position(tp, p, ns) xml.SubElement(p, ns('AltitudeMeters')).text = format( tp.elevation, '.1f') if lp and lp.heartrate is not None: create_sub_value(p, ns('HeartRateBpm'), format(lp.heartrate, 'd')) if lp and lp.cadence is not None: xml.SubElement(p, ns('Cadence')).text = format(lp.cadence, 'd') if lp and lp.speed > 0: create_tpx(lp, p)
def track_to_tcx(track, pretty=False, fake_garmin_device=False, no_laps=False): ns = tcx_ns root = xml.Element(ns('TrainingCenterDatabase')) root.set(xsi_ns('schemaLocation'), ' '.join([_TCX_NS, _TCX_NS_XSD, _ACT_EXT_NS, _ACT_EXT_NS_XSD])) xml.register_namespace('', _TCX_NS) xml.register_namespace('_ns3', _ACT_EXT_NS) activities = xml.SubElement(root, ns('Activities')) activity = xml.SubElement(activities, ns('Activity')) activity.set(ns('Sport'), 'Biking') xml.SubElement(activity, ns('Id')).text = \ format_timestamp(track.timestamp) create_laps(track, no_laps, activity, ns) if fake_garmin_device: create_fake_creator_element(activity, ns) create_author_element(root, ns) if pretty: indent_element_tree(root, ws=' ') out = xml.tostring(root) # ElementTree doesn't let me set prefix ns3 and a lot of software # seems to be hardcoded to use ns3 so have to use this little hack. out = out.replace('_ns3:', 'ns3:').replace('xmlns:_ns3', 'xmlns:ns3') return "<?xml version='1.0' encoding='utf-8'?>\n" + out
def track_to_json(track, pretty=False): out = OrderedDict() out['name'] = track.name out['timestamp'] = format_timestamp(track.timestamp) trackpoints = [] for seg in track.trackpoints: segment = [] for tp in seg: segment.append(OrderedDict(( ('timestamp', format_timestamp(tp.timestamp)), ('latitude', tp.latitude), ('longitude', tp.longitude), ('elevation', tp.elevation), ))) trackpoints.append(segment) out['trackpoints'] = trackpoints logpoints = [] for seg in track.logpoints: segment = [] for lp in seg: d = OrderedDict(( ('timestamp', format_timestamp(lp.timestamp)), )) if lp.speed is not None: d['speed'] = lp.speed if lp.temperature is not None: d['temperature'] = lp.temperature if lp.airpressure is not None: d['airpressure'] = lp.airpressure if lp.cadence is not None: d['cadence'] = lp.cadence if lp.heartrate is not None: d['heartrate'] = lp.heartrate if lp.watts is not None: d['watts'] = lp.watts segment.append(d) logpoints.append(segment) out['logpoints'] = logpoints laps = [] if track.lap_count > 0: for sum in track.lap_summaries: laps.append(_create_summary(sum)) out['laps'] = laps out['summary'] = _create_summary(track.summary) if pretty: return json.dumps(out, indent=1, separators=(',', ': ')) return json.dumps(out)