-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathqgsoapifschemarequest.cpp
More file actions
266 lines (239 loc) · 9.92 KB
/
Copy pathqgsoapifschemarequest.cpp
File metadata and controls
266 lines (239 loc) · 9.92 KB
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
/***************************************************************************
qgsoapifschemarequest.cpp
-------------------------
begin : March 2025
copyright : (C) 2025 by Even Rouault
email : even.rouault at spatialys.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <nlohmann/json.hpp>
#include <QString>
using namespace Qt::StringLiterals;
using namespace nlohmann;
#include "qgslogger.h"
#include "qgsmessagelog.h"
#include "qgsoapifschemarequest.h"
#include "moc_qgsoapifschemarequest.cpp"
#include "qgsoapifutils.h"
#include "qgswfsconstants.h"
#include <algorithm>
#include <map>
#include <QTextCodec>
QgsOapifSchemaRequest::QgsOapifSchemaRequest( const QgsDataSourceUri &uri )
: QgsBaseNetworkRequest( QgsAuthorizationSettings( uri.username(), uri.password(), QgsHttpHeaders(), uri.authConfigId() ), "OAPIF" )
{
// Using Qt::DirectConnection since the download might be running on a different thread.
// In this case, the request was sent from the main thread and is executed with the main
// thread being blocked in future.waitForFinished() so we can run code on this object which
// lives in the main thread without risking havoc.
connect( this, &QgsBaseNetworkRequest::downloadFinished, this, &QgsOapifSchemaRequest::processReply, Qt::DirectConnection );
}
const QgsOapifSchemaRequest::Schema &QgsOapifSchemaRequest::schema( const QUrl &schemaUrl )
{
mUrl = schemaUrl;
sendGET( schemaUrl, QString( "application/schema+json" ), /*synchronous=*/true, /*forceRefresh=*/false );
return mSchema;
}
QString QgsOapifSchemaRequest::errorMessageWithReason( const QString &reason )
{
return tr( "Download of schema failed: %1" ).arg( reason );
}
void QgsOapifSchemaRequest::processReply()
{
if ( mErrorCode != QgsBaseNetworkRequest::NoError )
{
return;
}
const QByteArray &buffer = mResponse;
if ( buffer.isEmpty() )
{
mErrorMessage = tr( "empty response" );
mErrorCode = QgsBaseNetworkRequest::ServerExceptionError;
return;
}
QgsDebugMsgLevel( u"parsing Schema response: "_s + buffer, 4 );
QTextCodec::ConverterState state;
QTextCodec *codec = QTextCodec::codecForName( "UTF-8" );
Q_ASSERT( codec );
const QString utf8Text = codec->toUnicode( buffer.constData(), buffer.size(), &state );
if ( state.invalidChars != 0 )
{
mErrorCode = QgsBaseNetworkRequest::ApplicationLevelError;
mErrorMessage = errorMessageWithReason( tr( "Invalid UTF-8 content" ) );
return;
}
// Key is the explicit x-ogc-propertySeq or a synthetized one
std::map<int, QgsField> fields;
int seqNumber = 1;
int largestSeqNumber = 1;
bool seqNumbersAreOk = true;
try
{
const json j = json::parse( utf8Text.toStdString() );
if ( j.is_object() && j.contains( "properties" ) )
{
const json jProperties = j["properties"];
if ( jProperties.is_object() )
{
for ( const auto &[key, val] : jProperties.items() )
{
if ( val.is_object() && val.contains( "type" ) )
{
QgsField field( QString::fromStdString( key ), QMetaType::Type::QString );
const json jType = val["type"];
if ( jType.is_string() )
{
const QString type = QString::fromStdString( jType.get<std::string>() );
if ( type == "integer"_L1 )
{
field.setType( QMetaType::Type::LongLong );
}
else if ( type == "number"_L1 )
{
field.setType( QMetaType::Type::Double );
}
else if ( type == "boolean"_L1 )
{
field.setType( QMetaType::Type::Bool );
}
else if ( type == "object"_L1 )
{
field.setType( QMetaType::Type::QVariantMap );
}
else if ( type == "array"_L1 )
{
field.setType( QMetaType::Type::QVariantList );
}
else if ( type == "string"_L1 )
{
if ( val.contains( "format" ) )
{
const json jFormat = val["format"];
if ( jFormat.is_string() )
{
const QString format = QString::fromStdString( jFormat.get<std::string>() );
if ( format == "date-time"_L1 )
{
field.setType( QMetaType::Type::QDateTime );
}
else if ( format == "date"_L1 )
{
field.setType( QMetaType::Type::QDate );
}
}
}
}
else
{
QgsDebugMsgLevel( u"Unhandled OGC API Features schema type: "_s + type, 3 );
}
}
if ( val.contains( "title" ) )
{
const json jTitle = val["title"];
if ( jTitle.is_string() )
{
field.setAlias( QString::fromStdString( jTitle.get<std::string>() ) );
}
}
if ( val.contains( "description" ) )
{
const json jDescription = val["description"];
if ( jDescription.is_string() )
{
field.setComment( QString::fromStdString( jDescription.get<std::string>() ) );
}
}
if ( seqNumbersAreOk && val.contains( "x-ogc-propertySeq" ) )
{
const json jOgcPropertySeq = val["x-ogc-propertySeq"];
if ( jOgcPropertySeq.is_number_integer() )
{
seqNumber = jOgcPropertySeq.get<int>();
}
}
if ( val.contains( "readOnly" ) )
{
const json jReadOnly = val["readOnly"];
if ( jReadOnly.is_boolean() )
{
field.setReadOnly( jReadOnly.get<bool>() );
}
}
// Make sure that there are no colliding sequence numbers. If so,
// give up honouring them.
if ( seqNumbersAreOk && fields.find( seqNumber ) != fields.end() )
{
QgsMessageLog::logMessage( tr( "Schema at %1 has inconsistent x-ogc-propertySeq" ).arg( mUrl.toString() ), tr( "OAPIF" ) );
seqNumbersAreOk = false;
seqNumber = largestSeqNumber + 1;
}
fields[seqNumber] = std::move( field );
largestSeqNumber = std::max( largestSeqNumber, seqNumber );
++seqNumber;
}
if ( val.is_object() && val.contains( "x-ogc-role" ) )
{
const json jOgcRole = val["x-ogc-role"];
if ( jOgcRole.is_string() )
{
if ( QString::fromStdString( jOgcRole.get<std::string>() ) == "primary-geometry"_L1 )
{
mSchema.mGeometryColumnName = QString::fromStdString( key );
mSchema.mWKBType = Qgis::WkbType::Unknown;
if ( val.contains( "format" ) )
{
const json jFormat = val["format"];
if ( jFormat.is_string() )
{
const QString format = QString::fromStdString( jFormat.get<std::string>() );
const QMap<QString, Qgis::WkbType> mapFormatToWkbType = {
{ u"geometry-point"_s, Qgis::WkbType::Point },
{ u"geometry-multipoint"_s, Qgis::WkbType::MultiPoint },
{ u"geometry-linestring"_s, Qgis::WkbType::LineString },
{ u"geometry-multilinestring"_s, Qgis::WkbType::MultiLineString },
{ u"geometry-circularstring"_s, Qgis::WkbType::CircularString },
{ u"geometry-compoundcurve"_s, Qgis::WkbType::CompoundCurve },
{ u"geometry-multicurve"_s, Qgis::WkbType::MultiCurve },
{ u"geometry-polygon"_s, Qgis::WkbType::Polygon },
{ u"geometry-multipolygon"_s, Qgis::WkbType::MultiPolygon },
{ u"geometry-curvepolygon"_s, Qgis::WkbType::CurvePolygon },
{ u"geometry-multisurface"_s, Qgis::WkbType::MultiSurface },
{ u"geometry-geometrycollection"_s, Qgis::WkbType::GeometryCollection },
{ u"geometry-any"_s, Qgis::WkbType::Unknown },
{ u"geometry-point-or-multipoint"_s, Qgis::WkbType::MultiPoint },
{ u"geometry-linestring-or-multilinestring"_s, Qgis::WkbType::MultiLineString },
{ u"geometry-polygon-or-multipolygon"_s, Qgis::WkbType::MultiPolygon },
};
const auto it = mapFormatToWkbType.constFind( format );
if ( it != mapFormatToWkbType.end() )
{
mSchema.mWKBType = it.value();
}
}
}
}
}
}
}
}
}
// Order fields by ascending sequence number
for ( const auto &[seqNumber, f] : fields )
{
mSchema.mFields.append( f );
}
}
catch ( const json::parse_error &ex )
{
mErrorCode = QgsBaseNetworkRequest::ApplicationLevelError;
mErrorMessage = errorMessageWithReason( tr( "Cannot decode JSON document: %1" ).arg( QString::fromStdString( ex.what() ) ) );
return;
}
}