KPublicTransport

journeyutil.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "journeyutil_p.h"
8
9#include "logging.h"
10#include "stopoverutil_p.h"
11#include "timeutil_p.h"
12
13#include "geo/pathfilter_p.h"
14
15#include <KPublicTransport/Journey>
16#include <KPublicTransport/Stopover>
17
18#include <QDateTime>
19#include <QTimeZone>
20
21using namespace KPublicTransport;
22
23QDateTime JourneyUtil::firstTransportDeparture(const Journey &jny)
24{
25 for (const auto &section : jny.sections()) {
26 if (section.mode() == JourneySection::PublicTransport) {
27 return section.scheduledDepartureTime();
28 }
29 }
30
31 return jny.scheduledDepartureTime();
32}
33
34bool JourneyUtil::firstTransportDepartureLessThan(const Journey &lhs, const Journey &rhs)
35{
36 return firstTransportDeparture(lhs) < firstTransportDeparture(rhs);
37}
38
39bool JourneyUtil::firstTransportDepartureEqual(const Journey &lhs, const Journey &rhs)
40{
41 return firstTransportDeparture(lhs) == firstTransportDeparture(rhs);
42}
43
44void JourneyUtil::applyTimeZone(Journey &jny, const QTimeZone &tz)
45{
46 auto sections = std::move(jny.takeSections());
47 for (auto &sec : sections) {
48 sec.setScheduledDepartureTime(TimeUtil::applyTimeZone(sec.scheduledDepartureTime(), tz));
49 sec.setExpectedDepartureTime(TimeUtil::applyTimeZone(sec.expectedDepartureTime(), tz));
50 sec.setScheduledArrivalTime(TimeUtil::applyTimeZone(sec.scheduledArrivalTime(), tz));
51 sec.setExpectedArrivalTime(TimeUtil::applyTimeZone(sec.expectedArrivalTime(), tz));
52
53 auto stops = sec.takeIntermediateStops();
54 for (auto &stop : stops) {
55 StopoverUtil::applyTimeZone(stop, tz);
56 }
57 sec.setIntermediateStops(std::move(stops));
58 }
59 jny.setSections(std::move(sections));
60}
61
62void JourneyUtil::propagateTimeZones(Journey &jny)
63{
64 auto sections = std::move(jny.takeSections());
65 for (auto &sec : sections) {
66 propagateTimeZones(sec);
67 }
68 jny.setSections(std::move(sections));
69}
70
71void JourneyUtil::propagateTimeZones(JourneySection &sec)
72{
73 if (const auto tz = sec.from().timeZone(); tz.isValid()) {
74 sec.setScheduledDepartureTime(TimeUtil::applyTimeZone(sec.scheduledDepartureTime(), tz));
75 sec.setExpectedDepartureTime(TimeUtil::applyTimeZone(sec.expectedDepartureTime(), tz));
76 }
77 if (const auto tz = sec.to().timeZone(); tz.isValid()) {
78 sec.setScheduledArrivalTime(TimeUtil::applyTimeZone(sec.scheduledArrivalTime(), tz));
79 sec.setExpectedArrivalTime(TimeUtil::applyTimeZone(sec.expectedArrivalTime(), tz));
80 }
81
82 auto stops = sec.takeIntermediateStops();
83 for (auto &stop : stops) {
84 StopoverUtil::propagateTimeZone(stop);
85 }
86 sec.setIntermediateStops(std::move(stops));
87}
88
89void JourneyUtil::postProcessPath(JourneySection &section)
90{
91 if (!section.from().hasCoordinate() || !section.to().hasCoordinate() || section.path().isEmpty()) {
92 return;
93 }
94
95 // remove implausible paths
96 const auto pointDist = Location::distance(section.from(), section.to());
97 const auto pathDist = section.path().distance();
98 if (pathDist > pointDist * 10) {
99 qCDebug(Log) << "Dropping implausibly long path:" << pointDist << pathDist;
100 section.setPath({});
101 }
102
103 // filter spikes found in rail paths in nearly all backends and GTFS shapes
104 if (section.mode() == JourneySection::PublicTransport && Line::modeIsRailBound(section.route().line().mode())) {
105 auto path = section.path();
106 auto pathSecs = path.takeSections();
107 for (auto &pathSec : pathSecs) {
108 QPolygonF p = pathSec.path();
109 PathFilter::removeSpikes(p, 55.0);
110 pathSec.setPath(p);
111 }
112 path.setSections(std::move(pathSecs));
113 section.setPath(path);
114 }
115}
A segment of a journey plan.
Definition journey.h:32
KPublicTransport::Path path
Movement path for this journey section.
Definition journey.h:141
KPublicTransport::Location from
Departure location of this segment.
Definition journey.h:83
QDateTime expectedArrivalTime
Actual arrival time, if available.
Definition journey.h:71
void setIntermediateStops(std::vector< Stopover > &&stops)
Set the intermediate stops.
Definition journey.cpp:245
QDateTime scheduledDepartureTime
Planned departure time.
Definition journey.h:56
KPublicTransport::Route route
Route to take on this segment.
Definition journey.h:87
KPublicTransport::Location to
Arrival location of this segment.
Definition journey.h:85
std::vector< Stopover > && takeIntermediateStops()
Moves the intermediate stops out of this object.
Definition journey.cpp:239
Mode mode
Mode of transport for this section.
Definition journey.h:53
QDateTime scheduledArrivalTime
Planned arrival time.
Definition journey.h:67
QDateTime expectedDepartureTime
Actual departure time, if available.
Definition journey.h:60
A journey plan.
Definition journey.h:287
QDateTime scheduledDepartureTime
Departure time of the journey, according to schedule.
Definition journey.h:292
void setSections(std::vector< JourneySection > &&sections)
Sets the journey sections.
Definition journey.cpp:774
QList< KPublicTransport::JourneySection > sections
Journey sections for consumption by QML.
Definition journey.h:290
std::vector< JourneySection > && takeSections()
Moves the journey sections out of this object.
Definition journey.cpp:768
KPublicTransport::Line::Mode mode
Type of transport.
Definition line.h:60
static bool modeIsRailBound(KPublicTransport::Line::Mode mode)
true if mode is bounds to rail tracks.
Definition line.cpp:139
QTimeZone timeZone() const
The timezone this location is in, if known.
Definition location.cpp:126
static double distance(double lat1, double lon1, double lat2, double lon2)
Compute the distance between two geo coordinates, in meters.
Definition location.cpp:458
int distance
The length of this path in meters.
Definition path.h:122
bool isEmpty() const
Returns true if this is an empty/not-set path.
Definition path.cpp:143
KPublicTransport::Line line
Line this route belongs to.
Definition line.h:151
void stop(Ekos::AlignState mode)
QString path(const QString &relativePath)
Query operations and data types for accessing realtime public transport information from online servi...
bool isValid() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:50:52 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.