コード例 #1
0
ファイル: gpx.py プロジェクト: Noneus/Track-Visualizer
class GpxParser(object):
    """
    Parse Gpx-Files
    """
    def __init__(self):
        """ create a new instance """
        self._track = None

    def parse(self, filename):
        """
        Parse a file and set track property
        filename - the filename
        """
        dom = parse(filename)
        self._track = Track()

        for node in dom.getElementsByTagName('trkpt'):
            self._track.append(
                GpsPoint(node.attributes['lat'].nodeValue,
                         node.attributes['lon'].nodeValue))
            for cnode in node.childNodes:
                if cnode.localName == 'ele':
                    self._track[-1].elevation = self._get_text(cnode.childNodes)
                elif cnode.localName == 'time':
                    self._track[-1].time = datetime.strptime(
                        self._get_text(cnode.childNodes), TIME_PATTERN)

    @staticmethod
    def _get_text(nodelist):
        """ get all text from the nodes in nodelist """
        text = ''
        for node in nodelist:
            if node.nodeType == node.TEXT_NODE:
                text += node.data
        return text

    @property
    def track(self):
        """ get the parsed gps track """
        return self._track

    @property
    def extension(self):
        """ get the extensions (e.g. .gpx) """
        return '.gpx'