[qt] Support feature collection for GeoJSON source data

This commit adds support of `QVector<QMapbox::Feature>`
and `QList<QMapbox::Feature>` for data in GeoJSON source.
This commit is contained in:
xylosper
2019-01-31 12:39:04 +09:00
committed by Thiago Marcos P. Santos
parent 43dc191f54
commit 6b2f17191b

View File

@@ -35,7 +35,9 @@ public:
static bool isObject(const QVariant& value) {
return value.canConvert(QVariant::Map)
|| value.type() == QVariant::ByteArray
|| QString(value.typeName()) == QStringLiteral("QMapbox::Feature");
|| QString(value.typeName()) == QStringLiteral("QMapbox::Feature")
|| value.userType() == qMetaTypeId<QVector<QMapbox::Feature>>()
|| value.userType() == qMetaTypeId<QList<QMapbox::Feature>>();
}
static optional<QVariant> objectMember(const QVariant& value, const char* key) {
@@ -119,6 +121,10 @@ public:
static optional<GeoJSON> toGeoJSON(const QVariant& value, Error& error) {
if (value.typeName() == QStringLiteral("QMapbox::Feature")) {
return GeoJSON { asMapboxGLFeature(value.value<QMapbox::Feature>()) };
} else if (value.userType() == qMetaTypeId<QVector<QMapbox::Feature>>()) {
return featureCollectionToGeoJSON(value.value<QVector<QMapbox::Feature>>());
} else if (value.userType() == qMetaTypeId<QList<QMapbox::Feature>>()) {
return featureCollectionToGeoJSON(value.value<QList<QMapbox::Feature>>());
} else if (value.type() != QVariant::ByteArray) {
error = { "JSON data must be in QByteArray" };
return {};
@@ -127,6 +133,17 @@ public:
QByteArray data = value.toByteArray();
return parseGeoJSON(std::string(data.constData(), data.size()), error);
}
private:
template<typename T>
static GeoJSON featureCollectionToGeoJSON(const T &features) {
mapbox::feature::feature_collection<double> collection;
collection.reserve(static_cast<std::size_t>(features.size()));
for (const auto &feature : features) {
collection.push_back(asMapboxGLFeature(feature));
}
return GeoJSON { std::move(collection) };
}
};
template <class T, class...Args>