17#include "moc_qgsarcgisvectortileservicedataprovider.cpp"
30#include <QJsonParseError>
31#include <QJsonDocument>
36QString QgsArcGisVectorTileServiceDataProvider::ARCGIS_VT_SERVICE_DATA_PROVIDER_KEY = QStringLiteral(
"arcgisvectortileservice" );
37QString QgsArcGisVectorTileServiceDataProvider::ARCGIS_VT_SERVICE_DATA_PROVIDER_DESCRIPTION = QObject::tr(
"ArcGIS Vector Tile Service data provider" );
40QgsArcGisVectorTileServiceDataProvider::QgsArcGisVectorTileServiceDataProvider(
const QString &uri,
const ProviderOptions &providerOptions,
Qgis::DataProviderReadFlags flags )
41 : QgsXyzVectorTileDataProviderBase( uri, providerOptions, flags )
43 mIsValid = setupArcgisVectorTileServiceConnection();
49 mLayerMetadata.setIdentifier( mArcgisLayerConfiguration.value( QStringLiteral(
"serviceUri" ) ).toString() );
50 const QString parentIdentifier = mArcgisLayerConfiguration.value( QStringLiteral(
"serviceItemId" ) ).toString();
51 if ( !parentIdentifier.isEmpty() )
53 mLayerMetadata.setParentIdentifier( parentIdentifier );
55 mLayerMetadata.setType( QStringLiteral(
"dataset" ) );
56 mLayerMetadata.setTitle( mArcgisLayerConfiguration.value( QStringLiteral(
"name" ) ).toString() );
57 const QString copyright = mArcgisLayerConfiguration.value( QStringLiteral(
"copyrightText" ) ).toString();
58 if ( !copyright.isEmpty() )
59 mLayerMetadata.setRights( QStringList() << copyright );
60 mLayerMetadata.addLink(
QgsAbstractMetadataBase::Link( tr(
"Source" ), QStringLiteral(
"WWW:LINK" ), mArcgisLayerConfiguration.value( QStringLiteral(
"serviceUri" ) ).toString() ) );
63QgsArcGisVectorTileServiceDataProvider::QgsArcGisVectorTileServiceDataProvider(
const QgsArcGisVectorTileServiceDataProvider &other )
64 : QgsXyzVectorTileDataProviderBase( other )
66 mIsValid = other.mIsValid;
67 mExtent = other.mExtent;
68 mMatrixSet = other.mMatrixSet;
69 mSourcePath = other.mSourcePath;
70 mArcgisLayerConfiguration = other.mArcgisLayerConfiguration;
71 mArcgisStyleConfiguration = other.mArcgisStyleConfiguration;
73 mLayerMetadata = other.mLayerMetadata;
91QString QgsArcGisVectorTileServiceDataProvider::name()
const
95 return ARCGIS_VT_SERVICE_DATA_PROVIDER_KEY;
98QString QgsArcGisVectorTileServiceDataProvider::description()
const
102 return ARCGIS_VT_SERVICE_DATA_PROVIDER_DESCRIPTION;
109 return new QgsArcGisVectorTileServiceDataProvider( *
this );
112QString QgsArcGisVectorTileServiceDataProvider::sourcePath()
const
119bool QgsArcGisVectorTileServiceDataProvider::isValid()
const
126QgsRectangle QgsArcGisVectorTileServiceDataProvider::extent()
const
147QgsLayerMetadata QgsArcGisVectorTileServiceDataProvider::layerMetadata()
const
151 return mLayerMetadata;
154QVariantMap QgsArcGisVectorTileServiceDataProvider::styleDefinition()
const
158 return mArcgisStyleConfiguration;
161QString QgsArcGisVectorTileServiceDataProvider::styleUrl()
const
166 return mArcgisLayerConfiguration.value( QStringLiteral(
"serviceUri" ) ).toString()
167 +
'/' + mArcgisLayerConfiguration.value( QStringLiteral(
"defaultStyles" ) ).toString();
170QString QgsArcGisVectorTileServiceDataProvider::htmlMetadata()
const
174 if ( !mTileMapUrl.isEmpty() )
175 metadata += QStringLiteral(
"<tr><td class=\"highlight\">" ) % tr(
"Tilemap" ) % QStringLiteral(
"</td><td><a href=\"%1\">%1</a>" ).arg( mTileMapUrl ) % QStringLiteral(
"</td></tr>\n" );
180bool QgsArcGisVectorTileServiceDataProvider::setupArcgisVectorTileServiceConnection()
186 QString tileServiceUri = dsUri.param( QStringLiteral(
"url" ) );
188 QUrl url( tileServiceUri );
192 query.addQueryItem( QStringLiteral(
"f" ), QStringLiteral(
"pjson" ) );
193 url.setQuery( query );
195 QNetworkRequest request = QNetworkRequest( url );
200 switch ( networkRequest.get( request ) )
212 const QByteArray raw = content.
content();
216 const QJsonDocument doc = QJsonDocument::fromJson( raw, &err );
222 mArcgisLayerConfiguration = doc.object().toVariantMap();
223 if ( mArcgisLayerConfiguration.contains( QStringLiteral(
"error" ) ) )
228 if ( !mArcgisLayerConfiguration.value( QStringLiteral(
"tiles" ) ).isValid() )
231 const QString sourceUri = mArcgisLayerConfiguration.value( QStringLiteral(
"sources" ) ).toMap().value( QStringLiteral(
"esri" ) ).toMap().value( QStringLiteral(
"url" ) ).toString();
232 if ( !sourceUri.isEmpty() )
234 QUrl url( sourceUri );
238 query.addQueryItem( QStringLiteral(
"f" ), QStringLiteral(
"pjson" ) );
239 url.setQuery( query );
241 QNetworkRequest request = QNetworkRequest( url );
246 switch ( networkRequest.get( request ) )
258 const QByteArray raw = content.
content();
262 const QJsonDocument doc = QJsonDocument::fromJson( raw, &err );
268 tileServiceUri = sourceUri;
271 mArcgisStyleConfiguration = mArcgisLayerConfiguration;
272 mArcgisLayerConfiguration = doc.object().toVariantMap();
273 if ( mArcgisLayerConfiguration.contains( QStringLiteral(
"error" ) ) )
282 const QString tileMapEndpoint = mArcgisLayerConfiguration.value( QStringLiteral(
"tileMap" ) ).toString();
283 if ( !tileMapEndpoint.isEmpty() )
285 mTileMapUrl = tileServiceUri +
'/' + tileMapEndpoint;
286 QUrl tilemapUrl( mTileMapUrl );
287 tilemapUrl.setQuery( query );
289 QNetworkRequest tileMapRequest = QNetworkRequest( tilemapUrl );
293 switch ( tileMapNetworkRequest.get( tileMapRequest ) )
305 const QByteArray tileMapRaw = tileMapContent.
content();
307 const QJsonDocument tileMapDoc = QJsonDocument::fromJson( tileMapRaw, &err );
308 if ( !tileMapDoc.isNull() )
310 tileMap = tileMapDoc.object().toVariantMap();
314 mSourcePath = tileServiceUri +
'/' + mArcgisLayerConfiguration.value( QStringLiteral(
"tiles" ) ).toList().value( 0 ).toString();
317 QgsDebugError( QStringLiteral(
"Invalid format of URL for XYZ source: " ) + tileServiceUri );
321 mArcgisLayerConfiguration.insert( QStringLiteral(
"serviceUri" ), tileServiceUri );
323 mMatrixSet.fromEsriJson( mArcgisLayerConfiguration, tileMap );
324 mCrs = mMatrixSet.crs();
327 if ( dsUri.hasParam( QStringLiteral(
"zmin" ) ) )
328 mMatrixSet.dropMatricesOutsideZoomRange( dsUri.param( QStringLiteral(
"zmin" ) ).toInt(), 99 );
330 if ( dsUri.hasParam( QStringLiteral(
"zmax" ) ) )
331 mMatrixSet.dropMatricesOutsideZoomRange( 0, dsUri.param( QStringLiteral(
"zmax" ) ).toInt() );
333 const QVariantMap fullExtent = mArcgisLayerConfiguration.value( QStringLiteral(
"fullExtent" ) ).toMap();
334 if ( !fullExtent.isEmpty() )
337 fullExtent.value( QStringLiteral(
"xmin" ) ).toDouble(),
338 fullExtent.value( QStringLiteral(
"ymin" ) ).toDouble(),
339 fullExtent.value( QStringLiteral(
"xmax" ) ).toDouble(),
340 fullExtent.value( QStringLiteral(
"ymax" ) ).toDouble()
347 mExtent = extentTransform.transformBoundingBox( fullExtentRect );
351 QgsDebugError( QStringLiteral(
"Could not transform layer fullExtent to layer CRS" ) );
360 mExtent = extentTransform.transformBoundingBox(
QgsRectangle( -20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892 ) );
364 QgsDebugError( QStringLiteral(
"Could not transform layer extent to layer CRS" ) );
376QgsArcGisVectorTileServiceDataProviderMetadata::QgsArcGisVectorTileServiceDataProviderMetadata()
377 :
QgsProviderMetadata( QgsArcGisVectorTileServiceDataProvider::ARCGIS_VT_SERVICE_DATA_PROVIDER_KEY,
378 QgsArcGisVectorTileServiceDataProvider::ARCGIS_VT_SERVICE_DATA_PROVIDER_DESCRIPTION )
382QIcon QgsArcGisVectorTileServiceDataProviderMetadata::icon()
const
394 return new QgsArcGisVectorTileServiceDataProvider( uri, options, flags );
397QVariantMap QgsArcGisVectorTileServiceDataProviderMetadata::decodeUri(
const QString &uri )
const
402 QVariantMap uriComponents;
403 uriComponents.insert( QStringLiteral(
"type" ), QStringLiteral(
"xyz" ) );
404 uriComponents.insert( QStringLiteral(
"serviceType" ), QStringLiteral(
"arcgis" ) );
405 uriComponents.insert( QStringLiteral(
"url" ), dsUri.
param( QStringLiteral(
"url" ) ) );
407 if ( dsUri.
hasParam( QStringLiteral(
"zmin" ) ) )
408 uriComponents.insert( QStringLiteral(
"zmin" ), dsUri.
param( QStringLiteral(
"zmin" ) ) );
409 if ( dsUri.
hasParam( QStringLiteral(
"zmax" ) ) )
410 uriComponents.insert( QStringLiteral(
"zmax" ), dsUri.
param( QStringLiteral(
"zmax" ) ) );
414 if ( dsUri.
hasParam( QStringLiteral(
"styleUrl" ) ) )
415 uriComponents.insert( QStringLiteral(
"styleUrl" ), dsUri.
param( QStringLiteral(
"styleUrl" ) ) );
418 if ( !authcfg.isEmpty() )
419 uriComponents.insert( QStringLiteral(
"authcfg" ), authcfg );
421 return uriComponents;
424QString QgsArcGisVectorTileServiceDataProviderMetadata::encodeUri(
const QVariantMap &parts )
const
427 dsUri.
setParam( QStringLiteral(
"type" ), QStringLiteral(
"xyz" ) );
428 dsUri.
setParam( QStringLiteral(
"serviceType" ), QStringLiteral(
"arcgis" ) );
429 dsUri.
setParam( QStringLiteral(
"url" ), parts.value( QStringLiteral(
"url" ) ).toString() );
431 if ( parts.contains( QStringLiteral(
"zmin" ) ) )
432 dsUri.
setParam( QStringLiteral(
"zmin" ), parts[ QStringLiteral(
"zmin" ) ].toString() );
433 if ( parts.contains( QStringLiteral(
"zmax" ) ) )
434 dsUri.
setParam( QStringLiteral(
"zmax" ), parts[ QStringLiteral(
"zmax" ) ].toString() );
438 if ( parts.contains( QStringLiteral(
"styleUrl" ) ) )
439 dsUri.
setParam( QStringLiteral(
"styleUrl" ), parts[ QStringLiteral(
"styleUrl" ) ].toString() );
441 if ( parts.contains( QStringLiteral(
"authcfg" ) ) )
442 dsUri.
setAuthConfigId( parts[ QStringLiteral(
"authcfg" ) ].toString() );
447QString QgsArcGisVectorTileServiceDataProviderMetadata::absoluteToRelativeUri(
const QString &uri,
const QgsReadWriteContext & )
const
454QString QgsArcGisVectorTileServiceDataProviderMetadata::relativeToAbsoluteUri(
const QString &uri,
const QgsReadWriteContext & )
const
461QList<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.