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 | |
| 28 | PoiLayer::PoiLayer(MapWidget *map) : |
| 29 | AbstractLayer(map), |
| 30 | m_points(), |
| 31 | m_pointsOnScreen(), |
| 32 | m_icons(), |
| 33 | m_pointsOffset(0, 0) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | void 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 | |
| 99 | void 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 | |
| 113 | void PoiLayer::pan(const QPoint &move) |
| 114 | { |
| 115 | m_pointsOffset += move; |
| 116 | } |
| 117 | |
| 118 | void 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 | |
| 27 | class PoiLayer : public AbstractLayer |
| 28 | { |
| 29 | Q_OBJECT |
| 30 | public: |
| 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 | |
| 37 | protected: |
| 38 | virtual void paint(QPainter *painter); |
| 39 | |
| 40 | private: |
| 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 |