Package upoints :: Module gpx :: Class Waypoints
[hide private]
[frames] | no frames]

Class Waypoints

object --+        
         |        
      list --+    
             |    
  point.Points --+
                 |
                Waypoints

Class for representing a group of Waypoint objects

Since: 0.8.0

Instance Methods [hide private]
new list
__init__(self, gpx_file=None)
Initialise a new Waypoints object
list
import_locations(self, gpx_file, gpx_version=None)
Import GPX data files
ET.ElementTree
export_gpx_file(self, gpx_version='1.1', human_namespace=False)
Generate GPX element tree from Waypoints object

Inherited from point.Points: __repr__, bearing, destination, distance, final_bearing, forward, inverse, midpoint, range, sun_events, sunrise, sunset, to_grid_locator

Inherited from list: __add__, __contains__, __delitem__, __delslice__, __eq__, __ge__, __getattribute__, __getitem__, __getslice__, __gt__, __hash__, __iadd__, __imul__, __iter__, __le__, __len__, __lt__, __mul__, __ne__, __new__, __reversed__, __rmul__, __setitem__, __setslice__, append, count, extend, index, insert, pop, remove, reverse, sort

Inherited from object: __delattr__, __reduce__, __reduce_ex__, __setattr__, __str__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, gpx_file=None)
(Constructor)

 
Initialise a new Waypoints object
Parameters:
  • points - Point objects to wrap
  • parse - Whether to attempt import of points
  • units - Unit type to be used for distances when parsing string locations
Returns: new list
Overrides: object.__init__

import_locations(self, gpx_file, gpx_version=None)

 

Import GPX data files

import_locations() returns a list with Waypoint objects.

It expects data files in GPX format, as specified in GPX 1.1 Schema Documentation, which is XML such as:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<gpx version="1.1" creator="PocketGPSWorld.com"
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">

  <wpt lat="52.015" lon="-0.221">
    <name>Home</name>
    <desc>My place</desc>
  </wpt>
  <wpt lat="52.167" lon="0.390">
    <name>MSR</name>
    <desc>Microsoft Research, Cambridge</desc>
  </wpt>
</gpx>

The reader uses Python's ElementTree module, so should be very fast when importing data. The above file processed by import_locations() will return the following list object:

[Waypoint(52.015, -0.221, "Home", "My place"),
 Waypoint(52.167, 0.390, "MSR", "Microsoft Research, Cambridge")]
>>> waypoints = Waypoints(open("gpx"))
>>> for value in sorted(waypoints,
...                     key=lambda x: x.name.lower()):
...     print(value)
Home (52°00'54"N, 000°13'15"W) [My place]
MSR (52°10'01"N, 000°23'24"E) [Microsoft Research, Cambridge]
Parameters:
  • gpx_file (file, list or str) - GPX data to read
  • gpx_version (str) - Specific GPX version entities to import
Returns: list
Locations with optional comments
Overrides: point.Points.import_locations

export_gpx_file(self, gpx_version='1.1', human_namespace=False)

 

Generate GPX element tree from Waypoints object

>>> from sys import stdout
>>> locations = Waypoints(open("gpx"))
>>> xml = locations.export_gpx_file()
>>> xml.write(stdout)
<ns0:gpx xmlns:ns0="http://www.topografix.com/GPX/1/1"><ns0:wpt lat="52.015" lon="-0.221"><ns0:name>Home</ns0:name><ns0:desc>My place</ns0:desc></ns0:wpt><ns0:wpt lat="52.167" lon="0.39"><ns0:name>MSR</ns0:name><ns0:desc>Microsoft Research, Cambridge</ns0:desc></ns0:wpt></ns0:gpx>
Parameters:
  • gpx_version (str) - GPX version to generate
  • human_namespace (bool) - Whether to generate output using human readable namespace prefixes
Returns: ET.ElementTree
GPX element tree depicting Waypoints object