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