summaryrefslogtreecommitdiffstats
path: root/src/plugins/geoservices/esri/geocodereply_esri.cpp
blob: 82e83daab277c9826d6f17686f1fdd6249c36708 (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
// Copyright (C) 2013-2018 Esri <contracts@esri.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "geocodereply_esri.h"

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QGeoCoordinate>
#include <QGeoAddress>
#include <QGeoLocation>
#include <QGeoRectangle>

QT_BEGIN_NAMESPACE

GeoCodeReplyEsri::GeoCodeReplyEsri(QNetworkReply *reply, OperationType operationType,
                                   QObject *parent) :
    QGeoCodeReply(parent), m_operationType(operationType)
{
    if (!reply) {
        setError(UnknownError, QStringLiteral("Null reply"));
        return;
    }
    connect(reply, &QNetworkReply::finished, this, &GeoCodeReplyEsri::networkReplyFinished);
    connect(reply, &QNetworkReply::errorOccurred,
            this, &GeoCodeReplyEsri::networkReplyError);
    connect(this, &QGeoCodeReply::aborted, reply, &QNetworkReply::abort);
    connect(this, &QObject::destroyed, reply, &QObject::deleteLater);

    setLimit(1);
    setOffset(0);
}

GeoCodeReplyEsri::~GeoCodeReplyEsri()
{
}

void GeoCodeReplyEsri::networkReplyError(QNetworkReply::NetworkError error)
{
    Q_UNUSED(error);
    QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
    reply->deleteLater();
    setError(QGeoCodeReply::CommunicationError, reply->errorString());
}

void GeoCodeReplyEsri::networkReplyFinished()
{
    QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
    reply->deleteLater();

    if (reply->error() != QNetworkReply::NoError)
        return;

    QJsonDocument document = QJsonDocument::fromJson(reply->readAll());

    if (document.isObject()) {
        QJsonObject object = document.object();

        switch (operationType()) {
        case Geocode:
        {
            const QJsonArray candidates = object.value(QStringLiteral("candidates")).toArray();

            QList<QGeoLocation> locations;

            for (const auto candidate : candidates) {
                if (!candidate.isObject())
                    continue;
                locations.append(parseCandidate(candidate.toObject()));
            }

            setLocations(locations);
            setFinished(true);
        }
            break;

        case ReverseGeocode:
        {
            setLocations({parseAddress(object)});
            setFinished(true);
        }
            break;
        }

    } else {
        setError(QGeoCodeReply::CommunicationError, QStringLiteral("Unknown document"));
    }
}

QGeoLocation GeoCodeReplyEsri::parseAddress(const QJsonObject& object)
{
    QJsonObject addressObject = object.value(QStringLiteral("address")).toObject();

    QGeoAddress address;

    address.setCountryCode(addressObject.value(QStringLiteral("CountryCode")).toString());
    address.setState(addressObject.value(QStringLiteral("Region")).toString());
    address.setCity(addressObject.value(QStringLiteral("City")).toString());
    address.setDistrict(addressObject.value(QStringLiteral("Subregion")).toString());
    address.setPostalCode(addressObject.value(QStringLiteral("Postal")).toString());
    address.setStreet(addressObject.value(QStringLiteral("Address")).toString());

    QGeoCoordinate coordinate;

    QJsonObject locationObject = object.value(QStringLiteral("location")).toObject();

    coordinate.setLongitude(locationObject.value(QStringLiteral("x")).toDouble());
    coordinate.setLatitude(locationObject.value(QStringLiteral("y")).toDouble());

    QGeoLocation location;

    location.setCoordinate(coordinate);
    location.setAddress(address);

    return location;
}

QGeoLocation GeoCodeReplyEsri::parseCandidate(const QJsonObject& candidate)
{
    QGeoCoordinate coordinate;

    QJsonObject locationObject = candidate.value(QStringLiteral("location")).toObject();

    coordinate.setLongitude(locationObject.value(QStringLiteral("x")).toDouble());
    coordinate.setLatitude(locationObject.value(QStringLiteral("y")).toDouble());

    QGeoRectangle extent;

    if (candidate.contains(QStringLiteral("extent"))) {
        QJsonObject extentObject = candidate.value(QStringLiteral("extent")).toObject();

        extent.setTopLeft(QGeoCoordinate(extentObject.value(QStringLiteral("ymin")).toDouble(),
                                         extentObject.value(QStringLiteral("xmin")).toDouble()));

        extent.setBottomRight(QGeoCoordinate(extentObject.value(QStringLiteral("ymax")).toDouble(),
                                             extentObject.value(QStringLiteral("xmax")).toDouble()));
    }

    QJsonObject attributesObject = candidate.value(QStringLiteral("attributes")).toObject();

    QGeoAddress address;

    address.setText(candidate.value(QStringLiteral("address")).toString());

    address.setCountry(attributesObject.value(QStringLiteral("Country")).toString());
    address.setCountryCode(attributesObject.value(QStringLiteral("Country")).toString());
    address.setState(attributesObject.value(QStringLiteral("Region")).toString());
    address.setCity(attributesObject.value(QStringLiteral("City")).toString());
    address.setDistrict(attributesObject.value(QStringLiteral("Subregion")).toString());
    address.setPostalCode(attributesObject.value(QStringLiteral("Postal")).toString());

    QGeoLocation location;

    location.setCoordinate(coordinate);
    location.setBoundingShape(extent);
    location.setAddress(address);

    return location;
}

QT_END_NAMESPACE