geojson.cpp 3.09 KB
Newer Older
Volker Krause's avatar
Volker Krause committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
    SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "geojson_p.h"

#include <QJsonArray>
#include <QJsonObject>
#include <QPointF>
#include <QPolygonF>

using namespace KPublicTransport;

static QPointF readPointCoordinates(const QJsonArray &coords)
{
    if (coords.size() != 2) {
        return {};
    }

    return {coords.at(0).toDouble(), coords.at(1).toDouble()};
}

QPointF GeoJson::readPoint(const QJsonObject &obj)
{
    const auto type = obj.value(QLatin1String("type")).toString();
    if (type != QLatin1String("Point")) {
        return {};
    }

    const auto coordinates = obj.value(QLatin1String("coordinates")).toArray();
    return readPointCoordinates(coordinates);
}

static QPolygonF readPolygonCoordinates(const QJsonArray &coords)
{
    QPolygonF poly;
39
    poly.reserve(coords.size());
Volker Krause's avatar
Volker Krause committed
40
41
42
43
44
45
46
    for (const auto &pointV : coords) {
        const auto point = pointV.toArray();
        poly.push_back(readPointCoordinates(point));
    }
    return poly;
}

47
48
49
50
51
52
53
54
55
56
57
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);
}

Volker Krause's avatar
Volker Krause committed
58
59
60
61
62
63
64
65
66
67
68
69
70
QPolygonF GeoJson::readOuterPolygon(const QJsonObject &obj)
{
    const auto type = obj.value(QLatin1String("type")).toString();
    if (type == QLatin1String("Polygon")) {
        const auto coordinates = obj.value(QLatin1String("coordinates")).toArray();
        if (coordinates.empty()) {
            return {};
        }
        return readPolygonCoordinates(coordinates.at(0).toArray());
    } else if (type == QLatin1String("MultiPolygon")) {
        const auto coordinates = obj.value(QLatin1String("coordinates")).toArray();
        QPolygonF poly;
        for (const auto &polyV : coordinates) {
71
72
            const auto polyElements = polyV.toArray();
            if (polyElements.empty()) {
Volker Krause's avatar
Volker Krause committed
73
74
                return {};
            }
75
            auto subPoly = readPolygonCoordinates(polyElements.at(0).toArray());
Volker Krause's avatar
Volker Krause committed
76
77
78
79
80
81
82
            poly = poly.empty() ? std::move(subPoly) : poly.united(subPoly);
        }
        return poly;
    }

    return {};
}
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

static QJsonArray writePoint(const QPointF &p)
{
    return QJsonArray({ p.x(), p.y() });
}

QJsonObject GeoJson::writeLineString(const QPolygonF &lineString)
{
    QJsonObject obj;
    obj.insert(QLatin1String("type"), QLatin1String("LineString"));

    QJsonArray coords;
    for (const auto &p : lineString) {
        coords.push_back(writePoint(p));
    }
    obj.insert(QLatin1String("coordinates"), coords);
    return obj;
}
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

QJsonObject GeoJson::writePolygon(const QPolygonF &polygon)
{
    QJsonObject obj;
    obj.insert(QLatin1String("type"), QLatin1String("Polygon"));

    QJsonArray coords;
    for (const auto &p : polygon) {
        coords.push_back(writePoint(p));
    }
    QJsonArray polyArray;
    polyArray.push_back(coords);
    obj.insert(QLatin1String("coordinates"), polyArray);
    return obj;
}