29#include <QJsonParseError>
30#include <QJsonDocument>
35QString QgsArcGisVectorTileServiceDataProvider::ARCGIS_VT_SERVICE_DATA_PROVIDER_KEY = QStringLiteral(
"arcgisvectortileservice" );
36QString QgsArcGisVectorTileServiceDataProvider::ARCGIS_VT_SERVICE_DATA_PROVIDER_DESCRIPTION = QObject::tr(
"ArcGIS Vector Tile Service data provider" );
39QgsArcGisVectorTileServiceDataProvider::QgsArcGisVectorTileServiceDataProvider(
const QString &uri,
const ProviderOptions &providerOptions,
Qgis::DataProviderReadFlags flags )
40 : QgsXyzVectorTileDataProviderBase( uri, providerOptions, flags )
42 mIsValid = setupArcgisVectorTileServiceConnection();
48 mLayerMetadata.setIdentifier( mArcgisLayerConfiguration.value( QStringLiteral(
"serviceUri" ) ).toString() );
49 const QString parentIdentifier = mArcgisLayerConfiguration.value( QStringLiteral(
"serviceItemId" ) ).toString();
50 if ( !parentIdentifier.isEmpty() )
52 mLayerMetadata.setParentIdentifier( parentIdentifier );
54 mLayerMetadata.setType( QStringLiteral(
"dataset" ) );
55 mLayerMetadata.setTitle( mArcgisLayerConfiguration.value( QStringLiteral(
"name" ) ).toString() );
56 const QString copyright = mArcgisLayerConfiguration.value( QStringLiteral(
"copyrightText" ) ).toString();
57 if ( !copyright.isEmpty() )
58 mLayerMetadata.setRights( QStringList() << copyright );
59 mLayerMetadata.addLink(
QgsAbstractMetadataBase::Link( tr(
"Source" ), QStringLiteral(
"WWW:LINK" ), mArcgisLayerConfiguration.value( QStringLiteral(
"serviceUri" ) ).toString() ) );
62QgsArcGisVectorTileServiceDataProvider::QgsArcGisVectorTileServiceDataProvider(
const QgsArcGisVectorTileServiceDataProvider &other )
63 : QgsXyzVectorTileDataProviderBase( other )
65 mIsValid = other.mIsValid;
66 mExtent = other.mExtent;
67 mMatrixSet = other.mMatrixSet;
68 mSourcePath = other.mSourcePath;
69 mArcgisLayerConfiguration = other.mArcgisLayerConfiguration;
70 mArcgisStyleConfiguration = other.mArcgisStyleConfiguration;
72 mLayerMetadata = other.mLayerMetadata;
90QString QgsArcGisVectorTileServiceDataProvider::name()
const
94 return ARCGIS_VT_SERVICE_DATA_PROVIDER_KEY;
97QString QgsArcGisVectorTileServiceDataProvider::description()
const
101 return ARCGIS_VT_SERVICE_DATA_PROVIDER_DESCRIPTION;
108 return new QgsArcGisVectorTileServiceDataProvider( *
this );
111QString QgsArcGisVectorTileServiceDataProvider::sourcePath()
const
118bool QgsArcGisVectorTileServiceDataProvider::isValid()
const
125QgsRectangle QgsArcGisVectorTileServiceDataProvider::extent()
const
146QgsLayerMetadata QgsArcGisVectorTileServiceDataProvider::layerMetadata()
const
150 return mLayerMetadata;
153QVariantMap QgsArcGisVectorTileServiceDataProvider::styleDefinition()
const
157 return mArcgisStyleConfiguration;
160QString QgsArcGisVectorTileServiceDataProvider::styleUrl()
const
165 return mArcgisLayerConfiguration.value( QStringLiteral(
"serviceUri" ) ).toString()
166 +
'/' + mArcgisLayerConfiguration.value( QStringLiteral(
"defaultStyles" ) ).toString();
169QString QgsArcGisVectorTileServiceDataProvider::htmlMetadata()
const
173 if ( !mTileMapUrl.isEmpty() )
174 metadata += QStringLiteral(
"<tr><td class=\"highlight\">" ) % tr(
"Tilemap" ) % QStringLiteral(
"</td><td><a href=\"%1\">%1</a>" ).arg( mTileMapUrl ) % QStringLiteral(
"</td></tr>\n" );
179bool QgsArcGisVectorTileServiceDataProvider::setupArcgisVectorTileServiceConnection()
185 QString tileServiceUri = dsUri.param( QStringLiteral(
"url" ) );
187 QUrl url( tileServiceUri );
191 query.addQueryItem( QStringLiteral(
"f" ), QStringLiteral(
"pjson" ) );
192 url.setQuery( query );
194 QNetworkRequest request = QNetworkRequest( url );
199 switch ( networkRequest.get( request ) )
211 const QByteArray raw = content.
content();
215 const QJsonDocument doc = QJsonDocument::fromJson( raw, &err );
221 mArcgisLayerConfiguration = doc.object().toVariantMap();
222 if ( mArcgisLayerConfiguration.contains( QStringLiteral(
"error" ) ) )
227 if ( !mArcgisLayerConfiguration.value( QStringLiteral(
"tiles" ) ).isValid() )
230 const QString sourceUri = mArcgisLayerConfiguration.value( QStringLiteral(
"sources" ) ).toMap().value( QStringLiteral(
"esri" ) ).toMap().value( QStringLiteral(
"url" ) ).toString();
231 if ( !sourceUri.isEmpty() )
233 QUrl url( sourceUri );
237 query.addQueryItem( QStringLiteral(
"f" ), QStringLiteral(
"pjson" ) );
238 url.setQuery( query );
240 QNetworkRequest request = QNetworkRequest( url );
245 switch ( networkRequest.get( request ) )
257 const QByteArray raw = content.
content();
261 const QJsonDocument doc = QJsonDocument::fromJson( raw, &err );
267 tileServiceUri = sourceUri;
270 mArcgisStyleConfiguration = mArcgisLayerConfiguration;
271 mArcgisLayerConfiguration = doc.object().toVariantMap();
272 if ( mArcgisLayerConfiguration.contains( QStringLiteral(
"error" ) ) )
281 const QString tileMapEndpoint = mArcgisLayerConfiguration.value( QStringLiteral(
"tileMap" ) ).toString();
282 if ( !tileMapEndpoint.isEmpty() )
284 mTileMapUrl = tileServiceUri +
'/' + tileMapEndpoint;
285 QUrl tilemapUrl( mTileMapUrl );
286 tilemapUrl.setQuery( query );
288 QNetworkRequest tileMapRequest = QNetworkRequest( tilemapUrl );
292 switch ( tileMapNetworkRequest.get( tileMapRequest ) )
304 const QByteArray tileMapRaw = tileMapContent.
content();
306 const QJsonDocument tileMapDoc = QJsonDocument::fromJson( tileMapRaw, &err );
307 if ( !tileMapDoc.isNull() )
309 tileMap = tileMapDoc.object().toVariantMap();
313 mSourcePath = tileServiceUri +
'/' + mArcgisLayerConfiguration.value( QStringLiteral(
"tiles" ) ).toList().value( 0 ).toString();
316 QgsDebugError( QStringLiteral(
"Invalid format of URL for XYZ source: " ) + tileServiceUri );
320 mArcgisLayerConfiguration.insert( QStringLiteral(
"serviceUri" ), tileServiceUri );
322 mMatrixSet.fromEsriJson( mArcgisLayerConfiguration, tileMap );
323 mCrs = mMatrixSet.crs();
326 if ( dsUri.hasParam( QStringLiteral(
"zmin" ) ) )
327 mMatrixSet.dropMatricesOutsideZoomRange( dsUri.param( QStringLiteral(
"zmin" ) ).toInt(), 99 );
329 if ( dsUri.hasParam( QStringLiteral(
"zmax" ) ) )
330 mMatrixSet.dropMatricesOutsideZoomRange( 0, dsUri.param( QStringLiteral(
"zmax" ) ).toInt() );
332 const QVariantMap fullExtent = mArcgisLayerConfiguration.value( QStringLiteral(
"fullExtent" ) ).toMap();
333 if ( !fullExtent.isEmpty() )
336 fullExtent.value( QStringLiteral(
"xmin" ) ).toDouble(),
337 fullExtent.value( QStringLiteral(
"ymin" ) ).toDouble(),
338 fullExtent.value( QStringLiteral(
"xmax" ) ).toDouble(),
339 fullExtent.value( QStringLiteral(
"ymax" ) ).toDouble()
346 mExtent = extentTransform.transformBoundingBox( fullExtentRect );
350 QgsDebugError( QStringLiteral(
"Could not transform layer fullExtent to layer CRS" ) );
359 mExtent = extentTransform.transformBoundingBox(
QgsRectangle( -20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892 ) );
363 QgsDebugError( QStringLiteral(
"Could not transform layer extent to layer CRS" ) );
375QgsArcGisVectorTileServiceDataProviderMetadata::QgsArcGisVectorTileServiceDataProviderMetadata()
376 :
QgsProviderMetadata( QgsArcGisVectorTileServiceDataProvider::ARCGIS_VT_SERVICE_DATA_PROVIDER_KEY,
377 QgsArcGisVectorTileServiceDataProvider::ARCGIS_VT_SERVICE_DATA_PROVIDER_DESCRIPTION )
381QIcon QgsArcGisVectorTileServiceDataProviderMetadata::icon()
const
393 return new QgsArcGisVectorTileServiceDataProvider( uri, options, flags );
396QVariantMap QgsArcGisVectorTileServiceDataProviderMetadata::decodeUri(
const QString &uri )
const
401 QVariantMap uriComponents;
402 uriComponents.insert( QStringLiteral(
"type" ), QStringLiteral(
"xyz" ) );
403 uriComponents.insert( QStringLiteral(
"serviceType" ), QStringLiteral(
"arcgis" ) );
404 uriComponents.insert( QStringLiteral(
"url" ), dsUri.
param( QStringLiteral(
"url" ) ) );
406 if ( dsUri.
hasParam( QStringLiteral(
"zmin" ) ) )
407 uriComponents.insert( QStringLiteral(
"zmin" ), dsUri.
param( QStringLiteral(
"zmin" ) ) );
408 if ( dsUri.
hasParam( QStringLiteral(
"zmax" ) ) )
409 uriComponents.insert( QStringLiteral(
"zmax" ), dsUri.
param( QStringLiteral(
"zmax" ) ) );
413 if ( dsUri.
hasParam( QStringLiteral(
"styleUrl" ) ) )
414 uriComponents.insert( QStringLiteral(
"styleUrl" ), dsUri.
param( QStringLiteral(
"styleUrl" ) ) );
417 if ( !authcfg.isEmpty() )
418 uriComponents.insert( QStringLiteral(
"authcfg" ), authcfg );
420 return uriComponents;
423QString QgsArcGisVectorTileServiceDataProviderMetadata::encodeUri(
const QVariantMap &parts )
const
426 dsUri.
setParam( QStringLiteral(
"type" ), QStringLiteral(
"xyz" ) );
427 dsUri.
setParam( QStringLiteral(
"serviceType" ), QStringLiteral(
"arcgis" ) );
428 dsUri.
setParam( QStringLiteral(
"url" ), parts.value( QStringLiteral(
"url" ) ).toString() );
430 if ( parts.contains( QStringLiteral(
"zmin" ) ) )
431 dsUri.
setParam( QStringLiteral(
"zmin" ), parts[ QStringLiteral(
"zmin" ) ].toString() );
432 if ( parts.contains( QStringLiteral(
"zmax" ) ) )
433 dsUri.
setParam( QStringLiteral(
"zmax" ), parts[ QStringLiteral(
"zmax" ) ].toString() );
437 if ( parts.contains( QStringLiteral(
"styleUrl" ) ) )
438 dsUri.
setParam( QStringLiteral(
"styleUrl" ), parts[ QStringLiteral(
"styleUrl" ) ].toString() );
440 if ( parts.contains( QStringLiteral(
"authcfg" ) ) )
441 dsUri.
setAuthConfigId( parts[ QStringLiteral(
"authcfg" ) ].toString() );
446QString QgsArcGisVectorTileServiceDataProviderMetadata::absoluteToRelativeUri(
const QString &uri,
const QgsReadWriteContext & )
const
453QString QgsArcGisVectorTileServiceDataProviderMetadata::relativeToAbsoluteUri(
const QString &uri,
const QgsReadWriteContext & )
const
460QList<Qgis::LayerType> QgsArcGisVectorTileServiceDataProviderMetadata::supportedLayerTypes()
const
@ ReadLayerMetadata
Provider can read layer metadata from data store. See QgsDataProvider::layerMetadata()
@ AlwaysUseTileMatrixSetFromProvider
Vector tile layer must always use the tile matrix set from the data provider, and should never store,...
QFlags< DataProviderFlag > DataProviderFlags
Data provider flags.
@ FastExtent2D
Provider's 2D extent retrieval via QgsDataProvider::extent() is always guaranteed to be trivial/fast ...
QFlags< DataProviderReadFlag > DataProviderReadFlags
Flags which control data provider construction.
QFlags< VectorTileProviderCapability > VectorTileProviderCapabilities
Vector tile data provider capabilities.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
QFlags< VectorTileProviderFlag > VectorTileProviderFlags
Vector tile data provider flags.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsCoordinateReferenceSystem convertSpatialReference(const QVariantMap &spatialReferenceMap)
Converts a spatial reference JSON definition to a QgsCoordinateReferenceSystem value.
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
@ NetworkError
A network error occurred.
@ ServerExceptionError
An exception was raised by the server.
@ NoError
No error was encountered.
@ TimeoutError
Timeout was reached before a reply was received.
This class represents a coordinate reference system (CRS).
Custom exception class for Coordinate Reference System related exceptions.
Class for storing the component parts of a RDBMS data source URI (e.g.
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
bool hasParam(const QString &key) const
Returns true if a parameter with the specified key exists.
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
void setAuthConfigId(const QString &authcfg)
Sets the authentication configuration ID for the URI.
QgsHttpHeaders httpHeaders() const
Returns http headers.
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
QByteArray content() const
Returns the reply content.
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
Base class for vector tile layer data providers.
Encapsulates properties of a vector tile matrix set, including tile origins and scaling information.
static bool checkXYZUrlTemplate(const QString &url)
Checks whether the URL template string is correct (contains {x}, {y} / {-y}, {z} placeholders)
#define QgsDebugError(str)
#define QgsSetRequestInitiatorClass(request, _class)
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
Setting options for creating vector data providers.