summaryrefslogtreecommitdiff
path: root/platform/qt/src/qt_geojson.hpp
blob: a6958b7edcf4a0f71abbe03d9eb0367f2f8fc291 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#pragma once

#include <mapbox/geojson.hpp>
#include <mbgl/style/conversion/geojson.hpp>

#include <QMapbox>

#include <QByteArray>
#include <QDebug>
#include <QVariant>

#include <string>

namespace QMapbox {

mbgl::Point<double> asMapboxGLPoint(const QMapbox::Coordinate &coordinate) {
    return mbgl::Point<double> { coordinate.second, coordinate.first };
}

mbgl::MultiPoint<double> asMapboxGLMultiPoint(const QMapbox::Coordinates &multiPoint) {
    mbgl::MultiPoint<double> mbglMultiPoint;
    mbglMultiPoint.reserve(multiPoint.size());
    for (const auto &point: multiPoint) {
        mbglMultiPoint.emplace_back(asMapboxGLPoint(point));
    }
    return mbglMultiPoint;
};

mbgl::LineString<double> asMapboxGLLineString(const QMapbox::Coordinates &lineString) {
    mbgl::LineString<double> mbglLineString;
    mbglLineString.reserve(lineString.size());
    for (const auto &coordinate : lineString) {
        mbglLineString.emplace_back(asMapboxGLPoint(coordinate));
    }
    return mbglLineString;
};

mbgl::MultiLineString<double> asMapboxGLMultiLineString(const QMapbox::CoordinatesCollection &multiLineString) {
    mbgl::MultiLineString<double> mbglMultiLineString;
    mbglMultiLineString.reserve(multiLineString.size());
    for (const auto &lineString : multiLineString) {
        mbglMultiLineString.emplace_back(std::forward<mbgl::LineString<double>>(asMapboxGLLineString(lineString)));
    }
    return mbglMultiLineString;
};

mbgl::Polygon<double> asMapboxGLPolygon(const QMapbox::CoordinatesCollection &polygon) {
    mbgl::Polygon<double> mbglPolygon;
    mbglPolygon.reserve(polygon.size());
    for (const auto &linearRing : polygon) {
        mbgl::LinearRing<double> mbglLinearRing;
        mbglLinearRing.reserve(linearRing.size());
        for (const auto &coordinate: linearRing) {
            mbglLinearRing.emplace_back(asMapboxGLPoint(coordinate));
        }
        mbglPolygon.emplace_back(std::move(mbglLinearRing));
    }
    return mbglPolygon;
};

mbgl::MultiPolygon<double> asMapboxGLMultiPolygon(const QMapbox::CoordinatesCollections &multiPolygon) {
    mbgl::MultiPolygon<double> mbglMultiPolygon;
    mbglMultiPolygon.reserve(multiPolygon.size());
    for (const auto &polygon : multiPolygon) {
        mbglMultiPolygon.emplace_back(std::forward<mbgl::Polygon<double>>(asMapboxGLPolygon(polygon)));
    }
    return mbglMultiPolygon;
};

mbgl::Value asMapboxGLPropertyValue(const QVariant &value) {
    auto valueList = [](const QVariantList &list) {
        std::vector<mbgl::Value> mbglList;
        mbglList.reserve(list.size());
        for (const auto& listValue : list) {
            mbglList.emplace_back(asMapboxGLPropertyValue(listValue));
        }
        return mbglList;
    };

    auto valueMap = [](const QVariantMap &map) {
        std::unordered_map<std::string, mbgl::Value> mbglMap;
        mbglMap.reserve(map.size());
        auto it = map.constBegin();
        while (it != map.constEnd()) {
            mbglMap.emplace(std::make_pair(it.key().toStdString(), asMapboxGLPropertyValue(it.value())));
            ++it;
        }
        return mbglMap;
    };

    switch (value.type()) {
#if QT_VERSION >= 0x050000
    case QMetaType::UnknownType:
#else
    case QVariant::Invalid:
#endif
        return mbgl::NullValue {};
    case QMetaType::Bool:
        return { value.toBool() };
    case QMetaType::ULongLong:
        return { uint64_t(value.toULongLong()) };
    case QMetaType::LongLong:
        return { int64_t(value.toLongLong()) };
    case QMetaType::Double:
        return { value.toDouble() };
    case QMetaType::QString:
        return { value.toString().toStdString() };
    case QMetaType::QVariantList:
        return valueList(value.toList());
    case QMetaType::QVariantMap:
        return valueMap(value.toMap());
    default:
        qWarning() << "Unsupported feature property value:" << value;
        return {};
    }
}

mbgl::FeatureIdentifier asMapboxGLFeatureIdentifier(const QVariant &id) {
    switch (id.type()) {
#if QT_VERSION >= 0x050000
    case QMetaType::UnknownType:
#else
    case QVariant::Invalid:
#endif
        return {};
    case QMetaType::ULongLong:
        return { uint64_t(id.toULongLong()) };
    case QMetaType::LongLong:
        return { int64_t(id.toLongLong()) };
    case QMetaType::Double:
        return { id.toDouble() };
    case QMetaType::QString:
        return { id.toString().toStdString() };
    default:
        qWarning() << "Unsupported feature identifier:" << id;
        return {};
    }
}

mbgl::Feature asMapboxGLFeature(const QMapbox::Feature &feature) {
    mbgl::PropertyMap properties;
    properties.reserve(feature.properties.size());
    auto it = feature.properties.constBegin();
    while (it != feature.properties.constEnd()) {
        properties.emplace(std::make_pair(it.key().toStdString(), asMapboxGLPropertyValue(it.value())));
    }

    mbgl::FeatureIdentifier id = asMapboxGLFeatureIdentifier(feature.id);

    if (feature.type == QMapbox::Feature::PointType) {
        const QMapbox::Coordinates &points = feature.geometry.first().first();
        if (points.size() == 1) {
            return { asMapboxGLPoint(points.first()), std::move(properties), std::move(id) };
        } else {
            return { asMapboxGLMultiPoint(points), std::move(properties), std::move(id) };
        }
    } else if (feature.type == QMapbox::Feature::LineStringType) {
        const QMapbox::CoordinatesCollection &lineStrings = feature.geometry.first();
        if (lineStrings.size() == 1) {
            return { asMapboxGLLineString(lineStrings.first()), std::move(properties), std::move(id) };
        } else {
            return { asMapboxGLMultiLineString(lineStrings), std::move(properties), std::move(id) };
        }
    } else { // PolygonType
        const QMapbox::CoordinatesCollections &polygons = feature.geometry;
        if (polygons.size() == 1) {
            return { asMapboxGLPolygon(polygons.first()), std::move(properties), std::move(id) };
        } else {
            return { asMapboxGLMultiPolygon(polygons), std::move(properties), std::move(id) };
        }
    }
};

} // namespace QMapbox

namespace mbgl {
namespace style {
namespace conversion {

template <>
optional<GeoJSON> Converter<GeoJSON>::operator()(const QVariant& value, Error& error) const {
#if QT_VERSION >= 0x050000
    if (value.typeName() == QStringLiteral("QMapbox::Feature")) {
#else
    if (value.typeName() == QString("QMapbox::Feature")) {
#endif
        return GeoJSON { asMapboxGLFeature(value.value<QMapbox::Feature>()) };
    } else if (value.type() != QVariant::ByteArray) {
        error = { "JSON data must be in QByteArray" };
        return {};
    }

    QByteArray data = value.toByteArray();
    return convert<GeoJSON>(std::string(data.constData(), data.size()), error);
}

} // namespace conversion
} // namespace style
} // namespace mbgl