KPublicTransport

path.cpp
1 /*
2  SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
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 
15 using namespace KPublicTransport;
16 
17 namespace KPublicTransport {
18 class PathSectionPrivate : public QSharedData {
19 public:
20  QPolygonF path;
21  QString description;
22  int floorLevelChange = 0;
24 };
25 }
26 
27 KPUBLICTRANSPORT_MAKE_GADGET(PathSection)
28 KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, QPolygonF, path, setPath)
29 KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, QString, description, setDescription)
30 KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, int, floorLevelChange, setFloorLevelChange)
31 KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, PathSection::Maneuver, maneuver, setManeuver)
32 
33 int PathSection::distance() const
34 {
35  if (d->path.size() < 2) {
36  return 0;
37  }
38 
39  int 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 
47 int PathSection::direction() const
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 
67 QJsonObject PathSection::toJson(const PathSection &section)
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 
82 QJsonArray PathSection::toJson(const std::vector<PathSection> &sections)
83 {
84  return Json::toJson(sections);
85 }
86 
87 PathSection PathSection::fromJson(const QJsonObject &obj)
88 {
89  auto section = Json::fromJson<PathSection>(obj);
90  section.setPath(GeoJson::readLineString(obj.value(QLatin1String("path")).toObject()));
91  return section;
92 }
93 
94 std::vector<PathSection> PathSection::fromJson(const QJsonArray &array)
95 {
96  return Json::fromJson<PathSection>(array);
97 }
98 
99 
100 namespace KPublicTransport {
101 class PathPrivate : public QSharedData {
102 public:
103  std::vector<PathSection> sections;
104 };
105 }
106 
107 KPUBLICTRANSPORT_MAKE_GADGET(Path)
108 
109 bool Path::isEmpty() const
110 {
111  return d->sections.empty();
112 }
113 
114 const std::vector<PathSection>& Path::sections() const
115 {
116  return d->sections;
117 }
118 
119 std::vector<PathSection>&& Path::takeSections()
120 {
121  d.detach();
122  return std::move(d->sections);
123 }
124 
125 void Path::setSections(std::vector<PathSection> &&sections)
126 {
127  d.detach();
128  d->sections = std::move(sections);
129 }
130 
131 int 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 
160 int Path::sectionCount() const
161 {
162  return d->sections.size();
163 }
164 
165 #include "moc_path.cpp"
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
Maneuver
Maneuver associated with a path section.
Definition: path.h:46
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
int direction() const
The overall direction of this section in degree.
int distance() const
Length of this path in meters.
int floorLevelChange
Floor level change during this path section.
Definition: path.h:42
QPointF startPoint() const
First point on the path of this section.
Definition: path.cpp:57
std::vector< PathSection > && takeSections()
Moves the path sections out of this object.
Definition: path.cpp:119
const std::vector< PathSection > & sections() const
The path sections.
std::vector< KPublicTransport::PathSection > sections
Access to path sections for QML.
Definition: path.h:93
A section of a Path.
Definition: path.h:24
static QJsonObject toJson(const PathSection &section)
Serializes one path section section to JSON.
Definition: path.cpp:67
QJsonObject toObject() const const
QPolygonF path
The geo coordinate poly-line followed by this path section.
Definition: path.h:28
QJsonArray toArray() const const
Move/drive with the default mode of transport for this path.
Definition: path.h:47
void setSections(std::vector< PathSection > &&sections)
Sets the path sections.
Definition: path.cpp:125
A path followed by any kind of location change.
Definition: path.h:88
QJsonValue value(const QString &key) const const
static float distance(float lat1, float lon1, float lat2, float lon2)
Compute the distance between two geo coordinates, in meters.
Definition: location.cpp:424
static PathSection fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
Definition: path.cpp:87
bool empty() const const
static QJsonObject toJson(const Path &path)
Serializes one path object to JSON.
Definition: path.cpp:146
Maneuver maneuver
Movement maneuver for this path section.
Definition: path.h:54
QPointF endPoint() const
Last point on the path of this section.
Definition: path.cpp:62
QPointF endPoint() const
Last point on this path.
Definition: path.cpp:141
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:05:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.