-
Notifications
You must be signed in to change notification settings - Fork 2
/
ccGreyhoundResource.cpp
90 lines (72 loc) · 2.29 KB
/
ccGreyhoundResource.cpp
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
#include <QJsonDocument>
#include <QJsonArray>
#include <QEventLoop>
#include "ccGreyhoundResource.h"
QString resource_name_from_url(const QString& url)
{
QStringList splits = url.split('/');
return splits.at(splits.size() - 1);
}
QJsonObject greyhound_info(const QUrl& url)
{
QNetworkAccessManager qnam;
QNetworkRequest request;
const QUrl info_url(url.toString() + "/info");
request.setUrl(info_url);
QNetworkReply *reply = qnam.get(request);
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() != QNetworkReply::NoError) {
throw std::runtime_error(reply->errorString().toStdString());
}
auto document = QJsonDocument::fromJson(reply->readAll());
if (!document.isObject()) {
throw std::runtime_error("Received info is not a proper json object");
}
return document.object();
}
ccGreyhoundResource::ccGreyhoundResource(QUrl url)
: ccCustomHObject(QString("[Greyhound] %1").arg(resource_name_from_url(url.toString())))
, m_url(std::move(url))
, m_info(greyhound_info(m_url))
{
}
GreyhoundInfo::GreyhoundInfo(const const QJsonObject& info)
: m_info(info)
{
}
std::vector<QString> GreyhoundInfo::available_dim_name() const
{
QJsonArray schema = m_info.value("schema").toArray();
std::vector<QString> dim_names;
dim_names.reserve(schema.size());
for (const auto& dimension : schema) {
QJsonObject dimension_infos = dimension.toObject();
dim_names.push_back(dimension_infos.value("name").toString());
}
return dim_names;
}
int GreyhoundInfo::base_depth() const
{
return m_info.value("baseDepth").toInt();
}
CCVector3d GreyhoundInfo::offset() const
{
QJsonArray offset = m_info.value("offset").toArray();
return { offset.at(0).toDouble(), offset.at(1).toDouble(), offset.at(2).toDouble() };
}
CCVector3d GreyhoundInfo::bounds_conforming_min() const
{
QJsonArray bounds_conforming = m_info.value("boundsConforming").toArray();
return { bounds_conforming.at(0).toDouble(), bounds_conforming.at(1).toDouble(), bounds_conforming.at(2).toDouble() };
}
CCVector3d GreyhoundInfo::bounds_min() const
{
QJsonArray bounds = m_info.value("bounds").toArray();
return { bounds.at(0).toDouble(), bounds.at(1).toDouble(), bounds.at(2).toDouble() };
}
QString GreyhoundInfo::srs() const
{
return m_info.value("srs").toString();
}