Date:2010-10-30 21:35:33 (13 years 4 months ago)
Author:nielsk
Commit:2d6e62a111b8ac9d7ef9d55d92837aa8778efd39
Message:add layer that displays points of interest from a .osm file TODO: draw different icons

Files: main.cpp (1 diff)
mainwidget.cpp (2 diffs)
mainwidget.h (1 diff)
poilayer.cpp (1 diff)
poilayer.h (1 diff)

Change Details

main.cpp
2828    MainWidget w;
2929
3030    if (QApplication::arguments().count() > 1) {
31        w.loadGpx(QApplication::arguments().at(1));
31        w.loadFile(QApplication::arguments().at(1));
3232    }
3333
3434    QObject::connect(&w, SIGNAL(close()), &a, SLOT(quit()));
mainwidget.cpp
2828#include "gpslayer.h"
2929#include "gpxlayer.h"
3030#include "markerlayer.h"
31#include "poilayer.h"
3132#include "timelayer.h"
3233
3334#include <QtCore/QDir>
...... 
8889{
8990}
9091
91void MainWidget::loadGpx(const QString &fileName)
92void MainWidget::loadFile(const QString &fileName)
9293{
9394    if (fileName.endsWith(".gpx")) {
9495        AbstractLayer *l = new GpxLayer(m_map);
9596        l->load(fileName);
9697        m_map->addLayer(l, 2, "GPS-Track");
98    } else if (fileName.endsWith(".osm")) {
99        AbstractLayer *l = new PoiLayer(m_map);
100        l->load(fileName);
101        m_map->addLayer(l, 3, "Points Of Interest");
97102    }
98103}
99104
mainwidget.h
3636    MainWidget(QWidget *parent = 0);
3737    ~MainWidget();
3838
39    void loadGpx(const QString &fileName);
39    void loadFile(const QString &fileName);
4040
4141signals:
4242    void close();
poilayer.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 "poilayer.h"
21
22#include "mapwidget.h"
23#include "projection.h"
24
25#include <QtCore/QFile>
26#include <QtXml/QXmlStreamReader>
27
28PoiLayer::PoiLayer(MapWidget *map) :
29    AbstractLayer(map),
30    m_points(),
31    m_pointsOnScreen(),
32    m_icons(),
33    m_pointsOffset(0, 0)
34{
35}
36
37void PoiLayer::load(const QString &filename)
38{
39    QFile file(filename);
40    if (file.open(QIODevice::ReadOnly)) {
41        QXmlStreamReader xml(&file);
42
43        QStringList categories;
44        categories << "highway";
45        categories << "traffic_calming";
46        categories << "barrier";
47        categories << "waterway";
48        categories << "railway";
49        categories << "aeroway";
50        categories << "aerialway";
51        categories << "power";
52        categories << "man_made";
53        categories << "leisure";
54        categories << "amenity";
55        categories << "office";
56        categories << "shop";
57        categories << "craft";
58        categories << "emergency";
59        categories << "tourism";
60        categories << "historic";
61        categories << "landuse";
62        categories << "military";
63        categories << "natural";
64        categories << "geological";
65        categories << "sport";
66
67        QHash<QString, QString> tags;
68        QPointF pos;
69        while (!xml.atEnd()) {
70            xml.readNext();
71            if (xml.isStartElement()) {
72                if (xml.name() == "node") {
73                    float lat = xml.attributes().value("lat").toString().toFloat();
74                    float lon = xml.attributes().value("lon").toString().toFloat();
75
76                    pos = QPointF(Projection::lon2rawx(lon), Projection::lat2rawy(lat));
77                } else if (xml.name() == "tag") {
78                    tags.insert(xml.attributes().value("k").toString(),
79                                xml.attributes().value("v").toString());
80                }
81            } else if (xml.isEndElement()) {
82                if (xml.name() == "node") {
83                    foreach (const QString &c, categories) {
84                        QString t = tags.value(c, "");
85                        if (!t.isEmpty()) {
86                            m_points << pos;
87                            m_icons << t;
88                            break;
89                        }
90                    }
91                    tags.clear();
92                }
93            }
94        }
95        zoom(0);
96    }
97}
98
99void PoiLayer::zoom(int level)
100{
101    if (m_points.count() > 1) {
102        int scale = 1 << level;
103        m_pointsOnScreen.clear();
104        m_pointsOffset = map()->raw2screen(m_points.first().x(), m_points.first().y(), scale);
105        m_pointsOnScreen << QPoint(0, 0);
106        for (int i = 1; i < m_points.count(); ++i) {
107            QPointF p = m_points.at(i);
108            m_pointsOnScreen << map()->raw2screen(p.x(), p.y(), scale) - m_pointsOffset;
109        }
110    }
111}
112
113void PoiLayer::pan(const QPoint &move)
114{
115    m_pointsOffset += move;
116}
117
118void PoiLayer::paint(QPainter *painter)
119{
120    QPoint p;
121    for (int i = 0; i < m_pointsOnScreen.count(); ++i) {
122        p = m_pointsOnScreen.at(i);
123        painter->drawEllipse(p + m_pointsOffset, 5, 5);
124    }
125}
126
poilayer.h
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#ifndef POI_LAYER_H
21#define POI_LAYER_H
22
23#include "abstractlayer.h"
24
25#include <QtGui/QPainter>
26
27class PoiLayer : public AbstractLayer
28{
29    Q_OBJECT
30public:
31    PoiLayer(MapWidget *map);
32
33    virtual void load(const QString &filename);
34    virtual void zoom(int level);
35    virtual void pan(const QPoint &move);
36
37protected:
38    virtual void paint(QPainter *painter);
39
40private:
41    QList<QPointF> m_points;
42    QList<QPoint> m_pointsOnScreen;
43    QStringList m_icons;
44    QPoint m_pointsOffset;
45
46};
47
48#endif // POI_LAYER_H

Archive Download the corresponding diff file

Branches:
master



interactive