Commit 96580c4a authored by Volker Krause's avatar Volker Krause
Browse files

Add GBFS v2.1 vehicle type data parser

Yet to be used.
parent 09d2c108
Pipeline #78150 passed with stage
in 17 seconds
......@@ -83,6 +83,7 @@ target_sources(KPublicTransport PRIVATE
gbfs/gbfsjob.cpp
gbfs/gbfsservice.cpp
gbfs/gbfsstore.cpp
gbfs/gbfsvehicletypes.cpp
geo/geojson.cpp
geo/polylinedecoder.cpp
......
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "gbfsvehicletypes.h"
#include "gbfs.h"
#include "gbfsservice.h"
#include "gbfsstore.h"
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
using namespace KPublicTransport;
namespace KPublicTransport {
static bool operator<(const GBFSVehicleType &lhs, const GBFSVehicleType &rhs)
{
return lhs.typeId < rhs.typeId;
}
static bool operator<(const GBFSVehicleType &lhs, QStringView rhs)
{
return lhs.typeId < rhs;
}
}
template <typename T>
struct value_map_entry
{
const char *name;
T value;
};
static constexpr const value_map_entry<GBFSVehicleType::FormFactor> form_factor_map[] = {
{ "bicycle", GBFSVehicleType::Bicycle },
{ "car", GBFSVehicleType::Car },
{ "moped", GBFSVehicleType::Moped },
{ "scooter", GBFSVehicleType::Scooter },
{ "other", GBFSVehicleType::Other },
};
static constexpr const value_map_entry<GBFSVehicleType::PropulsionType> propulsion_map[] = {
{ "human", GBFSVehicleType::Human },
{ "electric_assist", GBFSVehicleType::ElectricAssist },
{ "electric", GBFSVehicleType::Electric },
{ "combustion", GBFSVehicleType::Combustion },
};
template <typename T, std::size_t N>
static T lookupValue(const value_map_entry<T>(&map)[N], QStringView name)
{
for (const auto &entry : map) {
if (name.compare(QLatin1String(entry.name), Qt::CaseInsensitive) == 0) {
return entry.value;
}
}
qDebug() << "unknown value:" << name;
return {};
}
GBFSVehicleType GBFSVehicleType::fromJson(const QJsonObject &obj)
{
GBFSVehicleType v;
v.typeId = obj.value(QLatin1String("vehicle_type_id")).toString();
v.formFactor = lookupValue(form_factor_map, obj.value(QLatin1String("form_factor")).toString());
v.propulsionType = lookupValue(propulsion_map, obj.value(QLatin1String("propulsion_type")).toString());
return v;
}
GBFSVehicleTypes::GBFSVehicleTypes(const GBFSService &feed)
{
GBFSStore store(feed.systemId);
const auto doc = store.loadData(GBFS::VehicleTypes);
const auto types = doc.object().value(QLatin1String("data")).toObject().value(QLatin1String("vehicle_types")).toArray();
m_vehicleTypes.reserve(types.size());
for (const auto &typeVal : types) {
auto v = GBFSVehicleType::fromJson(typeVal.toObject());
if (!v.typeId.isEmpty()) {
m_vehicleTypes.push_back(std::move(v));
}
}
std::sort(m_vehicleTypes.begin(), m_vehicleTypes.end());
qDebug() << "Found" << m_vehicleTypes.size() << "vehicle types.";
}
GBFSVehicleTypes::~GBFSVehicleTypes() = default;
GBFSVehicleType GBFSVehicleTypes::vehicleType(QStringView typeId) const
{
const auto it = std::lower_bound(m_vehicleTypes.begin(), m_vehicleTypes.end(), typeId);
if (it != m_vehicleTypes.end() && (*it).typeId == typeId) {
return (*it);
}
return {};
}
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef KPUBLICTRANSPORT_GBFSVEHICLETYPES_H
#define KPUBLICTRANSPORT_GBFSVEHICLETYPES_H
#include <QString>
#include <vector>
class QJsonObject;
namespace KPublicTransport {
class GBFSService;
/** GBFS v2.1 vehicle type entry. */
class GBFSVehicleType
{
public:
QString typeId;
enum FormFactor {
UndefinedFormFactor,
Bicycle,
Car,
Moped,
Scooter,
Other,
};
FormFactor formFactor = UndefinedFormFactor;
enum PropulsionType {
UndefinedPropulsion,
Human,
ElectricAssist,
Electric,
Combustion,
};
PropulsionType propulsionType = UndefinedPropulsion;
static GBFSVehicleType fromJson(const QJsonObject &obj);
};
/** GBFS v2.1 vehicle types data parsing. */
class GBFSVehicleTypes
{
public:
explicit GBFSVehicleTypes(const GBFSService &feed);
~GBFSVehicleTypes();
GBFSVehicleType vehicleType(QStringView typeId) const;
private:
std::vector<GBFSVehicleType> m_vehicleTypes;
};
}
#endif // KPUBLICTRANSPORT_GBFSVEHICLETYPES_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