KOSMIndoorMap

routeoverlay.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "routeoverlay.h"
7
8using namespace KOSMIndoorRouting;
9
10RouteOverlay::RouteOverlay(QObject *parent)
11 : KOSMIndoorMap::AbstractOverlaySource(parent)
12{
13}
14
15RouteOverlay::~RouteOverlay() = default;
16
17void RouteOverlay::setMapData(const KOSMIndoorMap::MapData &mapData)
18{
19 m_data = mapData;
20 if (mapData.isEmpty()) {
21 return;
22 }
23
24 m_mxRouteKey = m_data.dataSet().makeTagKey("mx:routing");
25 Q_EMIT reset();
26}
27
28void RouteOverlay::setStart(OSM::Coordinate c, int floorLevel)
29{
30 if (m_startNode) {
31 m_gc.push_back(std::move(m_startNode));
32 }
33 if (c.isValid()) {
34 m_startNode = OSM::UniqueElement(new OSM::Node);
35 m_startNode.setId(m_data.dataSet().nextInternalId());
36 m_startNode.node()->coordinate = c;
37 m_startNode.setTagValue(m_mxRouteKey, "start");
38 m_startLevel = floorLevel;
39 }
40 Q_EMIT update();
41}
42
43void RouteOverlay::setEnd(OSM::Coordinate c, int floorLevel)
44{
45 if (m_endNode) {
46 m_gc.push_back(std::move(m_endNode));
47 }
48 if (c.isValid()) {
49 m_endNode = OSM::UniqueElement(new OSM::Node);
50 m_endNode.setId(m_data.dataSet().nextInternalId());
51 m_endNode.node()->coordinate = c;
52 m_endNode.setTagValue(m_mxRouteKey, "end");
53 m_endLevel = floorLevel;
54 }
55 Q_EMIT update();
56}
57
58void RouteOverlay::setRoute(const Route &route)
59{
60 for (auto &way : m_routeWays) {
61 m_gc.push_back(std::move(way));
62 }
63 m_routeWays.clear();
64 m_routeWayFloorLevels.clear();
65 m_transientNodesGC.push_back(std::move(m_transientNodes));
66
67 m_route = route;
68 if (m_route.steps().size() < 2) {
69 Q_EMIT update();
70 return;
71 }
72
73 m_transientNodes.reserve(m_route.steps().size());
75
76 int prevLevel = m_route.steps()[0].floorLevel;
77
79 for (auto it = m_route.steps().begin(); it != m_route.steps().end();) {
80 // TODO compute per segment rather than globally
81 bbox = OSM::unite(bbox, {(*it).coordinate, (*it).coordinate});
82
83 if (!way) {
85 way.setId(m_data.dataSet().nextInternalId());
86 way.setTagValue(m_mxRouteKey, "route");
87 }
88
89 OSM::Node node;
90 node.id = m_data.dataSet().nextInternalId();
91 node.coordinate = (*it).coordinate;
92 way.way()->nodes.push_back(node.id);
93 m_transientNodes.push_back(std::move(node));
94
95 if (way.way()->nodes.size() >= 2 && (*it).floorLevel != prevLevel) {
96 m_routeWays.push_back(std::move(way));
97 m_routeWayFloorLevels.push_back(prevLevel);
98 prevLevel = (*it).floorLevel;
99 --it;
100 } else {
101 ++it;
102 }
103 }
104
105 if (way) {
106 m_routeWays.push_back(std::move(way));
107 m_routeWayFloorLevels.push_back(prevLevel);
108 }
109
110 std::sort(m_transientNodes.begin(), m_transientNodes.end());
111 for (auto &way :m_routeWays) {
112 way.way()->bbox = bbox;
113 }
114
115 Q_EMIT update();
116}
117
118void RouteOverlay::forEach(int floorLevel, const std::function<void(OSM::Element, int)> &func) const
119{
120 for (std::size_t i = 0; i < m_routeWays.size(); ++i) {
121 if (floorLevel == m_routeWayFloorLevels[i]) {
122 func(m_routeWays[i], floorLevel);
123 }
124 }
125
126 if (m_startNode && m_startLevel == floorLevel) {
127 func(m_startNode, floorLevel);
128 }
129 if (m_endNode && m_endLevel == floorLevel) {
130 func(m_endNode, floorLevel);
131 }
132}
133
134void RouteOverlay::endSwap()
135{
136 m_gc.clear();
137 m_transientNodesGC.clear();
138}
139
140const std::vector<OSM::Node>* RouteOverlay::transientNodes() const
141{
142 return &m_transientNodes;
143}
144
145#include "moc_routeoverlay.cpp"
void reset()
Trigger style re-compilation.
void update()
Trigger map re-rendering when the source changes.
Raw OSM map data, separated by levels.
Definition mapdata.h:60
Bounding box, ie.
Definition datatypes.h:95
Coordinate, stored as 1e7 * degree to avoid floating point precision issues, and offset to unsigned v...
Definition datatypes.h:37
Id nextInternalId() const
Create a unique id for internal use (ie.
TagKey makeTagKey(const char *keyName, StringMemory keyMemOpt=StringMemory::Transient)
Create a tag key for the given tag name.
Definition datatypes.cpp:28
A reference to any of OSM::Node/OSM::Way/OSM::Relation.
Definition element.h:24
An OSM node.
Definition datatypes.h:204
A std::unique_ptr-like object for OSM element types.
Definition element.h:100
An OSM way.
Definition datatypes.h:232
OSM-based multi-floor indoor maps for buildings.
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.