Ejemplo n.º 1
0
    def store_route(self, route, name="", cat='misc'):
        """store a route, found by Google Directions to a GPX file,
           then load this file to tracklogs list,
           return resulting path
           or None when storing fails"""
        # import the GPX module only when really needed
        from upoints import gpx

        newTracklog = gpx.Trackpoints()
        trackpoints = map(lambda x: gpx.Trackpoint(x[0], x[1]), route)
        newTracklog.append(trackpoints)

        timeString = strftime("%Y%m%d#%H-%M-%S", gmtime())
        # gdr = Google Directions Result, TODO: alternate prefixes when we have more routing providers

        name = name.encode('ascii', 'ignore')
        filename = "gdr_%s%s.gpx" % (name, timeString)
        # TODO: store to more formats ?
        return self.store_tracklog(newTracklog, filename, cat, "GPX")
Ejemplo n.º 2
0
    def saveToGPX(self, path, turns=False):
        """Save way to GPX file
        points are saved as trackpoints,
        message points as routepoints with turn description in the <desc> field
        """
        try: # first check if we cant open the file for writing
            f = open(path, "wb")
            # Handle trackpoints
            trackpoints = gpx.Trackpoints()
            # check for stored timestamps
            if self._points and len(self._points[0]) >= 4: # LLET
                trackpoints.append(
                    [gpx.Trackpoint(x[0], x[1], None, None, x[2], x[3]) for x in self._points]
                )

            else: # LLE
                trackpoints.append(
                    [gpx.Trackpoint(x[0], x[1], None, None, x[2], None) for x in self._points]
                )

            # Handle message points

            # TODO: find how to make a GPX trac with segments of different types
            # is it as easy just dumping the segment lists to Trackpoints ?

            # message is stored in <desc>
            messagePoints = self.message_points
            index = 1
            mpCount = len(messagePoints)
            if turns: # message points contain Turn-By-Turn directions
                routepoints = gpx.Routepoints()
                for mp in messagePoints:
                    name = ""
                    if turns:
                        name = "Turn %d/%d" % (index, mpCount)
                    lat, lon, elev, message = mp.get_llmi()
                    routepoints.append(gpx.Routepoint(lat, lon, name, message, elev, None))
                    index += 1
                log.info('%d points, %d routepoints saved to %s in GPX format',
                         path, len(trackpoints), len(routepoints))
            else:
                waypoints = []
                for mp in messagePoints:
                    name = ""
                    if turns:
                        name = "Turn %d/%d" % (index, mpCount)
                    lat, lon, elev, message = mp.get_llmi()
                    waypoints.append(gpx.Routepoint(lat, lon, name, message, elev, None))
                    index += 1
                log.info('%d points, %d waypoints saved to %s in GPX format',
                         len(trackpoints[0]), len(waypoints), path)

            # write the GPX tree to file
            # TODO: waypoints & routepoints support
            xmlTree = trackpoints.export_gpx_file()
            xmlTree.write(f)
            # close the file
            f.close()
            return True
        except Exception:
            log.exception('saving to GPX format failed')
            return False