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