summaryrefslogtreecommitdiff
path: root/src/plugins/geoservices/mapbox/qgeoroutereplymapbox.cpp
diff options
context:
space:
mode:
authorPaolo Angelelli <paolo.angelelli@qt.io>2017-11-28 16:53:54 +0100
committerPaolo Angelelli <paolo.angelelli@qt.io>2017-11-29 23:02:27 +0000
commitfe821df2179d496a3ffc853076e9c62f96abde2a (patch)
tree649ce88bd270af2a74b47ae6e529fc0ecd6122e0 /src/plugins/geoservices/mapbox/qgeoroutereplymapbox.cpp
parent057fe199941b8783312abeedde315118b59ccb8e (diff)
downloadqtlocation-fe821df2179d496a3ffc853076e9c62f96abde2a.tar.gz
Unify OSRM backends for OSM and Mapbox plugins
This patch unifies the OSRM backend in both the OSM and Mapbox plugins, adding some extra functionalities to QGeoRouteParserOsrmV5 to handle the extra osrm-text-instructions information coming from the Mapbox servers. It also adds a plugin parameter to let the user choose whether to use the server's text instructions or the plugin-generated ones. Change-Id: Id7ce73f4285e2e7db6872f40d72c0610847fce91 Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'src/plugins/geoservices/mapbox/qgeoroutereplymapbox.cpp')
-rw-r--r--src/plugins/geoservices/mapbox/qgeoroutereplymapbox.cpp129
1 files changed, 11 insertions, 118 deletions
diff --git a/src/plugins/geoservices/mapbox/qgeoroutereplymapbox.cpp b/src/plugins/geoservices/mapbox/qgeoroutereplymapbox.cpp
index 8fc3386a..43b18454 100644
--- a/src/plugins/geoservices/mapbox/qgeoroutereplymapbox.cpp
+++ b/src/plugins/geoservices/mapbox/qgeoroutereplymapbox.cpp
@@ -39,7 +39,8 @@
****************************************************************************/
#include "qgeoroutereplymapbox.h"
-
+#include "qgeoroutingmanagerenginemapbox.h"
+#include <QtLocation/private/qgeorouteparser_p.h>
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonArray>
@@ -48,40 +49,6 @@
QT_BEGIN_NAMESPACE
-static QList<QGeoCoordinate> parsePolyline(const QString &line)
-{
- QList<QGeoCoordinate> path;
- QByteArray data(line.toLocal8Bit());
-
- int mode = 0, shift = 0, value = 0, coord[2] = {0, 0};
- for (int i = 0; i < data.length(); ++i) {
- int c = data.at(i) - 63;
- value |= (c & 0x1f) << shift;
- shift += 5;
- if (c & 0x20) continue;
- coord[mode] += (value & 1) ? ~(value >> 1) : (value >> 1);
- if (mode) path.append(QGeoCoordinate((double)coord[0]/1e5, (double)coord[1]/1e5));
- mode = 1 - mode;
- value = shift = 0;
- }
- return path;
-}
-
-static QList<QGeoCoordinate> parseGeometry(const QJsonValue &geometry)
-{
- QList<QGeoCoordinate> path;
- if (geometry.isString()) path = parsePolyline(geometry.toString());
- if (geometry.isObject()) {
- QJsonArray coords = geometry.toObject().value(QStringLiteral("coordinates")).toArray();
- for (int i = 0; i < coords.count(); i++) {
- QJsonArray coord = coords.at(i).toArray();
- if (coord.count() != 2) continue;
- path.append(QGeoCoordinate(coord.at(1).toDouble(), coord.at(0).toDouble()));
- }
- }
- return path;
-}
-
QGeoRouteReplyMapbox::QGeoRouteReplyMapbox(QNetworkReply *reply, const QGeoRouteRequest &request,
QObject *parent)
: QGeoRouteReply(request, parent)
@@ -101,73 +68,6 @@ QGeoRouteReplyMapbox::~QGeoRouteReplyMapbox()
{
}
-static QGeoRoute constructRoute(const QJsonObject &obj)
-{
- QGeoRoute route;
- route.setDistance(obj.value(QStringLiteral("distance")).toDouble());
- route.setTravelTime(obj.value(QStringLiteral("duration")).toDouble());
-
- QList<QGeoCoordinate> path = parseGeometry(obj.value(QStringLiteral("geometry")));
- route.setPath(path);
-
- QGeoRouteSegment firstSegment, lastSegment;
- QJsonArray legs = obj.value(QStringLiteral("legs")).toArray();
-
- for (int i = 0; i < legs.count(); i++) {
- QJsonObject leg = legs.at(i).toObject();
- QJsonArray steps = leg.value("steps").toArray();
-
- for (int j = 0; j < steps.count(); j++) {
- QJsonObject step = steps.at(j).toObject();
- QJsonObject stepManeuver = step.value("maneuver").toObject();
-
- QGeoRouteSegment segment;
- segment.setDistance(step.value("distance").toDouble());
- segment.setTravelTime(step.value(QStringLiteral("duration")).toDouble());
-
- QGeoManeuver maneuver;
- maneuver.setDistanceToNextInstruction(step.value("distance").toDouble());
- maneuver.setInstructionText(stepManeuver.value("instruction").toString());
- maneuver.setTimeToNextInstruction(step.value(QStringLiteral("duration")).toDouble());
- QJsonArray location = stepManeuver.value(QStringLiteral("location")).toArray();
- if (location.count() > 1)
- maneuver.setPosition(QGeoCoordinate(location.at(0).toDouble(), location.at(1).toDouble()));
-
- QString modifier = stepManeuver.value("modifier").toString();
- int bearing1 = stepManeuver.value("bearing_before").toInt();
- int bearing2 = stepManeuver.value("bearing_after").toInt();
-
- if (modifier == "straight")
- maneuver.setDirection(QGeoManeuver::DirectionForward);
- else if (modifier == "slight right")
- maneuver.setDirection(QGeoManeuver::DirectionLightRight);
- else if (modifier == "right")
- maneuver.setDirection(QGeoManeuver::DirectionRight);
- else if (modifier == "sharp right")
- maneuver.setDirection(QGeoManeuver::DirectionHardRight);
- else if (modifier == "uturn")
- maneuver.setDirection(bearing2 - bearing1 > 180 ? QGeoManeuver::DirectionUTurnLeft : QGeoManeuver::DirectionUTurnRight);
- else if (modifier == "sharp left")
- maneuver.setDirection(QGeoManeuver::DirectionHardLeft);
- else if (modifier == "left")
- maneuver.setDirection(QGeoManeuver::DirectionLeft);
- else if (modifier == "slight left")
- maneuver.setDirection(QGeoManeuver::DirectionLightLeft);
- else
- maneuver.setDirection(QGeoManeuver::NoDirection);
-
- segment.setManeuver(maneuver);
- segment.setPath(parseGeometry(step.value(QStringLiteral("geometry"))));
-
- if (!firstSegment.isValid()) firstSegment = segment;
- if (lastSegment.isValid()) lastSegment.setNextRouteSegment(segment);
- lastSegment = segment;
- }
- }
- route.setFirstRouteSegment(firstSegment);
- return route;
-}
-
void QGeoRouteReplyMapbox::networkReplyFinished()
{
QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
@@ -176,26 +76,19 @@ void QGeoRouteReplyMapbox::networkReplyFinished()
if (reply->error() != QNetworkReply::NoError)
return;
- QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
- if (document.isObject()) {
- QJsonObject object = document.object();
+ QGeoRoutingManagerEngineMapbox *engine = qobject_cast<QGeoRoutingManagerEngineMapbox *>(parent());
+ const QGeoRouteParser *parser = engine->routeParser();
- QString status = object.value(QStringLiteral("code")).toString();
- if (status != QStringLiteral("Ok")) {
- setError(QGeoRouteReply::UnknownError, object.value(QStringLiteral("message")).toString());
- return;
- }
+ QList<QGeoRoute> routes;
+ QString errorString;
+ QGeoRouteReply::Error error = parser->parseReply(routes, errorString, reply->readAll());
- QList<QGeoRoute> list;
- QJsonArray routes = object.value(QStringLiteral("routes")).toArray();
- for (int i = 0; i < routes.count(); i++) {
- QGeoRoute route = constructRoute(routes.at(i).toObject());
- list.append(route);
- }
- setRoutes(list);
+ if (error == QGeoRouteReply::NoError) {
+ setRoutes(routes.mid(0, request().numberAlternativeRoutes() + 1));
+ // setError(QGeoRouteReply::NoError, status); // can't do this, or NoError is emitted and does damages
setFinished(true);
} else {
- setError(QGeoRouteReply::ParseError, QStringLiteral("Couldn't parse json."));
+ setError(error, errorString);
}
}