1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 Mapbox, Inc.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qmapboxcommon.h"
41
42 #include <QtCore/QJsonArray>
43 #include <QtPositioning/QGeoAddress>
44 #include <QtPositioning/QGeoCoordinate>
45 #include <QtPositioning/QGeoRectangle>
46
mapboxNameForCategory(const QString & category)47 QString QMapboxCommon::mapboxNameForCategory(const QString &category)
48 {
49 if (category.isEmpty())
50 return category;
51
52 QString categoryName = category;
53 categoryName[0] = categoryName[0].toUpper();
54 return categoryName;
55 }
56
57 // https://www.mapbox.com/api-documentation/#response-object
parseGeoLocation(const QJsonObject & response)58 QGeoLocation QMapboxCommon::parseGeoLocation(const QJsonObject &response)
59 {
60 QGeoLocation location;
61
62 QGeoAddress address;
63
64 QString streetAddress = response.value(QStringLiteral("address")).toString();
65
66 // If place type is 'address', the street address is a combo of 'text' and
67 // 'address'. The former provides the street name, and the latter provides
68 // the street number in that case.
69 if (response.value(QStringLiteral("place_type")).isArray()) {
70 foreach (const QJsonValue &value, response.value(QStringLiteral("place_type")).toArray()) {
71 if (!value.isString())
72 continue;
73 if (value.toString() == QStringLiteral("address")) {
74 streetAddress.prepend(response.value(QStringLiteral("text")).toString() + QLatin1Char(' '));
75 break;
76 }
77 }
78 }
79
80 if (response.value(QStringLiteral("properties")).isObject()) {
81 const QJsonObject properties = response.value(QStringLiteral("properties")).toObject();
82
83 // Prefer properties.address over address.
84 const QString addressString = properties.value(QStringLiteral("address")).toString();
85 if (!addressString.isEmpty())
86 streetAddress = addressString;
87 }
88
89 address.setStreet(streetAddress);
90
91 if (response.value(QStringLiteral("context")).isArray()) {
92 foreach (const QJsonValue &value, response.value(QStringLiteral("context")).toArray()) {
93 if (!value.isObject())
94 continue;
95
96 const QJsonObject object = value.toObject();
97 const QString valueId = object.value(QStringLiteral("id")).toString();
98 const QString valueText = object.value(QStringLiteral("text")).toString();
99
100 if (valueId.isEmpty() || valueText.isEmpty())
101 continue;
102
103 // XXX: locality, neighborhood, address, poi, poi.landmark
104 if (valueId.startsWith(QStringLiteral("country"))) {
105 address.setCountry(valueText);
106 const QString countryCode = object.value(QStringLiteral("short_code")).toString();
107 if (!countryCode.isEmpty())
108 address.setCountryCode(countryCode);
109 } else if (valueId.startsWith(QStringLiteral("region"))) {
110 address.setState(valueText);
111 } else if (valueId.startsWith(QStringLiteral("postcode"))) {
112 address.setPostalCode(valueText);
113 } else if (valueId.startsWith(QStringLiteral("district"))) {
114 address.setDistrict(valueText);
115 } else if (valueId.startsWith(QStringLiteral("place"))) {
116 address.setCity(valueText);
117 }
118 }
119 } else {
120 // Fallback to using information from place_name.
121 const QString placeName = response.value(QStringLiteral("place_name")).toString();
122
123 // Remove actual place name.
124 address.setText(placeName.mid(placeName.indexOf(QLatin1Char(',')) + 1));
125 }
126
127 location.setAddress(address);
128
129 QJsonArray bbox = response.value(QStringLiteral("bbox")).toArray();
130 double top = bbox.at(3).toDouble();
131 double left = bbox.at(0).toDouble();
132 double bottom = bbox.at(1).toDouble();
133 double right = bbox.at(2).toDouble();
134 location.setBoundingBox(QGeoRectangle(QGeoCoordinate(top, left), QGeoCoordinate(bottom, right)));
135
136 QJsonArray center = response.value(QStringLiteral("center")).toArray();
137 location.setCoordinate(QGeoCoordinate(center.at(1).toDouble(), center.at(0).toDouble()));
138
139 return location;
140 }
141