Commit 868ced77 authored by Volker Krause's avatar Volker Krause
Browse files

Consider path length for computing the journey section length

This gives us considerably better estimates when we don't get the exact
length from the backend.

Hafas even has separate length values for each line segment along a path,
but those values match our computation within a 0.1% margin, so not worth
explicitly storing that.
parent 573776e1
Pipeline #50331 passed with stage
in 24 seconds
......@@ -171,7 +171,7 @@
},
{
"disruptionEffect": "NormalService",
"distance": 4146,
"distance": 4156,
"from": {
"identifier": {
"testid": "80001020"
......
......@@ -72,7 +72,7 @@
},
{
"disruptionEffect": "NormalService",
"distance": 4514,
"distance": 17665,
"expectedArrivalPlatform": "",
"expectedArrivalTime": "2019-11-11T22:09:00",
"expectedDeparturePlatform": "",
......
......@@ -3,7 +3,7 @@
"sections": [
{
"disruptionEffect": "NoService",
"distance": 144221,
"distance": 163558,
"from": {
"identifier": {
"unit-test": "8011113"
......
......@@ -407,9 +407,19 @@ static std::vector<Path> parsePaths(const QJsonArray &polyL)
PolylineDecoder<2> crdEncYXDecoder(crdEncYX.constData());
section.setPath(crdEncYXDecoder.readPolygon());
// TODO there's more data in here
path.setSections({section});
paths.push_back(path);
#if 0 // check if there is more information in the crdEncDist than can be derived from that path data
const auto crdEncDist = polyObj.value(QLatin1String("crdEncDist")).toString().toUtf8();
PolylineDecoder<1> crdEncDistDecoder(crdEncDist.constData());
int encDist = 0;
while (crdEncDistDecoder.canReadMore()) {
encDist = crdEncDistDecoder.readNextInt();
}
qDebug() << encDist << section.distance();
#endif
paths.push_back(std::move(path));
}
return paths;
}
......
......@@ -90,7 +90,7 @@ int JourneySectionPrivate::estimatedDistance() const
}
d += Location::distance(startLat, startLon, to.latitude(), to.longitude());
return d;
return std::max(d, path.distance());
}
bool JourneySection::hasExpectedDepartureTime() const
......
......@@ -8,6 +8,7 @@
#include "datatypes_p.h"
#include "json_p.h"
#include "../geo/geojson_p.h"
#include "location.h"
using namespace KPublicTransport;
......@@ -21,6 +22,16 @@ public:
KPUBLICTRANSPORT_MAKE_GADGET(PathSection)
KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, QPolygonF, path, setPath)
int PathSection::distance() const
{
int dist = 0;
for (auto it = d->path.begin(); it != std::prev(d->path.end()); ++it) {
const auto nextIt = std::next(it);
dist += Location::distance((*it).y(), (*it).x(), (*nextIt).y(), (*nextIt).x());
}
return dist;
}
QJsonObject PathSection::toJson(const PathSection &section)
{
auto obj = Json::toJson(section);
......@@ -77,6 +88,11 @@ void Path::setSections(std::vector<PathSection> &&sections)
d->sections = std::move(sections);
}
int Path::distance() const
{
return std::accumulate(d->sections.begin(), d->sections.end(), 0, [](int d, const auto &sec) { return d + sec.distance(); });
}
QJsonObject Path::toJson(const Path &path)
{
auto obj = Json::toJson(path);
......
......@@ -28,7 +28,14 @@ class KPUBLICTRANSPORT_EXPORT PathSection
KPUBLICTRANSPORT_PROPERTY(QPolygonF, path, setPath)
// TODO add more properties: maneuver instructions, human-readable path description, floor level
/** The length of this path section in meters. */
Q_PROPERTY(int distance READ distance STORED false)
public:
/** Length of this path section in meters. */
int distance() const;
/** Serializes one path section section to JSON. */
static QJsonObject toJson(const PathSection &section);
/** Serializes a vector of path sections to JSON. */
......@@ -52,6 +59,9 @@ class KPUBLICTRANSPORT_EXPORT Path
{
KPUBLICTRANSPORT_GADGET(Path)
/** The length of this path in meters. */
Q_PROPERTY(int distance READ distance STORED false)
public:
/** Returns @c true if this is an empty/not-set path. */
bool isEmpty() const;
......@@ -63,6 +73,9 @@ public:
/** Sets the path sections. */
void setSections(std::vector<PathSection> &&sections);
/** Length of this path in meters. */
int distance() const;
/** Serializes one path object to JSON. */
static QJsonObject toJson(const Path &path);
/** Deserialize an object from JSON. */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment