Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created July 11, 2017 12:58
Show Gist options
  • Select an option

  • Save TimSC/82a533156eac13f1a6c36db47913fda2 to your computer and use it in GitHub Desktop.

Select an option

Save TimSC/82a533156eac13f1a6c36db47913fda2 to your computer and use it in GitHub Desktop.
Analyse pitch in gpx file
import math
import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt
from pyo5m import OsmData
if __name__=="__main__":
tree = ET.parse("/home/tim/Desktop/CampingMapping/20170709.gpx")
root = tree.getroot()
prevPos = None
prevEle = None
cumDist = 0.0
distData, gradData = [], []
latLonData, eleData = [], []
nid = -1
osmData = OsmData.OsmData()
for k in root:
if k.tag != "{http://www.topografix.com/GPX/1/1}trk": continue
for ks in k:
if ks.tag != "{http://www.topografix.com/GPX/1/1}trkseg": continue
for trkpt in ks:
if trkpt.tag != "{http://www.topografix.com/GPX/1/1}trkpt": continue
lat = float(trkpt.attrib["lat"])
lon = float(trkpt.attrib["lon"])
ele = None
for prop in trkpt:
if prop.tag == "{http://www.topografix.com/GPX/1/1}ele":
ele = float(prop.text)
R = 6371000.0
latr = math.radians(lat)
lonr = math.radians(lon)
x = R * math.cos(latr) * math.cos(lonr)
y = R * math.cos(latr) * math.sin(lonr)
z = R * math.sin(latr)
if prevPos is not None:
dist2d = ((x-prevPos[0])**2.0+(y-prevPos[1])**2.0+(z-prevPos[2])**2.0)**0.5
distData.append(cumDist)
cumDist += dist2d
eleData.append(ele)
latLonData.append((lat, lon))
prevPos = (x, y, z)
distData2 = []
for i, (cumDist, ele, (lat, lon)) in enumerate(zip(distData, eleData, latLonData)):
if i+10 >= len(distData): continue
cumDist2, ele2 = distData[i+10], eleData[i+10]
dist2d = cumDist2 - cumDist
dele = ele2 - ele
#print cumDist, dist2d, dele
gradient = math.atan2(dele, dist2d) * 100.0
if dist2d > 1.0:
#print cumDist, gradient
gradData.append(gradient)
distData2.append(0.5*(cumDist+cumDist2))
#print 0.5*(cumDist+cumDist2), gradient
metaData = (1, None, None, None, None, True)
osmData.nodes.append([nid, metaData, {"pitch": str(gradient)}, [lat, lon]])
nid -= 1
prevPos = (x, y, z)
prevEle = ele
fi = open("export.osm", "wb")
osmData.SaveToOsmXml(fi)
fi.close()
plt.plot(distData2, gradData)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment