4

I am looking for Python library, which can work with GPS coordinates. Let's say, I have a list of coordinates:

>>> gps = [
...  (53.25012925, −6.24479338, 349.9, '2011-08-20T09:35:00Z'),
...  (53.25028285, -6.24441800, 359.9, '2011-08-20T09:35:30Z'),
...  (53.25049500, -6.24266032, 395.9, '2011-08-20T09:36:00Z'),
...  # and so on...
... ]
>>>

I would like to compute average speed, distance, get the higest point, and other info. I know, it is quite simple to compute it, but I am wondering if there is any existing code (I do not like to reinvent the wheel).

Note: There is similar question here in stackoverflow (Which gps library would you recommend for python?), but it is about GPSD. I am not working with any device, I just have GPS coordinates in text file.

Community
  • 1
  • 1
vasco
  • 1,502
  • 1
  • 16
  • 19
  • An excellent question. Clarify: for distances, do you mean ideal surface distance (treating earth as an ideal ellipsoid), actual surface distance based on the geoid, or actual surface distance based on measured topography? The last would require a topographic database. – wberry Sep 08 '11 at 17:31
  • By distances I mean ideal surface distance. Simply compute distance between two points. – vasco Sep 12 '11 at 09:12
  • 1
    @vasco This question is completely unrelated to GPS since you're already operating on coordinates. Therefore, it would work fine with positions obtained by Beidou, Galileo, GLONASS, or human input. – phihag Sep 14 '11 at 10:45
  • @phihag Yes, you are right. In Python I'm working with coordinates. But source of these coordinates is my GPS logger, so that is why I'm talking about GPS. – vasco Sep 14 '11 at 10:52

2 Answers2

4

I have found an interesting library named geopy. It can calculate distances between two GPS points (it uses great-circle distance and Vincenty distance methods). On top of that, geopy can do geocoding (it can get GPS coordinates from an address).

The other features (average speed, higest point, etc.) I can hack by myself.

vasco
  • 1,502
  • 1
  • 16
  • 19
3

You may still be able to use data portions of GPSD, rather than writing something from scratch. The following code is from the GPSD source and has a module for making paths from streams of GPS data (and then getting the path length and whatnot)

http://code.google.com/p/python-gpsd/source/browse/src/nmea/track.py

class Track(object):

    def __init__(self, recordDelay=10, maxSize=3600, ignoreDuplicates=True, duplicateRange=0.0001):
        """ Constructor

        The default values allow for 10 hours worth of data recorded
        at 10 second intervals.

        recordDelay - Delay between recording data points
        maxSize - Maximum number of points to record
        ignoreDuplicates - Ignore entries that are very similar (ie moved a minimal distance)
        duplicateRange - Varience range within a duplicate is detected (adjust to account for
            subtle gps position drift)
        """
        self.recordDelay = recordDelay
        self.maxSize = maxSize
        self.ignoreDuplicates = ignoreDuplicates
        self.duplicateRange = duplicateRange
        self.positions = []
        self.latest = None

    def append(self, position, heading, timeStamp=None):
        """ Append position and heading information """
        if timeStamp is None: timeStamp = datetime.utcnow()

        self.latest = (timeStamp, position, heading)
        if len(self.positions):
            last = self.positions[0]
        else:
            last = None

        # Make sure we re in range
        if last is None or (timeStamp - last[0]).seconds >= self.recordDelay:
            self.positions.insert(0, self.latest)
            self.latest = None

        # Clear extra data
        if len(self.positions) > self.maxSize: pass

    def clear(self):
        """ Clear all items from track """
        self.positions = []
        self.latest = None

    def __len__(self):
        """ Return the length of the track """
        if self.latest is None: return len(self.positions)
        return len(self.positions) + 1

    def __getslice__(self, i, j):
        return self.positions[i:j]

    def __getitem__(self, i):
        return self.positions[i]

    def get_latest(self):
        if self.latest is None and len(self.positions) > 0:
            return self.positions

    def get_by_time(self, timeRange, now=datetime.utcnow()):
        """ Returns the last n items within the time range """
        result = []
        if self.latest is not None: result.append(self.latest)
        for position in self.positions:
            if (now - position[0]).seconds > timeRange: break
            result.append(position)
        return result
Matt Sweeney
  • 2,060
  • 1
  • 14
  • 19