QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsarcgisrestquery.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsarcgisrestquery.cpp
3 ----------------------
4 begin : December 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsarcgisrestquery.h"
17#include "moc_qgsarcgisrestquery.cpp"
18#include "qgsarcgisrestutils.h"
22#include "qgslogger.h"
23#include "qgsapplication.h"
24#include "qgsmessagelog.h"
25#include "qgsauthmanager.h"
26#include "qgsvariantutils.h"
27
28#include <QUrl>
29#include <QUrlQuery>
30#include <QImageReader>
31#include <QRegularExpression>
32#include <QJsonParseError>
33
34QVariantMap QgsArcGisRestQueryUtils::getServiceInfo( const QString &baseurl, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders, const QString &urlPrefix )
35{
36 // http://sampleserver5.arcgisonline.com/arcgis/rest/services/Energy/Geology/FeatureServer?f=json
37 QUrl queryUrl( baseurl );
38 QUrlQuery query( queryUrl );
39 query.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
40 queryUrl.setQuery( query );
41 return queryServiceJSON( queryUrl, authcfg, errorTitle, errorText, requestHeaders, nullptr, urlPrefix );
42}
43
44QVariantMap QgsArcGisRestQueryUtils::getLayerInfo( const QString &layerurl, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders, const QString &urlPrefix )
45{
46 // http://sampleserver5.arcgisonline.com/arcgis/rest/services/Energy/Geology/FeatureServer/1?f=json
47 QUrl queryUrl( layerurl );
48 QUrlQuery query( queryUrl );
49 query.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
50 queryUrl.setQuery( query );
51 return queryServiceJSON( queryUrl, authcfg, errorTitle, errorText, requestHeaders, nullptr, urlPrefix );
52}
53
54QVariantMap QgsArcGisRestQueryUtils::getObjectIds( const QString &layerurl, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders, const QString &urlPrefix, const QgsRectangle &bbox, const QString &whereClause )
55{
56 // http://sampleserver5.arcgisonline.com/arcgis/rest/services/Energy/Geology/FeatureServer/1/query?where=1%3D1&returnIdsOnly=true&f=json
57 QUrl queryUrl( layerurl + "/query" );
58 QUrlQuery query( queryUrl );
59 query.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
60 query.addQueryItem( QStringLiteral( "where" ), whereClause.isEmpty() ? QStringLiteral( "1=1" ) : whereClause );
61 query.addQueryItem( QStringLiteral( "returnIdsOnly" ), QStringLiteral( "true" ) );
62 if ( !bbox.isNull() )
63 {
64 query.addQueryItem( QStringLiteral( "geometry" ), QStringLiteral( "%1,%2,%3,%4" )
65 .arg( bbox.xMinimum(), 0, 'f', -1 ).arg( bbox.yMinimum(), 0, 'f', -1 )
66 .arg( bbox.xMaximum(), 0, 'f', -1 ).arg( bbox.yMaximum(), 0, 'f', -1 ) );
67 query.addQueryItem( QStringLiteral( "geometryType" ), QStringLiteral( "esriGeometryEnvelope" ) );
68 query.addQueryItem( QStringLiteral( "spatialRel" ), QStringLiteral( "esriSpatialRelEnvelopeIntersects" ) );
69 }
70 queryUrl.setQuery( query );
71 return queryServiceJSON( queryUrl, authcfg, errorTitle, errorText, requestHeaders, nullptr, urlPrefix );
72}
73
74QgsRectangle QgsArcGisRestQueryUtils::getExtent( const QString &layerurl, const QString &whereClause, const QString &authcfg, const QgsHttpHeaders &requestHeaders, const QString &urlPrefix )
75{
76 // http://sampleserver5.arcgisonline.com/arcgis/rest/services/Energy/Geology/FeatureServer/1/query?where=1%3D1&returnExtentOnly=true&f=json
77 QUrl queryUrl( layerurl + "/query" );
78 QUrlQuery query( queryUrl );
79 query.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
80 query.addQueryItem( QStringLiteral( "where" ), whereClause );
81 query.addQueryItem( QStringLiteral( "returnExtentOnly" ), QStringLiteral( "true" ) );
82 queryUrl.setQuery( query );
83 QString errorTitle;
84 QString errorText;
85 const QVariantMap res = queryServiceJSON( queryUrl, authcfg, errorTitle, errorText, requestHeaders, nullptr, urlPrefix );
86 if ( res.isEmpty() )
87 {
88 QgsDebugError( QStringLiteral( "getExtent failed: %1 - %2" ).arg( errorTitle, errorText ) );
89 return QgsRectangle();
90 }
91
92 return QgsArcGisRestUtils::convertRectangle( res.value( QStringLiteral( "extent" ) ) );
93}
94
95QVariantMap QgsArcGisRestQueryUtils::getObjects( const QString &layerurl, const QString &authcfg, const QList<quint32> &objectIds, const QString &crs,
96 bool fetchGeometry, const QStringList &fetchAttributes,
97 bool fetchM, bool fetchZ,
98 const QgsRectangle &filterRect,
99 QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders, QgsFeedback *feedback, const QString &urlPrefix )
100{
101 QStringList ids;
102 for ( const int id : objectIds )
103 {
104 ids.append( QString::number( id ) );
105 }
106 QUrl queryUrl( layerurl + "/query" );
107 QUrlQuery query( queryUrl );
108 query.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
109 query.addQueryItem( QStringLiteral( "objectIds" ), ids.join( QLatin1Char( ',' ) ) );
110 if ( !crs.isEmpty() && crs.contains( ':' ) )
111 {
112 const QString wkid = crs.indexOf( QLatin1Char( ':' ) ) >= 0 ? crs.split( ':' )[1] : QString();
113 query.addQueryItem( QStringLiteral( "inSR" ), wkid );
114 query.addQueryItem( QStringLiteral( "outSR" ), wkid );
115 }
116
117 query.addQueryItem( QStringLiteral( "returnGeometry" ), fetchGeometry ? QStringLiteral( "true" ) : QStringLiteral( "false" ) );
118
119 QString outFields;
120 if ( fetchAttributes.isEmpty() )
121 outFields = QStringLiteral( "*" );
122 else
123 outFields = fetchAttributes.join( ',' );
124 query.addQueryItem( QStringLiteral( "outFields" ), outFields );
125
126 query.addQueryItem( QStringLiteral( "returnM" ), fetchM ? QStringLiteral( "true" ) : QStringLiteral( "false" ) );
127 query.addQueryItem( QStringLiteral( "returnZ" ), fetchZ ? QStringLiteral( "true" ) : QStringLiteral( "false" ) );
128 if ( !filterRect.isNull() )
129 {
130 query.addQueryItem( QStringLiteral( "geometry" ), QStringLiteral( "%1,%2,%3,%4" )
131 .arg( filterRect.xMinimum(), 0, 'f', -1 ).arg( filterRect.yMinimum(), 0, 'f', -1 )
132 .arg( filterRect.xMaximum(), 0, 'f', -1 ).arg( filterRect.yMaximum(), 0, 'f', -1 ) );
133 query.addQueryItem( QStringLiteral( "geometryType" ), QStringLiteral( "esriGeometryEnvelope" ) );
134 query.addQueryItem( QStringLiteral( "spatialRel" ), QStringLiteral( "esriSpatialRelEnvelopeIntersects" ) );
135 }
136 queryUrl.setQuery( query );
137 return queryServiceJSON( queryUrl, authcfg, errorTitle, errorText, requestHeaders, feedback, urlPrefix );
138}
139
140QList<quint32> QgsArcGisRestQueryUtils::getObjectIdsByExtent( const QString &layerurl, const QgsRectangle &filterRect, QString &errorTitle, QString &errorText, const QString &authcfg, const QgsHttpHeaders &requestHeaders, QgsFeedback *feedback, const QString &whereClause, const QString &urlPrefix )
141{
142 QUrl queryUrl( layerurl + "/query" );
143 QUrlQuery query( queryUrl );
144 query.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
145 query.addQueryItem( QStringLiteral( "where" ), whereClause.isEmpty() ? QStringLiteral( "1=1" ) : whereClause );
146 query.addQueryItem( QStringLiteral( "returnIdsOnly" ), QStringLiteral( "true" ) );
147 query.addQueryItem( QStringLiteral( "geometry" ), QStringLiteral( "%1,%2,%3,%4" )
148 .arg( filterRect.xMinimum(), 0, 'f', -1 ).arg( filterRect.yMinimum(), 0, 'f', -1 )
149 .arg( filterRect.xMaximum(), 0, 'f', -1 ).arg( filterRect.yMaximum(), 0, 'f', -1 ) );
150 query.addQueryItem( QStringLiteral( "geometryType" ), QStringLiteral( "esriGeometryEnvelope" ) );
151 query.addQueryItem( QStringLiteral( "spatialRel" ), QStringLiteral( "esriSpatialRelEnvelopeIntersects" ) );
152 queryUrl.setQuery( query );
153 const QVariantMap objectIdData = queryServiceJSON( queryUrl, authcfg, errorTitle, errorText, requestHeaders, feedback, urlPrefix );
154
155 if ( objectIdData.isEmpty() )
156 {
157 return QList<quint32>();
158 }
159
160 QList<quint32> ids;
161 const QVariantList objectIdsList = objectIdData[QStringLiteral( "objectIds" )].toList();
162 ids.reserve( objectIdsList.size() );
163 for ( const QVariant &objectId : objectIdsList )
164 {
165 ids << objectId.toInt();
166 }
167 return ids;
168}
169
170QByteArray QgsArcGisRestQueryUtils::queryService( const QUrl &u, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders, QgsFeedback *feedback, QString *contentType, const QString &urlPrefix )
171{
172 QUrl url = parseUrl( u );
173
174 if ( !urlPrefix.isEmpty() )
175 url = QUrl( urlPrefix + url.toString() );
176
177 QNetworkRequest request( url );
178 QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsArcGisRestUtils" ) );
179 requestHeaders.updateNetworkRequest( request );
180
181 QgsBlockingNetworkRequest networkRequest;
182 networkRequest.setAuthCfg( authcfg );
183 const QgsBlockingNetworkRequest::ErrorCode error = networkRequest.get( request, false, feedback );
184
185 if ( feedback && feedback->isCanceled() )
186 return QByteArray();
187
188 // Handle network errors
190 {
191 QgsDebugError( QStringLiteral( "Network error: %1" ).arg( networkRequest.errorMessage() ) );
192 errorTitle = QStringLiteral( "Network error" );
193 errorText = networkRequest.errorMessage();
194
195 // try to get detailed error message from reply
196 const QString content = networkRequest.reply().content();
197 const thread_local QRegularExpression errorRx( QStringLiteral( "Error: <.*?>(.*?)<" ) );
198 const QRegularExpressionMatch match = errorRx.match( content );
199 if ( match.hasMatch() )
200 {
201 errorText = match.captured( 1 );
202 }
203
204 return QByteArray();
205 }
206
207 const QgsNetworkReplyContent content = networkRequest.reply();
208 if ( contentType )
209 *contentType = content.rawHeader( "Content-Type" );
210 return content.content();
211}
212
213QVariantMap QgsArcGisRestQueryUtils::queryServiceJSON( const QUrl &url, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders, QgsFeedback *feedback, const QString &urlPrefix )
214{
215 const QByteArray reply = queryService( url, authcfg, errorTitle, errorText, requestHeaders, feedback, nullptr, urlPrefix );
216 if ( !errorTitle.isEmpty() )
217 {
218 return QVariantMap();
219 }
220 if ( feedback && feedback->isCanceled() )
221 return QVariantMap();
222
223 // Parse data
224 QJsonParseError err;
225 const QJsonDocument doc = QJsonDocument::fromJson( reply, &err );
226 if ( doc.isNull() )
227 {
228 errorTitle = QStringLiteral( "Parsing error" );
229 errorText = err.errorString();
230 QgsDebugError( QStringLiteral( "Parsing error: %1" ).arg( err.errorString() ) );
231 return QVariantMap();
232 }
233 const QVariantMap res = doc.object().toVariantMap();
234 if ( res.contains( QStringLiteral( "error" ) ) )
235 {
236 const QVariantMap error = res.value( QStringLiteral( "error" ) ).toMap();
237 errorText = error.value( QStringLiteral( "message" ) ).toString();
238 errorTitle = QObject::tr( "Error %1" ).arg( error.value( QStringLiteral( "code" ) ).toString() );
239 return QVariantMap();
240 }
241 return res;
242}
243
244QUrl QgsArcGisRestQueryUtils::parseUrl( const QUrl &url, bool *isTestEndpoint )
245{
246 if ( isTestEndpoint )
247 *isTestEndpoint = false;
248
249 QUrl modifiedUrl( url );
250 if ( modifiedUrl.toString().contains( QLatin1String( "fake_qgis_http_endpoint" ) ) )
251 {
252 if ( isTestEndpoint )
253 *isTestEndpoint = true;
254
255 // Just for testing with local files instead of http:// resources
256 QString modifiedUrlString = modifiedUrl.toString();
257 // Qt5 does URL encoding from some reason (of the FILTER parameter for example)
258 modifiedUrlString = QUrl::fromPercentEncoding( modifiedUrlString.toUtf8() );
259 modifiedUrlString.replace( QLatin1String( "fake_qgis_http_endpoint/" ), QLatin1String( "fake_qgis_http_endpoint_" ) );
260 QgsDebugMsgLevel( QStringLiteral( "Get %1" ).arg( modifiedUrlString ), 2 );
261 modifiedUrlString = modifiedUrlString.mid( QStringLiteral( "http://" ).size() );
262 QString args = modifiedUrlString.indexOf( '?' ) >= 0 ? modifiedUrlString.mid( modifiedUrlString.indexOf( '?' ) ) : QString();
263 if ( modifiedUrlString.size() > 150 )
264 {
265 args = QCryptographicHash::hash( args.toUtf8(), QCryptographicHash::Md5 ).toHex();
266 }
267 else
268 {
269 args.replace( QLatin1String( "?" ), QLatin1String( "_" ) );
270 args.replace( QLatin1String( "&" ), QLatin1String( "_" ) );
271 args.replace( QLatin1String( "<" ), QLatin1String( "_" ) );
272 args.replace( QLatin1String( ">" ), QLatin1String( "_" ) );
273 args.replace( QLatin1String( "'" ), QLatin1String( "_" ) );
274 args.replace( QLatin1String( "\"" ), QLatin1String( "_" ) );
275 args.replace( QLatin1String( " " ), QLatin1String( "_" ) );
276 args.replace( QLatin1String( ":" ), QLatin1String( "_" ) );
277 args.replace( QLatin1String( "/" ), QLatin1String( "_" ) );
278 args.replace( QLatin1String( "\n" ), QLatin1String( "_" ) );
279 }
280#ifdef Q_OS_WIN
281 // Passing "urls" like "http://c:/path" to QUrl 'eats' the : after c,
282 // so we must restore it
283 if ( modifiedUrlString[1] == '/' )
284 {
285 modifiedUrlString = modifiedUrlString[0] + ":/" + modifiedUrlString.mid( 2 );
286 }
287#endif
288 modifiedUrlString = modifiedUrlString.mid( 0, modifiedUrlString.indexOf( '?' ) ) + args;
289 QgsDebugMsgLevel( QStringLiteral( "Get %1 (after laundering)" ).arg( modifiedUrlString ), 2 );
290 modifiedUrl = QUrl::fromLocalFile( modifiedUrlString );
291 if ( !QFile::exists( modifiedUrlString ) )
292 {
293 QgsDebugError( QStringLiteral( "Local test file %1 for URL %2 does not exist!!!" ).arg( modifiedUrlString, url.toString() ) );
294 }
295 }
296
297 return modifiedUrl;
298}
299
300void QgsArcGisRestQueryUtils::adjustBaseUrl( QString &baseUrl, const QString &name )
301{
302 const QStringList parts = name.split( '/' );
303 QString checkString;
304 for ( const QString &part : parts )
305 {
306 if ( !checkString.isEmpty() )
307 checkString += QString( '/' );
308
309 checkString += part;
310 if ( baseUrl.indexOf( QRegularExpression( checkString.replace( '/', QLatin1String( "\\/" ) ) + QStringLiteral( "\\/?$" ) ) ) > -1 )
311 {
312 baseUrl = baseUrl.left( baseUrl.length() - checkString.length() - 1 );
313 break;
314 }
315 }
316}
317
318void QgsArcGisRestQueryUtils::visitFolderItems( const std::function< void( const QString &, const QString & ) > &visitor, const QVariantMap &serviceData, const QString &baseUrl )
319{
320 QString base( baseUrl );
321 bool baseChecked = false;
322 if ( !base.endsWith( '/' ) )
323 base += QLatin1Char( '/' );
324
325 const QStringList folderList = serviceData.value( QStringLiteral( "folders" ) ).toStringList();
326 for ( const QString &folder : folderList )
327 {
328 if ( !baseChecked )
329 {
330 adjustBaseUrl( base, folder );
331 baseChecked = true;
332 }
333 visitor( folder, base + folder );
334 }
335}
336
337void QgsArcGisRestQueryUtils::visitServiceItems( const std::function<void ( const QString &, const QString &, Qgis::ArcGisRestServiceType )> &visitor, const QVariantMap &serviceData, const QString &baseUrl )
338{
339 QString base( baseUrl );
340 bool baseChecked = false;
341 if ( !base.endsWith( '/' ) )
342 base += QLatin1Char( '/' );
343
344 const QVariantList serviceList = serviceData.value( QStringLiteral( "services" ) ).toList();
345 for ( const QVariant &service : serviceList )
346 {
347 const QVariantMap serviceMap = service.toMap();
348 const QString serviceTypeString = serviceMap.value( QStringLiteral( "type" ) ).toString();
349 const Qgis::ArcGisRestServiceType serviceType = QgsArcGisRestUtils::serviceTypeFromString( serviceTypeString );
350
351 switch ( serviceType )
352 {
356 // supported
357 break;
358
363 // unsupported
364 continue;
365 }
366
367 const QString serviceName = serviceMap.value( QStringLiteral( "name" ) ).toString();
368 const QString displayName = serviceName.split( '/' ).last();
369 if ( !baseChecked )
370 {
371 adjustBaseUrl( base, serviceName );
372 baseChecked = true;
373 }
374
375 visitor( displayName, base + serviceName + '/' + serviceTypeString, serviceType );
376 }
377}
378
379void QgsArcGisRestQueryUtils::addLayerItems( const std::function<void ( const QString &, ServiceTypeFilter, Qgis::GeometryType, const QString &, const QString &, const QString &, const QString &, bool, const QgsCoordinateReferenceSystem &, const QString & )> &visitor, const QVariantMap &serviceData, const QString &parentUrl, const QString &parentSupportedFormats, const ServiceTypeFilter filter )
380{
381 const QgsCoordinateReferenceSystem crs = QgsArcGisRestUtils::convertSpatialReference( serviceData.value( QStringLiteral( "spatialReference" ) ).toMap() );
382
383 bool found = false;
384 const QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
385 const QStringList supportedImageFormatTypes = serviceData.value( QStringLiteral( "supportedImageFormatTypes" ) ).toString().isEmpty() ? parentSupportedFormats.split( ',' ) : serviceData.value( QStringLiteral( "supportedImageFormatTypes" ) ).toString().split( ',' );
386 QString format = supportedImageFormatTypes.value( 0 );
387 for ( const QString &encoding : supportedImageFormatTypes )
388 {
389 for ( const QByteArray &fmt : supportedFormats )
390 {
391 if ( encoding.startsWith( fmt, Qt::CaseInsensitive ) )
392 {
393 format = encoding;
394 found = true;
395 break;
396 }
397 }
398 if ( found )
399 break;
400 }
401 const QStringList capabilities = serviceData.value( QStringLiteral( "capabilities" ) ).toString().split( ',' );
402
403 // If the requested layer type is vector, do not show raster-only layers (i.e. non query-able layers)
404 const bool serviceMayHaveQueryCapability = capabilities.contains( QStringLiteral( "Query" ) ) ||
405 serviceData.value( QStringLiteral( "serviceDataType" ) ).toString().startsWith( QLatin1String( "esriImageService" ) );
406
407 const bool serviceMayRenderMaps = capabilities.contains( QStringLiteral( "Map" ) ) ||
408 serviceData.value( QStringLiteral( "serviceDataType" ) ).toString().startsWith( QLatin1String( "esriImageService" ) );
409
410 const QVariantList layerInfoList = serviceData.value( QStringLiteral( "layers" ) ).toList();
411 for ( const QVariant &layerInfo : layerInfoList )
412 {
413 const QVariantMap layerInfoMap = layerInfo.toMap();
414 const QString id = layerInfoMap.value( QStringLiteral( "id" ) ).toString();
415 const QString parentLayerId = layerInfoMap.value( QStringLiteral( "parentLayerId" ) ).toString();
416 const QString name = layerInfoMap.value( QStringLiteral( "name" ) ).toString();
417 const QString description = layerInfoMap.value( QStringLiteral( "description" ) ).toString();
418
419 // Yes, potentially we may visit twice, once as as a raster (if applicable), and once as a vector (if applicable)!
420 if ( serviceMayRenderMaps && ( filter == ServiceTypeFilter::Raster || filter == ServiceTypeFilter::AllTypes ) )
421 {
422 if ( !layerInfoMap.value( QStringLiteral( "subLayerIds" ) ).toList().empty() )
423 {
424 visitor( parentLayerId, ServiceTypeFilter::Raster, Qgis::GeometryType::Unknown, id, name, description, parentUrl + '/' + id, true, QgsCoordinateReferenceSystem(), format );
425 }
426 else
427 {
428 visitor( parentLayerId, ServiceTypeFilter::Raster, Qgis::GeometryType::Unknown, id, name, description, parentUrl + '/' + id, false, crs, format );
429 }
430 }
431
432 if ( serviceMayHaveQueryCapability && ( filter == ServiceTypeFilter::Vector || filter == ServiceTypeFilter::AllTypes ) )
433 {
434 const QString geometryType = layerInfoMap.value( QStringLiteral( "geometryType" ) ).toString();
435#if 0
436 // we have a choice here -- if geometryType is unknown and the service reflects that it supports Map capabilities,
437 // then we can't be sure whether or not the individual sublayers support Query or Map requests only. So we either:
438 // 1. Send off additional requests for each individual layer's capabilities (too expensive)
439 // 2. Err on the side of only showing services we KNOW will work for layer -- but this has the side effect that layers
440 // which ARE available as feature services will only show as raster mapserver layers, which is VERY bad/restrictive
441 // 3. Err on the side of showing services we THINK may work, even though some of them may or may not work depending on the actual
442 // server configuration
443 // We opt for 3, because otherwise we're making it impossible for users to load valid vector layers into QGIS
444
445 if ( serviceMayRenderMaps )
446 {
447 if ( geometryType.isEmpty() )
448 continue;
449 }
450#endif
451
452 const Qgis::WkbType wkbType = QgsArcGisRestUtils::convertGeometryType( geometryType );
453
454
455 if ( !layerInfoMap.value( QStringLiteral( "subLayerIds" ) ).toList().empty() )
456 {
457 visitor( parentLayerId, ServiceTypeFilter::Vector, QgsWkbTypes::geometryType( wkbType ), id, name, description, parentUrl + '/' + id, true, QgsCoordinateReferenceSystem(), format );
458 }
459 else
460 {
461 visitor( parentLayerId, ServiceTypeFilter::Vector, QgsWkbTypes::geometryType( wkbType ), id, name, description, parentUrl + '/' + id, false, crs, format );
462 }
463 }
464 }
465
466 const QVariantList tableInfoList = serviceData.value( QStringLiteral( "tables" ) ).toList();
467 for ( const QVariant &tableInfo : tableInfoList )
468 {
469 const QVariantMap tableInfoMap = tableInfo.toMap();
470 const QString id = tableInfoMap.value( QStringLiteral( "id" ) ).toString();
471 const QString parentLayerId = tableInfoMap.value( QStringLiteral( "parentLayerId" ) ).toString();
472 const QString name = tableInfoMap.value( QStringLiteral( "name" ) ).toString();
473 const QString description = tableInfoMap.value( QStringLiteral( "description" ) ).toString();
474
475 if ( serviceMayHaveQueryCapability && ( filter == ServiceTypeFilter::Vector || filter == ServiceTypeFilter::AllTypes ) )
476 {
477 if ( !tableInfoMap.value( QStringLiteral( "subLayerIds" ) ).toList().empty() )
478 {
479 visitor( parentLayerId, ServiceTypeFilter::Vector, Qgis::GeometryType::Null, id, name, description, parentUrl + '/' + id, true, QgsCoordinateReferenceSystem(), format );
480 }
481 else
482 {
483 visitor( parentLayerId, ServiceTypeFilter::Vector, Qgis::GeometryType::Null, id, name, description, parentUrl + '/' + id, false, crs, format );
484 }
485 }
486 }
487
488 // Add root MapServer as raster layer when multiple layers are listed
489 if ( filter != ServiceTypeFilter::Vector && layerInfoList.count() > 1 && serviceData.contains( QStringLiteral( "supportedImageFormatTypes" ) ) )
490 {
491 const QString name = QStringLiteral( "(%1)" ).arg( QObject::tr( "All layers" ) );
492 const QString description = serviceData.value( QStringLiteral( "Comments" ) ).toString();
493 visitor( nullptr, ServiceTypeFilter::Raster, Qgis::GeometryType::Unknown, nullptr, name, description, parentUrl, false, crs, format );
494 }
495
496 // Add root ImageServer as layer
497 if ( serviceData.value( QStringLiteral( "serviceDataType" ) ).toString().startsWith( QLatin1String( "esriImageService" ) ) )
498 {
499 const QString name = serviceData.value( QStringLiteral( "name" ) ).toString();
500 const QString description = serviceData.value( QStringLiteral( "description" ) ).toString();
501 visitor( nullptr, ServiceTypeFilter::Raster, Qgis::GeometryType::Unknown, nullptr, name, description, parentUrl, false, crs, format );
502 }
503}
504
505
507
508//
509// QgsArcGisAsyncQuery
510//
511
512QgsArcGisAsyncQuery::QgsArcGisAsyncQuery( QObject *parent )
513 : QObject( parent )
514{
515}
516
517QgsArcGisAsyncQuery::~QgsArcGisAsyncQuery()
518{
519 if ( mReply )
520 mReply->deleteLater();
521}
522
523void QgsArcGisAsyncQuery::start( const QUrl &url, const QString &authCfg, QByteArray *result, bool allowCache, const QgsHttpHeaders &headers, const QString &urlPrefix )
524{
525 mResult = result;
526 QUrl mUrl = url;
527 if ( !urlPrefix.isEmpty() )
528 mUrl = QUrl( urlPrefix + url.toString() );
529 QNetworkRequest request( mUrl );
530
531 headers.updateNetworkRequest( request );
532
533 if ( !authCfg.isEmpty() && !QgsApplication::authManager()->updateNetworkRequest( request, authCfg ) )
534 {
535 const QString error = tr( "network request update failed for authentication config" );
536 emit failed( QStringLiteral( "Network" ), error );
537 return;
538 }
539
540 QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsArcGisAsyncQuery" ) );
541 request.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy );
542 if ( allowCache )
543 {
544 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
545 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
546 }
547
548 mReply = QgsNetworkAccessManager::instance()->get( request );
549 connect( mReply, &QNetworkReply::finished, this, &QgsArcGisAsyncQuery::handleReply );
550}
551
552void QgsArcGisAsyncQuery::handleReply()
553{
554 mReply->deleteLater();
555 // Handle network errors
556 if ( mReply->error() != QNetworkReply::NoError )
557 {
558 QgsDebugError( QStringLiteral( "Network error: %1" ).arg( mReply->errorString() ) );
559 emit failed( QStringLiteral( "Network error" ), mReply->errorString() );
560 return;
561 }
562
563 // Handle HTTP redirects
564 const QVariant redirect = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
565 if ( !QgsVariantUtils::isNull( redirect ) )
566 {
567 QNetworkRequest request = mReply->request();
568 request.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy );
569 QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsArcGisAsyncQuery" ) );
570 QgsDebugMsgLevel( "redirecting to " + redirect.toUrl().toString(), 2 );
571 request.setUrl( redirect.toUrl() );
572 mReply = QgsNetworkAccessManager::instance()->get( request );
573 connect( mReply, &QNetworkReply::finished, this, &QgsArcGisAsyncQuery::handleReply );
574 return;
575 }
576
577 *mResult = mReply->readAll();
578 mResult = nullptr;
579 emit finished();
580}
581
582//
583// QgsArcGisAsyncParallelQuery
584//
585
586QgsArcGisAsyncParallelQuery::QgsArcGisAsyncParallelQuery( const QString &authcfg, const QgsHttpHeaders &requestHeaders, QObject *parent )
587 : QObject( parent )
588 , mAuthCfg( authcfg )
589 , mRequestHeaders( requestHeaders )
590{
591}
592
593void QgsArcGisAsyncParallelQuery::start( const QVector<QUrl> &urls, QVector<QByteArray> *results, bool allowCache )
594{
595 Q_ASSERT( results->size() == urls.size() );
596 mResults = results;
597 mPendingRequests = mResults->size();
598 for ( int i = 0, n = urls.size(); i < n; ++i )
599 {
600 QNetworkRequest request( urls[i] );
601 QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsArcGisAsyncParallelQuery" ) );
602 QgsSetRequestInitiatorId( request, QString::number( i ) );
603
604 mRequestHeaders.updateNetworkRequest( request );
605 if ( !mAuthCfg.isEmpty() && !QgsApplication::authManager()->updateNetworkRequest( request, mAuthCfg ) )
606 {
607 const QString error = tr( "network request update failed for authentication config" );
608 mErrors.append( error );
609 QgsMessageLog::logMessage( error, tr( "Network" ) );
610 continue;
611 }
612
613 request.setAttribute( QNetworkRequest::HttpPipeliningAllowedAttribute, true );
614 if ( allowCache )
615 {
616 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
617 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
618 request.setRawHeader( "Connection", "keep-alive" );
619 }
620 QNetworkReply *reply = QgsNetworkAccessManager::instance()->get( request );
621 reply->setProperty( "idx", i );
622 connect( reply, &QNetworkReply::finished, this, &QgsArcGisAsyncParallelQuery::handleReply );
623 }
624}
625
626void QgsArcGisAsyncParallelQuery::handleReply()
627{
628 QNetworkReply *reply = qobject_cast<QNetworkReply *>( QObject::sender() );
629 const QVariant redirect = reply->attribute( QNetworkRequest::RedirectionTargetAttribute );
630 const int idx = reply->property( "idx" ).toInt();
631 reply->deleteLater();
632 if ( reply->error() != QNetworkReply::NoError )
633 {
634 // Handle network errors
635 mErrors.append( reply->errorString() );
636 --mPendingRequests;
637 }
638 else if ( !QgsVariantUtils::isNull( redirect ) )
639 {
640 // Handle HTTP redirects
641 QNetworkRequest request = reply->request();
642 request.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy );
643 QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsArcGisAsyncParallelQuery" ) );
644 QgsDebugMsgLevel( "redirecting to " + redirect.toUrl().toString(), 2 );
645 request.setUrl( redirect.toUrl() );
646 reply = QgsNetworkAccessManager::instance()->get( request );
647 reply->setProperty( "idx", idx );
648 connect( reply, &QNetworkReply::finished, this, &QgsArcGisAsyncParallelQuery::handleReply );
649 }
650 else
651 {
652 // All OK
653 ( *mResults )[idx] = reply->readAll();
654 --mPendingRequests;
655 }
656 if ( mPendingRequests == 0 )
657 {
658 emit finished( mErrors );
659 mResults = nullptr;
660 mErrors.clear();
661 }
662}
663
ArcGisRestServiceType
Available ArcGIS REST service types.
Definition qgis.h:4237
@ GeocodeServer
GeocodeServer.
@ Unknown
Other unknown/unsupported type.
@ FeatureServer
FeatureServer.
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:337
@ Unknown
Unknown types.
@ Null
No geometry.
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
static void visitFolderItems(const std::function< void(const QString &folderName, const QString &url)> &visitor, const QVariantMap &serviceData, const QString &baseUrl)
Calls the specified visitor function on all folder items found within the given service data.
static QVariantMap queryServiceJSON(const QUrl &url, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), QgsFeedback *feedback=nullptr, const QString &urlPrefix=QString())
Performs a blocking request to a URL and returns the retrieved JSON content.
static QVariantMap getObjects(const QString &layerurl, const QString &authcfg, const QList< quint32 > &objectIds, const QString &crs, bool fetchGeometry, const QStringList &fetchAttributes, bool fetchM, bool fetchZ, const QgsRectangle &filterRect, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), QgsFeedback *feedback=nullptr, const QString &urlPrefix=QString())
Retrieves all matching objects from the specified layer URL.
static QgsRectangle getExtent(const QString &layerurl, const QString &whereClause, const QString &authcfg, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), const QString &urlPrefix=QString())
Retrieves the extent for the features matching a whereClause.
static QVariantMap getServiceInfo(const QString &baseurl, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), const QString &urlPrefix=QString())
Retrieves JSON service info for the specified base URL.
static QVariantMap getObjectIds(const QString &layerurl, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), const QString &urlPrefix=QString(), const QgsRectangle &bbox=QgsRectangle(), const QString &whereClause=QString())
Retrieves all object IDs for the specified layer URL.
static QUrl parseUrl(const QUrl &url, bool *isTestEndpoint=nullptr)
Parses and processes a url.
static QVariantMap getLayerInfo(const QString &layerurl, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), const QString &urlPrefix=QString())
Retrieves JSON layer info for the specified layer URL.
static void addLayerItems(const std::function< void(const QString &parentLayerId, ServiceTypeFilter serviceType, Qgis::GeometryType geometryType, const QString &layerId, const QString &name, const QString &description, const QString &url, bool isParentLayer, const QgsCoordinateReferenceSystem &crs, const QString &format)> &visitor, const QVariantMap &serviceData, const QString &parentUrl, const QString &parentSupportedFormats, const ServiceTypeFilter filter=ServiceTypeFilter::AllTypes)
Calls the specified visitor function on all layer items found within the given service data.
static void visitServiceItems(const std::function< void(const QString &serviceName, const QString &url, Qgis::ArcGisRestServiceType serviceType)> &visitor, const QVariantMap &serviceData, const QString &baseUrl)
Calls the specified visitor function on all service items found within the given service data.
static QByteArray queryService(const QUrl &url, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), QgsFeedback *feedback=nullptr, QString *contentType=nullptr, const QString &urlPrefix=QString())
Performs a blocking request to a URL and returns the retrieved data.
static QList< quint32 > getObjectIdsByExtent(const QString &layerurl, const QgsRectangle &filterRect, QString &errorTitle, QString &errorText, const QString &authcfg, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), QgsFeedback *feedback=nullptr, const QString &whereClause=QString(), const QString &urlPrefix=QString())
Gets a list of object IDs which fall within the specified extent.
static QgsCoordinateReferenceSystem convertSpatialReference(const QVariantMap &spatialReferenceMap)
Converts a spatial reference JSON definition to a QgsCoordinateReferenceSystem value.
static Qgis::WkbType convertGeometryType(const QString &type)
Converts an ESRI REST geometry type to a WKB type.
static Qgis::ArcGisRestServiceType serviceTypeFromString(const QString &type)
Converts a string value to a REST service type.
static QgsRectangle convertRectangle(const QVariant &value)
Converts a rectangle value to a QgsRectangle.
bool updateNetworkRequest(QNetworkRequest &request, const QString &authcfg, const QString &dataprovider=QString())
Provider call to update a QNetworkRequest with an authentication config.
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
void setAuthCfg(const QString &authCfg)
Sets the authentication config id which should be used during the request.
QString errorMessage() const
Returns the error message string, after a get(), post(), head() or put() request has been made.
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr, RequestFlags requestFlags=QgsBlockingNetworkRequest::RequestFlags())
Performs a "get" operation on the specified request.
@ NoError
No error was encountered.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
Represents a coordinate reference system (CRS).
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
Implements simple HTTP header management.
bool updateNetworkRequest(QNetworkRequest &request) const
Updates a request by adding all the HTTP headers.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
QByteArray content() const
Returns the reply content.
QByteArray rawHeader(const QByteArray &headerName) const
Returns the content of the header with the specified headerName, or an empty QByteArray if the specif...
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:41
#define QgsDebugError(str)
Definition qgslogger.h:40
#define QgsSetRequestInitiatorClass(request, _class)
#define QgsSetRequestInitiatorId(request, str)
const QgsCoordinateReferenceSystem & crs