summaryrefslogtreecommitdiffstats
path: root/src/plugins/geoservices/nokia/qgeocodejsonparser.cpp
blob: c8c0823e562913d966ec222f0b0b6f33007266ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright (C) 2015 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qgeocodejsonparser.h"

#include <QtPositioning/QGeoShape>
#include <QtPositioning/QGeoRectangle>
#include <QtPositioning/QGeoAddress>
#include <QtPositioning/QGeoCoordinate>

#include <QtCore/QThreadPool>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonArray>
#include <QtCore/QJsonParseError>
#include <QtCore/QVariantMap>

#include <QtDebug>

QT_BEGIN_NAMESPACE

namespace {

/*
    Checks that the given Location object contains the information
    we need and is not malformed in any way.  We expect a Location
    object of the following form:

    "Location": {
        "Address": {
            "AdditionalData": [
                {
                    "key": "CountryName",
                    "value": "Australia"
                },
                {
                    "key": "StateName",
                    "value": "New South Wales"
                }
            ],
            "City": "Sydney",
            "Country": "AUS",
            "District": "Casula",
            "Label": "Casula, Sydney, NSW, Australia",
            "PostalCode": "2170",
            "State": "NSW"
        },
        "DisplayPosition": {
            "Latitude": -33.949509999999997,
            "Longitude": 150.90386000000001
        },
        "LocationId": "NT_5UQ89lKoiI4DIYbOrIR0-D",
        "LocationType": "area",
        "MapReference": {
            "CityId": "1469266800",
            "CountryId": "1469256839",
            "DistrictId": "1469267758",
            "MapId": "NXAM16130",
            "MapReleaseDate": "2016-10-05",
            "MapVersion": "Q1/2016",
            "ReferenceId": "868383156",
            "SideOfStreet": "neither",
            "StateId": "1469256831"
        },
        "MapView": {
            "BottomRight": {
                "Latitude": -33.966839999999998,
                "Longitude": 150.91875999999999
            },
            "TopLeft": {
                "Latitude": -33.937440000000002,
                "Longitude": 150.87457000000001
            }
        }
    }

*/
bool checkLocation(const QJsonObject &loc, QString *errorString)
{
    QJsonObject::const_iterator ait = loc.constFind(QLatin1String("Address"));
    if (ait == loc.constEnd()) {
        *errorString = QLatin1String("Expected Address element within Location object");
        return false;
    } else if (!ait.value().isObject()) {
        *errorString = QLatin1String("Expected Address object within Location object");
        return false;
    }

    QJsonObject::const_iterator dpit = loc.constFind(QLatin1String("DisplayPosition"));
    if (dpit == loc.constEnd()) {
        *errorString = QLatin1String("Expected DisplayPosition element within Location object");
        return false;
    } else if (!dpit.value().isObject()) {
        *errorString = QLatin1String("Expected DisplayPosition object within Location object");
        return false;
    }
    QJsonObject displayPosition = dpit.value().toObject();
    QJsonObject::const_iterator latit = displayPosition.constFind(QLatin1String("Latitude"));
    if (latit == displayPosition.constEnd()) {
        *errorString = QLatin1String("Expected Latitude element within Location.DisplayPosition object");
        return false;
    } else if (!latit.value().isDouble()) {
        *errorString = QLatin1String("Expected Latitude double within Location.DisplayPosition object");
        return false;
    }
    QJsonObject::const_iterator lonit = displayPosition.constFind(QLatin1String("Longitude"));
    if (lonit == displayPosition.constEnd()) {
        *errorString = QLatin1String("Expected Longitude element within Location.DisplayPosition object");
        return false;
    } else if (!lonit.value().isDouble()) {
        *errorString = QLatin1String("Expected Longitude double within Location.DisplayPosition object");
        return false;
    }

    QJsonObject::const_iterator mvit = loc.constFind(QLatin1String("MapView"));
    if (mvit == loc.constEnd()) {
        *errorString = QLatin1String("Expected MapView element within Location object");
        return false;
    } else if (!mvit.value().isObject()) {
        *errorString = QLatin1String("Expected MapView object within Location object");
        return false;
    }
    QJsonObject mapView = mvit.value().toObject();
    QJsonObject::const_iterator brit = mapView.constFind(QLatin1String("BottomRight"));
    if (brit == mapView.constEnd()) {
        *errorString = QLatin1String("Expected BottomRight element within Location.MapView object");
        return false;
    } else if (!brit.value().isObject()) {
        *errorString = QLatin1String("Expected BottomRight object within Location.MapView object");
        return false;
    }
    QJsonObject bottomRight = brit.value().toObject();
    QJsonObject::const_iterator brlatit = bottomRight.constFind(QLatin1String("Latitude"));
    if (brlatit == bottomRight.constEnd()) {
        *errorString = QLatin1String("Expected Latitude element within Location.MapView.BottomRight object");
        return false;
    } else if (!brlatit.value().isDouble()) {
        *errorString = QLatin1String("Expected Latitude double within Location.MapView.BottomRight object");
        return false;
    }
    QJsonObject::const_iterator brlonit = bottomRight.constFind(QLatin1String("Longitude"));
    if (brlonit == bottomRight.constEnd()) {
        *errorString = QLatin1String("Expected Longitude element within Location.MapView.BottomRight object");
        return false;
    } else if (!brlonit.value().isDouble()) {
        *errorString = QLatin1String("Expected Longitude double within Location.MapView.BottomRight object");
        return false;
    }
    QJsonObject::const_iterator tlit = mapView.constFind(QLatin1String("TopLeft"));
    if (tlit == mapView.constEnd()) {
        *errorString = QLatin1String("Expected TopLeft element within Location.MapView object");
        return false;
    } else if (!tlit.value().isObject()) {
        *errorString = QLatin1String("Expected TopLeft object within Location.MapView object");
        return false;
    }
    QJsonObject topLeft = tlit.value().toObject();
    QJsonObject::const_iterator tllatit = topLeft.constFind(QLatin1String("Latitude"));
    if (tllatit == topLeft.constEnd()) {
        *errorString = QLatin1String("Expected Latitude element within Location.MapView.TopLeft object");
        return false;
    } else if (!tllatit.value().isDouble()) {
        *errorString = QLatin1String("Expected Latitude double within Location.MapView.TopLeft object");
        return false;
    }
    QJsonObject::const_iterator tllonit = topLeft.constFind(QLatin1String("Longitude"));
    if (tllonit == bottomRight.constEnd()) {
        *errorString = QLatin1String("Expected Longitude element within Location.MapView.TopLeft object");
        return false;
    } else if (!tllonit.value().isDouble()) {
        *errorString = QLatin1String("Expected Longitude double within Location.MapView.TopLeft object");
        return false;
    }

    return true;
}

/*
    Checks that the given document contains the required information
    and is not malformed in any way.  We expect a document like the
    following:

    {
        "Response": {
            "MetaInfo": {
                "Timestamp": "2016-10-18T08:42:04.369+0000"
            },
            "View": [
                {
                    "ViewId": 0,
                    "_type": "SearchResultsViewType",
                    "Result": [
                        {
                            "Direction": 72.099999999999994,
                            "Distance": -1885.2,
                            "Location": {
                                // OMITTED FOR BREVITY
                            },
                            "MatchLevel": "district",
                            "MatchQuality": {
                                "City": 1,
                                "Country": 1,
                                "District": 1,
                                "PostalCode": 1,
                                "State": 1
                            },
                            "Relevance": 1
                        }
                    ]
                }
            ]
        }
    }
*/
bool checkDocument(const QJsonDocument &doc, QString *errorString)
{
    if (!doc.isObject()) {
        *errorString = QLatin1String("Expected JSON document containing object");
        return false;
    }

    QJsonObject rootObject = doc.object();
    QJsonObject::const_iterator it = rootObject.constFind(QLatin1String("Response"));
    if (it == rootObject.constEnd()) {
        *errorString = QLatin1String("Expected Response element within root object");
        return false;
    } else if (!it.value().isObject()) {
        *errorString = QLatin1String("Expected Response object within root object");
        return false;
    }

    QJsonObject response = it.value().toObject();
    QJsonObject::const_iterator rit = response.constFind(QLatin1String("View"));
    if (rit == response.constEnd()) {
        *errorString = QLatin1String("Expected View element within Response object");
        return false;
    } else if (!rit.value().isArray()) {
        *errorString = QLatin1String("Expected View array within Response object");
        return false;
    }

    const QJsonArray view = rit.value().toArray();
    for (const QJsonValueConstRef viewElement : view) {
        if (!viewElement.isObject()) {
            *errorString = QLatin1String("Expected View array element to be object");
            return false;
        }

        QJsonObject viewObject = viewElement.toObject();
        QJsonObject::const_iterator voit = viewObject.constFind(QLatin1String("Result"));
        if (voit == viewObject.constEnd()) {
            *errorString = QLatin1String("Expected Result element within View array object element");
            return false;
        } else if (!voit.value().isArray()) {
            *errorString = QLatin1String("Expected Result array within View array object element");
            return false;
        }

        const QJsonArray result = voit.value().toArray();
        for (const QJsonValueConstRef resultElement : result) {
            if (!resultElement.isObject()) {
                *errorString = QLatin1String("Expected Result array element to be object");
                return false;
            }

            QJsonObject resultObject = resultElement.toObject();
            QJsonObject::const_iterator roit = resultObject.constFind("Location");
            if (roit == resultObject.constEnd()) {
                *errorString = QLatin1String("Expected Location element in Result array element object");
                return false;
            } else if (!roit.value().isObject()) {
                *errorString = QLatin1String("Expected Location object in Result array element object");
                return false;
            }

            QJsonObject location = roit.value().toObject();
            if (!checkLocation(location, errorString)) {
                return false;
            }
        }
    }

    return true;
}

bool parseLocation(const QJsonObject &obj, const QGeoShape &bounds, QGeoLocation *loc)
{
    QJsonObject displayPosition = obj.value("DisplayPosition").toObject();
    QGeoCoordinate coordinate = QGeoCoordinate(displayPosition.value("Latitude").toDouble(), displayPosition.value("Longitude").toDouble());
    if (bounds.isValid() && !bounds.contains(coordinate)) {
        // manual bounds check failed, location can be omitted from results.
        return false;
    }

    QGeoAddress address;
    QJsonObject addr = obj.value("Address").toObject();
    address.setCountryCode(addr.value("Country").toString());
    address.setState(addr.value("State").toString());
    address.setCounty(addr.value("County").toString());
    address.setCity(addr.value("City").toString());
    address.setDistrict(addr.value("District").toString());
    QString houseNumber = addr.value("HouseNumber").toString();
    QString street = addr.value("Street").toString();
    address.setStreet(houseNumber.isEmpty() ? street : QString("%1 %2").arg(houseNumber, street));
    address.setPostalCode(addr.value("PostalCode").toString());
    QString label = addr.value("Label").toString().trimmed();
    if (!label.isEmpty()) {
        address.setText(label);
    }
    const QJsonArray additionalData = addr.value("AdditionalData").toArray();
    for (const QJsonValueConstRef adv : additionalData) {
        if (adv.isObject()) {
            const QJsonObject &ado(adv.toObject());
            if (ado.value("key").toString() == QLatin1String("CountryName")) {
                address.setCountry(ado.value("value").toString());
            }
        }
    }

    QGeoRectangle boundingBox;
    QJsonObject mapView = obj.value("MapView").toObject();
    QJsonObject bottomRight = mapView.value("BottomRight").toObject();
    QJsonObject topLeft = mapView.value("TopLeft").toObject();
    boundingBox.setBottomRight(QGeoCoordinate(bottomRight.value("Latitude").toDouble(), bottomRight.value("Longitude").toDouble()));
    boundingBox.setTopLeft(QGeoCoordinate(topLeft.value("Latitude").toDouble(), topLeft.value("Longitude").toDouble()));

    loc->setAddress(address);
    loc->setCoordinate(coordinate);
    loc->setBoundingShape(boundingBox);

    return true;
}

void parseDocument(const QJsonDocument &doc, const QGeoShape &bounds, QList<QGeoLocation> *locs)
{
    QJsonArray view = doc.object().value("Response").toObject().value("View").toArray();
    for (const QJsonValueRef viewElement : view) {
        QJsonArray result = viewElement.toObject().value("Result").toArray();
        for (const QJsonValueRef resultElement : result) {
            QGeoLocation location;
            if (parseLocation(resultElement.toObject().value("Location").toObject(), bounds, &location)) {
                locs->append(location);
            }
        }
    }
}

} // namespace

void QGeoCodeJsonParser::setBounds(const QGeoShape &bounds)
{
    m_bounds = bounds;
}

void QGeoCodeJsonParser::parse(const QByteArray &data)
{
    m_data = data;
    QThreadPool::globalInstance()->start(this);
}

void QGeoCodeJsonParser::run()
{
    // parse the document.
    QJsonParseError perror;
    m_document = QJsonDocument::fromJson(m_data, &perror);
    if (perror.error != QJsonParseError::NoError) {
        m_errorString = perror.errorString();
    } else {
        // ensure that the response is valid and contains the information we need.
        if (checkDocument(m_document, &m_errorString)) {
            // extract the location results from the response.
            parseDocument(m_document, m_bounds, &m_results);
            emit results(m_results);
            return;
        }
    }

    emit errorOccurred(m_errorString);
}

QT_END_NAMESPACE