Root/gpxlayer.cpp

1/*
2 * Copyright 2010 Niels Kummerfeldt <niels.kummerfeldt@tu-harburg.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
18 */
19
20#include "gpxlayer.h"
21
22#include "mapwidget.h"
23#include "projection.h"
24
25#include <QtCore/QDateTime>
26#include <QtCore/QFile>
27#include <QtXml/QXmlStreamReader>
28
29GpxLayer::GpxLayer(MapWidget *map) :
30    AbstractLayer(map)
31{
32}
33
34void GpxLayer::load(const QString &filename)
35{
36    QFile file(filename);
37    if (file.open(QIODevice::ReadOnly)) {
38        QXmlStreamReader xml(&file);
39
40        QPolygonF points;
41        QList<float> elev;
42        QList<int> time;
43
44        QString tag, tag2;
45        QString name;
46        QPointF pos;
47        while (!xml.atEnd()) {
48            xml.readNext();
49            if (xml.isStartElement()) {
50                if (xml.name() == "trkpt") {
51                    tag = "trkpt";
52                    float lat = xml.attributes().value("lat").toString().toFloat();
53                    float lon = xml.attributes().value("lon").toString().toFloat();
54
55                    points << QPointF(Projection::lon2rawx(lon), Projection::lat2rawy(lat));
56                } else if (xml.name() == "ele") {
57                    tag2 = "ele";
58                } else if (xml.name() == "time") {
59                    tag2 = "time";
60                } else if (xml.name() == "wpt") {
61                    tag = "wpt";
62                    float lat = xml.attributes().value("lat").toString().toFloat();
63                    float lon = xml.attributes().value("lon").toString().toFloat();
64
65                    pos = QPointF(lon, lat);
66                } else if (xml.name() == "name") {
67                    tag2 = "name";
68                } else if (xml.name() == "trk" ||
69                           xml.name() == "trkseg") {
70                } else {
71                    tag2.clear();
72                }
73            } else if (xml.isEndElement()) {
74                if (xml.name() == "trkseg") {
75                    if (!points.isEmpty()) {
76                        m_track << points;
77                        m_track << QPointF();
78                    }
79                    points.clear();
80                    elev.clear();
81                    time.clear();
82                } else if (xml.name() == "wpt") {
83                    //addMarker(pos, name);
84                    name.clear();
85                }
86            } else if (xml.isCharacters() && !xml.isWhitespace()) {
87                if (tag == "trkpt") {
88                    if (tag2 == "ele") {
89                        elev << xml.text().toString().toFloat();
90                    } else if (tag2 == "time") {
91                        QDateTime dt = QDateTime::fromString(xml.text().toString(), Qt::ISODate);
92                        time << dt.toTime_t();
93                    }
94                } else if (tag == "wpt") {
95                    if (tag2 == "name") {
96                        name = xml.text().toString();
97                    }
98                }
99            }
100        }
101        zoom(0);
102    }
103}
104
105void GpxLayer::zoom(int level)
106{
107    if (m_track.count() > 1) {
108        int scale = 1 << level;
109        m_trackOnScreen.clear();
110        m_trackOffset = map()->raw2screen(m_track.first().x(), m_track.first().y(), scale);
111        m_trackOnScreen << QPoint(0, 0);
112        for (int i = 1; i < m_track.count(); ++i) {
113            QPointF p = m_track.at(i);
114            if (!p.isNull()) {
115                m_trackOnScreen << map()->raw2screen(p.x(), p.y(), scale) - m_trackOffset;
116            } else {
117                m_trackOnScreen << QPoint();
118            }
119        }
120    }
121}
122
123void GpxLayer::pan(const QPoint &move)
124{
125    m_trackOffset += move;
126}
127
128void GpxLayer::paint(QPainter *painter)
129{
130    if (m_trackOnScreen.count() > 1) {
131        QPoint p1, p2 = m_trackOnScreen.first();
132        for (int i = 1; i < m_trackOnScreen.count(); ++i) {
133            p1 = m_trackOnScreen.at(i);
134            if (!p1.isNull()) {
135                painter->drawLine(p1 + m_trackOffset, p2 + m_trackOffset);
136                p2 = p1;
137            }
138        }
139    }
140}
141
142

Archive Download this file

Branches:
master



interactive