Commit 7653d8cd authored by Volker Krause's avatar Volker Krause
Browse files

Make GBFS coordinate reading also handle values wrongly encoded as strings

Not what the specification says, but occurs in the wild.
parent 71a3d78d
......@@ -33,6 +33,7 @@ ecm_add_test(deutschebahntest.cpp LINK_LIBRARIES Qt5::Test KPublicTransport)
ecm_add_test(otpparsertest.cpp LINK_LIBRARIES Qt5::Test KPublicTransport)
ecm_add_test(oebbparsertest.cpp LINK_LIBRARIES Qt5::Test KPublicTransport)
ecm_add_test(ivvassparsertest.cpp LINK_LIBRARIES Qt5::Test KPublicTransport)
ecm_add_test(gbfsreadertest.cpp LINK_LIBRARIES Qt5::Test KPublicTransport)
ecm_add_test(publictransportmanagertest.cpp LINK_LIBRARIES Qt5::Test KPublicTransport)
ecm_add_test(cachetest.cpp LINK_LIBRARIES Qt5::Test KPublicTransport)
......
......@@ -81,6 +81,7 @@ target_sources(KPublicTransport PRIVATE
gbfs/gbfs.qrc
gbfs/gbfsbackend.cpp
gbfs/gbfsjob.cpp
gbfs/gbfsreader.cpp
gbfs/gbfsservice.cpp
gbfs/gbfsstore.cpp
gbfs/gbfsvehicletypes.cpp
......
......@@ -8,6 +8,7 @@
#include "gbfsservice.h"
#include "gbfsstore.h"
#include "gbfsjob.h"
#include "gbfsreader.h"
#include "gbfsvehicletypes.h"
#include <KPublicTransport/Attribution>
......@@ -112,8 +113,8 @@ static void appendResults(const GBFSService &service, const LocationRequest &req
std::vector<QString> selectedStationIds;
for (const auto &stationV : stations) {
const auto station = stationV.toObject();
const auto lat = station.value(QLatin1String("lat")).toDouble();
const auto lon = station.value(QLatin1String("lon")).toDouble();
const auto lat = GBFSReader::readLatitude(station);
const auto lon = GBFSReader::readLongitude(station);
if (Location::distance(lat, lon, req.latitude(), req.longitude()) > req.maximumDistance()) {
continue;
}
......@@ -159,8 +160,8 @@ static void appendResults(const GBFSService &service, const LocationRequest &req
if (bike.value(QLatin1String("is_reserved")).toBool() || bike.value(QLatin1String("is_disabled")).toBool()) {
continue;
}
const auto lat = bike.value(QLatin1String("lat")).toDouble();
const auto lon = bike.value(QLatin1String("lon")).toDouble();
const auto lat = GBFSReader::readLatitude(bike);
const auto lon = GBFSReader::readLongitude(bike);
if (Location::distance(lat, lon, req.latitude(), req.longitude()) > req.maximumDistance()) {
continue;
}
......
......@@ -5,6 +5,7 @@
*/
#include "gbfsjob.h"
#include "gbfsreader.h"
#include "geo/geojson_p.h"
#include <KPublicTransport/Location>
......@@ -336,11 +337,11 @@ void GBFSJob::collectCoordinates(const QJsonArray &array)
for (const auto &statVal : array) {
const auto station = statVal.toObject();
const auto lat = station.value(QLatin1String("lat")).toDouble(NAN);
const auto lat = GBFSReader::readLatitude(station);
if (!std::isnan(lat) && lat >= -90.0 && lat <= 90.0 && lat != 0.0) {
m_latitudes.push_back(lat);
}
const auto lon = station.value(QLatin1String("lon")).toDouble(NAN);
const auto lon = GBFSReader::readLongitude(station);
if (!std::isnan(lon) && lon >= -180.0 && lon <= 180.0 && lon != 0.0) {
m_longitudes.push_back(lon);
}
......
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "gbfsreader.h"
#include <QJsonObject>
#include <cmath>
using namespace KPublicTransport;
static double readDouble(const QJsonValue &v)
{
if (v.isDouble()) {
return v.toDouble(NAN);
}
if (v.isString()) {
bool valid = false;
const auto d = v.toString().toDouble(&valid);
return valid ? d : NAN;
}
return NAN;
}
double GBFSReader::readLatitude(const QJsonObject &obj)
{
return readDouble(obj.value(QLatin1String("lat")));
}
double GBFSReader::readLongitude(const QJsonObject &obj)
{
return readDouble(obj.value(QLatin1String("lon")));
}
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef KPUBLICTRANSPORT_GBFSREADER_H
#define KPUBLICTRANSPORT_GBFSREADER_H
class QJsonObject;
namespace KPublicTransport {
/** Utility methods for reading GBFS data. */
namespace GBFSReader
{
/** Reads and sanity-checks geographic coordinates. */
double readLatitude(const QJsonObject &obj);
double readLongitude(const QJsonObject &obj);
}
}
#endif // KPUBLICTRANSPORT_GBFSREADER_H
Supports Markdown
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