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) ...

Get Learning Geospatial Analysis with Python - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.