Parsing the GPX

Now, we'll parse the GPX file, which is just XML, using the built-in xml.dom.minidom module. We'll extract the latitude, longitude, elevation, and timestamps. We'll store them in a list for later use. The timestamps are converted to struct_time objects using Python's time module, which makes them easier to work with:

# Parse the gpx file and extract the coordinates
log.info("Parsing GPX file: {}".format(gpx))
xml = minidom.parse(gpx)
# Grab all of the "trkpt" elements
trkpts = xml.getElementsByTagName("trkpt")
# Latitude list
lats = []
# Longitude list
lons = []
# Elevation list
elvs = []
# GPX timestamp list
times = []
# Parse lat/long, elevation and times
for trkpt in trkpts:
    # Latitude
    lat = float(trkpt.attributes["lat"].value)
    # Longitude
    lon = float(trkpt.attributes["lon"].value)
    lats.append(lat)
    lons.append(lon)
    # Elevation
    elv = trkpt.childNodes[0].firstChild.nodeValue
    elv = float(elv)
    elvs.append(elv)
    # Times
    t = trkpt.childNodes[1].firstChild.nodeValue
    # Convert to local time epoch seconds
    t = get_local_time(t)
    times.append(t)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset