Commit 0faaed61 authored by Volker Krause's avatar Volker Krause
Browse files

Implement reading of poly-lines from GeoJSON and Goolge Polyline

We'll need that for parsing journey paths.
parent e59247b1
Pipeline #50085 passed with stage
in 24 seconds
...@@ -60,6 +60,17 @@ private Q_SLOTS: ...@@ -60,6 +60,17 @@ private Q_SLOTS:
QCOMPARE(decoder.canReadMore(), false); QCOMPARE(decoder.canReadMore(), false);
} }
void testPolygon()
{
PolylineDecoder<2> decoder("_p~iF~ps|U_ulLnnqC_mqNvxq`@");
const auto polygon = decoder.readPolygon();
QCOMPARE(decoder.canReadMore(), false);
QCOMPARE(polygon.size(), 3);
QCOMPARE(polygon.isClosed(), false);
QCOMPARE(polygon[0].x(), -120.2);
QCOMPARE(polygon[0].y(), 38.5);
}
#if 0 #if 0
void dumpEncoded() // mostly for manual testing void dumpEncoded() // mostly for manual testing
{ {
......
...@@ -44,6 +44,17 @@ static QPolygonF readPolygonCoordinates(const QJsonArray &coords) ...@@ -44,6 +44,17 @@ static QPolygonF readPolygonCoordinates(const QJsonArray &coords)
return poly; return poly;
} }
QPolygonF GeoJson::readLineString(const QJsonObject &obj)
{
const auto type = obj.value(QLatin1String("type")).toString();
if (type != QLatin1String("LineString")) {
return {};
}
const auto coordinates = obj.value(QLatin1String("coordinates")).toArray();
return readPolygonCoordinates(coordinates);
}
QPolygonF GeoJson::readOuterPolygon(const QJsonObject &obj) QPolygonF GeoJson::readOuterPolygon(const QJsonObject &obj)
{ {
const auto type = obj.value(QLatin1String("type")).toString(); const auto type = obj.value(QLatin1String("type")).toString();
......
...@@ -23,6 +23,9 @@ namespace GeoJson ...@@ -23,6 +23,9 @@ namespace GeoJson
/** Coordinate of a point geometry object. */ /** Coordinate of a point geometry object. */
KPUBLICTRANSPORT_EXPORT QPointF readPoint(const QJsonObject &obj); KPUBLICTRANSPORT_EXPORT QPointF readPoint(const QJsonObject &obj);
/** Reads a line string object. */
QPolygonF readLineString(const QJsonObject &obj);
/** Outer path of a polygon or multi-polygon geometry object. */ /** Outer path of a polygon or multi-polygon geometry object. */
QPolygonF readOuterPolygon(const QJsonObject &obj); QPolygonF readOuterPolygon(const QJsonObject &obj);
} }
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include "kpublictransport_export.h" #include "kpublictransport_export.h"
#include <QPolygonF>
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <limits> #include <limits>
...@@ -75,6 +77,18 @@ public: ...@@ -75,6 +77,18 @@ public:
return readNextInt() / 100000.0; return readNextInt() / 100000.0;
} }
inline QPolygonF readPolygon()
{
static_assert(Dim == 2, "Polygons require a two-dimensional polyline");
QPolygonF p;
while (canReadMore()) {
const auto lat = readNextDouble();
const auto lon = readNextDouble();
p.push_back({lon, lat});
}
return p;
}
private: private:
int m_nextDim = 0; int m_nextDim = 0;
std::array<int32_t, Dim> m_accu; std::array<int32_t, Dim> m_accu;
......
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