diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-02-07 17:52:21 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-02-13 16:18:46 +0200 |
commit | 82528dfe12b88f2c419e9159f4b3eeb7acdf06af (patch) | |
tree | f902fe2b37abfb2e5177fe4c5f78583c5a5a3a67 | |
parent | 6e7eefaaac7a10c255544c282dc23b6264a25bdc (diff) | |
download | qtlocation-mapboxgl-82528dfe12b88f2c419e9159f4b3eeb7acdf06af.tar.gz |
[Qt] Added QMapbox::Feature → GeoJSON conversion helper
-rw-r--r-- | platform/qt/app/mapwindow.cpp | 24 | ||||
-rw-r--r-- | platform/qt/src/qt_conversion.hpp | 8 | ||||
-rw-r--r-- | platform/qt/src/qt_geojson.hpp | 44 |
3 files changed, 73 insertions, 3 deletions
diff --git a/platform/qt/app/mapwindow.cpp b/platform/qt/app/mapwindow.cpp index 1648816850..908f0f25d2 100644 --- a/platform/qt/app/mapwindow.cpp +++ b/platform/qt/app/mapwindow.cpp @@ -258,6 +258,30 @@ void MapWindow::keyPressEvent(QKeyEvent *ev) } } break; + case Qt::Key_5: { + if (m_map->layerExists("circleLayer")) { + m_map->removeLayer("circleLayer"); + m_map->removeSource("circleSource"); + } else { + QMapbox::CoordinatesCollections geometry { { { m_map->coordinate() } } }; + QMapbox::Feature feature { QMapbox::Feature::PointType, geometry, {}, {} }; + + QVariantMap circleSource; + circleSource["type"] = "geojson"; + circleSource["data"] = QVariant::fromValue<QMapbox::Feature>(feature); + m_map->addSource("circleSource", circleSource); + + QVariantMap circle; + circle["id"] = "circleLayer"; + circle["type"] = "circle"; + circle["source"] = "circleSource"; + m_map->addLayer(circle); + + m_map->setPaintProperty("circleLayer", "circle-radius", 10.0); + m_map->setPaintProperty("circleLayer", "circle-color", QColor("black")); + } + } + break; case Qt::Key_Tab: m_map->cycleDebugOptions(); break; diff --git a/platform/qt/src/qt_conversion.hpp b/platform/qt/src/qt_conversion.hpp index ae05200d66..4b93ca7423 100644 --- a/platform/qt/src/qt_conversion.hpp +++ b/platform/qt/src/qt_conversion.hpp @@ -30,7 +30,13 @@ inline QVariant arrayMember(const QVariant& value, std::size_t i) { } inline bool isObject(const QVariant& value) { - return value.canConvert(QVariant::Map) || value.type() == QVariant::ByteArray; + return value.canConvert(QVariant::Map) + || value.type() == QVariant::ByteArray +#if QT_VERSION >= 0x050000 + || QString(value.typeName()) == QStringLiteral("QMapbox::Feature"); +#else + || QString(value.typeName()) == QString("QMapbox::Feature"); +#endif } inline optional<QVariant> objectMember(const QVariant& value, const char* key) { diff --git a/platform/qt/src/qt_geojson.hpp b/platform/qt/src/qt_geojson.hpp index caec790842..e45729ebeb 100644 --- a/platform/qt/src/qt_geojson.hpp +++ b/platform/qt/src/qt_geojson.hpp @@ -149,6 +149,7 @@ mbgl::Feature asMapboxGLFeature(const QMapbox::Feature &feature) { mbgl::FeatureIdentifier id = asMapboxGLFeatureIdentifier(feature.id); +#if !defined(__GNUC__) || __GNUC__ >= 5 if (feature.type == QMapbox::Feature::PointType) { const QMapbox::Coordinates &points = feature.geometry.first().first(); if (points.size() == 1) { @@ -171,6 +172,34 @@ mbgl::Feature asMapboxGLFeature(const QMapbox::Feature &feature) { return { asMapboxGLMultiPolygon(polygons), std::move(properties), std::move(id) }; } } +#else + mbgl::Feature mbglFeature; + mbglFeature.properties = std::move(properties); + mbglFeature.id = std::move(id); + if (feature.type == QMapbox::Feature::PointType) { + const QMapbox::Coordinates &points = feature.geometry.first().first(); + if (points.size() == 1) { + mbglFeature.geometry = asMapboxGLPoint(points.first()); + } else { + mbglFeature.geometry = asMapboxGLMultiPoint(points); + } + } else if (feature.type == QMapbox::Feature::LineStringType) { + const QMapbox::CoordinatesCollection &lineStrings = feature.geometry.first(); + if (lineStrings.size() == 1) { + mbglFeature.geometry = asMapboxGLLineString(lineStrings.first()); + } else { + mbglFeature.geometry = asMapboxGLMultiLineString(lineStrings); + } + } else { // PolygonType + const QMapbox::CoordinatesCollections &polygons = feature.geometry; + if (polygons.size() == 1) { + mbglFeature.geometry = asMapboxGLPolygon(polygons.first()); + } else { + mbglFeature.geometry = asMapboxGLMultiPolygon(polygons); + } + } + return mbglFeature; +#endif }; } // namespace QMapbox @@ -180,8 +209,19 @@ namespace style { namespace conversion { template <> +Result<GeoJSON> convertGeoJSON(const QMapbox::Feature& feature) { + return Result<GeoJSON> { GeoJSON { asMapboxGLFeature(feature) } }; +} + +template <> Result<GeoJSON> convertGeoJSON(const QVariant& value) { - if (value.type() != QVariant::ByteArray) { +#if QT_VERSION >= 0x050000 + if (value.typeName() == QStringLiteral("QMapbox::Feature")) { +#else + if (value.typeName() == QString("QMapbox::Feature")) { +#endif + return convertGeoJSON(value.value<QMapbox::Feature>()); + } else if (value.type() != QVariant::ByteArray) { return Error { "JSON data must be in QByteArray" }; } @@ -201,7 +241,7 @@ Result<GeoJSON> convertGeoJSON(const QVariant& value) { return Error { message.str() }; } - conversion::Result<GeoJSON> geoJSON = conversion::convertGeoJSON<JSValue>(d); + Result<GeoJSON> geoJSON = convertGeoJSON<JSValue>(d); if (!geoJSON) { return Error { geoJSON.error().message }; } |