KPublicTransport

path.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "path.h"
8#include "datatypes_p.h"
9#include "json_p.h"
10#include "../geo/geojson_p.h"
11#include "location.h"
12
13#include <QLineF>
14
15using namespace KPublicTransport;
16
17namespace KPublicTransport {
18class PathSectionPrivate : public QSharedData {
19public:
20 QPolygonF path;
21 QString description;
22 int floorLevelChange = 0;
24};
25}
26
27KPUBLICTRANSPORT_MAKE_GADGET(PathSection)
28KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, QPolygonF, path, setPath)
29KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, QString, description, setDescription)
30KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, int, floorLevelChange, setFloorLevelChange)
31KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, PathSection::Maneuver, maneuver, setManeuver)
32
33int PathSection::distance() const
34{
35 if (d->path.size() < 2) {
36 return 0;
37 }
38
39 float dist = 0;
40 for (auto it = d->path.begin(); it != std::prev(d->path.end()); ++it) {
41 const auto nextIt = std::next(it);
42 dist += Location::distance((*it).y(), (*it).x(), (*nextIt).y(), (*nextIt).x());
43 }
44 return dist;
45}
46
48{
49 const auto p1 = startPoint();
50 const auto p2 = endPoint();
51 if (d->path.size() < 2 || p1 == p2) {
52 return -1;
53 }
54 return static_cast<int>(450 - QLineF(p1.x(), -p1.y(), p2.x(), -p2.y()).angle()) % 360;
55}
56
58{
59 return d->path.empty() ? QPointF() : d->path.constFirst();
60}
61
63{
64 return d->path.empty() ? QPointF() : d->path.constLast();
65}
66
68{
69 auto obj = Json::toJson(section);
70 if (!section.path().empty()) {
71 obj.insert(QLatin1String("path"), GeoJson::writeLineString(section.path()));
72 }
73 if (section.maneuver() == PathSection::Move) {
74 obj.remove(QLatin1String("maneuver"));
75 }
76 if (section.floorLevelChange() == 0) {
77 obj.remove(QLatin1String("floorLevelChange"));
78 }
79 return obj;
80}
81
82QJsonArray PathSection::toJson(const std::vector<PathSection> &sections)
83{
84 return Json::toJson(sections);
85}
86
88{
89 auto section = Json::fromJson<PathSection>(obj);
90 section.setPath(GeoJson::readLineString(obj.value(QLatin1String("path")).toObject()));
91 return section;
92}
93
94std::vector<PathSection> PathSection::fromJson(const QJsonArray &array)
95{
96 return Json::fromJson<PathSection>(array);
97}
98
99
100namespace KPublicTransport {
101class PathPrivate : public QSharedData {
102public:
103 std::vector<PathSection> sections;
104};
105}
106
107KPUBLICTRANSPORT_MAKE_GADGET(Path)
108
109bool Path::isEmpty() const
110{
111 return d->sections.empty();
112}
113
114const std::vector<PathSection>& Path::sections() const
115{
116 return d->sections;
117}
118
119std::vector<PathSection>&& Path::takeSections()
120{
121 d.detach();
122 return std::move(d->sections);
123}
124
125void Path::setSections(std::vector<PathSection> &&sections)
126{
127 d.detach();
128 d->sections = std::move(sections);
129}
130
131int Path::distance() const
132{
133 return std::accumulate(d->sections.begin(), d->sections.end(), 0, [](int d, const auto &sec) { return d + sec.distance(); });
134}
135
137{
138 return isEmpty() ? QPointF() : d->sections.front().startPoint();
139}
140
142{
143 return isEmpty() ? QPointF() : d->sections.front().endPoint();
144}
145
147{
148 auto obj = Json::toJson(path);
149 obj.insert(QLatin1String("sections"), PathSection::toJson(path.sections()));
150 return obj;
151}
152
154{
155 auto path = Json::fromJson<Path>(obj);
156 path.setSections(PathSection::fromJson(obj.value(QLatin1String("sections")).toArray()));
157 return path;
158}
159
160int Path::sectionCount() const
161{
162 return d->sections.size();
163}
164
165#include "moc_path.cpp"
static float distance(float lat1, float lon1, float lat2, float lon2)
Compute the distance between two geo coordinates, in meters.
Definition location.cpp:425
A section of a Path.
Definition path.h:25
Maneuver
Maneuver associated with a path section.
Definition path.h:46
@ Move
Move/drive with the default mode of transport for this path.
Definition path.h:47
QPointF startPoint() const
First point on the path of this section.
Definition path.cpp:57
QPolygonF path
The geo coordinate poly-line followed by this path section.
Definition path.h:28
static QJsonObject toJson(const PathSection &section)
Serializes one path section section to JSON.
Definition path.cpp:67
QPointF endPoint() const
Last point on the path of this section.
Definition path.cpp:62
int floorLevelChange
Floor level change during this path section.
Definition path.h:42
static PathSection fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
Definition path.cpp:87
int direction
The overall direction of this section in degree.
Definition path.h:37
Maneuver maneuver
Movement maneuver for this path section.
Definition path.h:54
A path followed by any kind of location change.
Definition path.h:89
std::vector< PathSection > && takeSections()
Moves the path sections out of this object.
Definition path.cpp:119
int sectionCount
Number of path sections for QML.
Definition path.h:95
int distance
The length of this path in meters.
Definition path.h:98
bool isEmpty() const
Returns true if this is an empty/not-set path.
Definition path.cpp:109
QPointF startPoint() const
First point on this path.
Definition path.cpp:136
static Path fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
Definition path.cpp:153
void setSections(std::vector< PathSection > &&sections)
Sets the path sections.
Definition path.cpp:125
QPointF endPoint() const
Last point on this path.
Definition path.cpp:141
static QJsonObject toJson(const Path &path)
Serializes one path object to JSON.
Definition path.cpp:146
std::vector< KPublicTransport::PathSection > sections
Access to path sections for QML.
Definition path.h:93
Query operations and data types for accessing realtime public transport information from online servi...
QJsonValue value(QLatin1StringView key) const const
qreal angle() const const
bool empty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.