summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Angelelli <paolo.angelelli@qt.io>2018-08-20 18:32:01 +0200
committerPaolo Angelelli <paolo.angelelli@qt.io>2018-08-22 10:36:10 +0000
commit34ee76720ee09ac692ea149ce44413cb5c0c7b99 (patch)
treefb94e9b2db62125782b920dd1f86d2c529af9b77
parentecef017a48f6fc291af3d4e79ddd5829f0386f1b (diff)
downloadqtlocation-34ee76720ee09ac692ea149ce44413cb5c0c7b99.tar.gz
Honor QGeoPolygon holes in the MapboxGL plugin
This since mapbox-gl-native is able to properly render these holes. Change-Id: I29ee9efd256c607ad36543a949b64699de310371 Reviewed-by: Bruno de Oliveira Abinader <brunoabinader@gmail.com> Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r--src/plugins/geoservices/mapboxgl/qmapboxglstylechange.cpp30
1 files changed, 21 insertions, 9 deletions
diff --git a/src/plugins/geoservices/mapboxgl/qmapboxglstylechange.cpp b/src/plugins/geoservices/mapboxgl/qmapboxglstylechange.cpp
index d9de5ba9..b45fdef1 100644
--- a/src/plugins/geoservices/mapboxgl/qmapboxglstylechange.cpp
+++ b/src/plugins/geoservices/mapboxgl/qmapboxglstylechange.cpp
@@ -41,6 +41,7 @@
#include <QtCore/QRegularExpression>
#include <QtCore/QStringList>
#include <QtPositioning/QGeoPath>
+#include <QtPositioning/QGeoPolygon>
#include <QtQml/QJSValue>
namespace {
@@ -66,7 +67,7 @@ QString getId(QDeclarativeGeoMapItemBase *mapItem)
// Mapbox GL supports geometry segments that spans above 180 degrees in
// longitude. To keep visual expectations in parity with Qt, we need to adapt
// the coordinates to always use the shortest path when in ambiguity.
-bool geoRectangleCrossesDateLine(const QGeoRectangle &rect) {
+static bool geoRectangleCrossesDateLine(const QGeoRectangle &rect) {
return rect.topLeft().longitude() > rect.bottomRight().longitude();
}
@@ -112,24 +113,35 @@ QMapbox::Feature featureFromMapCircle(QDeclarativeCircleMapItem *mapItem)
return QMapbox::Feature(QMapbox::Feature::PolygonType, geometry, {}, getId(mapItem));
}
-QMapbox::Feature featureFromMapPolygon(QDeclarativePolygonMapItem *mapItem)
+static QMapbox::Coordinates qgeocoordinate2mapboxcoordinate(const QList<QGeoCoordinate> &crds, const bool crossesDateline, bool closed = false)
{
- const QGeoPath *path = static_cast<const QGeoPath *>(&mapItem->geoShape());
QMapbox::Coordinates coordinates;
- const bool crossesDateline = geoRectangleCrossesDateLine(path->boundingGeoRectangle());
- for (const QGeoCoordinate &coordinate : path->path()) {
+ for (const QGeoCoordinate &coordinate : crds) {
if (!coordinates.empty() && crossesDateline && qAbs(coordinate.longitude() - coordinates.last().second) > 180.0) {
coordinates << QMapbox::Coordinate { coordinate.latitude(), coordinate.longitude() + (coordinate.longitude() >= 0 ? -360.0 : 360.0) };
} else {
coordinates << QMapbox::Coordinate { coordinate.latitude(), coordinate.longitude() };
}
}
+ if (closed && !coordinates.empty() && coordinates.last() != coordinates.first())
+ coordinates.append(coordinates.first()); // closing the path
+ return coordinates;
+}
- if (!coordinates.empty())
- coordinates.append(coordinates.first()); // closing the path
-
- QMapbox::CoordinatesCollections geometry { { coordinates } };
+QMapbox::Feature featureFromMapPolygon(QDeclarativePolygonMapItem *mapItem)
+{
+ const QGeoPolygon *polygon = static_cast<const QGeoPolygon *>(&mapItem->geoShape());
+ const bool crossesDateline = geoRectangleCrossesDateLine(polygon->boundingGeoRectangle());
+ QMapbox::CoordinatesCollections geometry;
+ QMapbox::CoordinatesCollection poly;
+ QMapbox::Coordinates coordinates = qgeocoordinate2mapboxcoordinate(polygon->path(), crossesDateline, true);
+ poly.push_back(coordinates);
+ for (int i = 0; i < polygon->holesCount(); ++i) {
+ coordinates = qgeocoordinate2mapboxcoordinate(polygon->holePath(i), crossesDateline, true);
+ poly.push_back(coordinates);
+ }
+ geometry.push_back(poly);
return QMapbox::Feature(QMapbox::Feature::PolygonType, geometry, {}, getId(mapItem));
}