Skip to content

Commit e3fda27

Browse files
committedMar 20, 2023
Move source path handling to vector tile provider subclasses
1 parent 097b2ca commit e3fda27

File tree

2 files changed

+98
-41
lines changed

2 files changed

+98
-41
lines changed
 

‎src/core/vectortile/qgsvectortilelayer.cpp

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,17 @@ bool QgsVectorTileLayer::loadDataSource()
8585
const QgsDataProvider::ReadFlags flags;
8686

8787
mSourceType = dsUri.param( QStringLiteral( "type" ) );
88-
mSourcePath = dsUri.param( QStringLiteral( "url" ) );
88+
const QString sourcePath = dsUri.param( QStringLiteral( "url" ) );
8989
if ( mSourceType == QLatin1String( "xyz" ) && dsUri.param( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
9090
{
91-
if ( !setupArcgisVectorTileServiceConnection( mSourcePath, dsUri ) )
91+
if ( !setupArcgisVectorTileServiceConnection( sourcePath, dsUri ) )
9292
return false;
93-
94-
mDataProvider = std::make_unique< QgsArcGisVectorTileServiceDataProvider >( providerOptions, flags );
9593
}
9694
else if ( mSourceType == QLatin1String( "xyz" ) )
9795
{
98-
if ( !QgsVectorTileUtils::checkXYZUrlTemplate( mSourcePath ) )
96+
if ( !QgsVectorTileUtils::checkXYZUrlTemplate( sourcePath ) )
9997
{
100-
QgsDebugMsg( QStringLiteral( "Invalid format of URL for XYZ source: " ) + mSourcePath );
98+
QgsDebugMsg( QStringLiteral( "Invalid format of URL for XYZ source: " ) + sourcePath );
10199
return false;
102100
}
103101

@@ -113,14 +111,14 @@ bool QgsVectorTileLayer::loadDataSource()
113111
mMatrixSet = QgsVectorTileMatrixSet::fromWebMercator( zMin, zMax );
114112
setExtent( QgsRectangle( -20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892 ) );
115113

116-
mDataProvider = std::make_unique< QgsXyzVectorTileDataProvider >( providerOptions, flags );
114+
mDataProvider = std::make_unique< QgsXyzVectorTileDataProvider >( mDataSource, providerOptions, flags );
117115
}
118116
else if ( mSourceType == QLatin1String( "mbtiles" ) )
119117
{
120-
QgsMbTiles reader( mSourcePath );
118+
QgsMbTiles reader( sourcePath );
121119
if ( !reader.open() )
122120
{
123-
QgsDebugMsg( QStringLiteral( "failed to open MBTiles file: " ) + mSourcePath );
121+
QgsDebugMsg( QStringLiteral( "failed to open MBTiles file: " ) + sourcePath );
124122
return false;
125123
}
126124

@@ -148,14 +146,14 @@ bool QgsVectorTileLayer::loadDataSource()
148146
r = ct.transformBoundingBox( r );
149147
setExtent( r );
150148

151-
mDataProvider = std::make_unique< QgsMbTilesVectorTileDataProvider >( providerOptions, flags );
149+
mDataProvider = std::make_unique< QgsMbTilesVectorTileDataProvider >( mDataSource, providerOptions, flags );
152150
}
153151
else if ( mSourceType == QLatin1String( "vtpk" ) )
154152
{
155-
QgsVtpkTiles reader( mSourcePath );
153+
QgsVtpkTiles reader( sourcePath );
156154
if ( !reader.open() )
157155
{
158-
QgsDebugMsg( QStringLiteral( "failed to open VTPK file: " ) + mSourcePath );
156+
QgsDebugMsg( QStringLiteral( "failed to open VTPK file: " ) + sourcePath );
159157
return false;
160158
}
161159

@@ -171,7 +169,7 @@ bool QgsVectorTileLayer::loadDataSource()
171169
setCrs( mMatrixSet.crs() );
172170
setExtent( reader.extent( transformContext() ) );
173171

174-
mDataProvider = std::make_unique< QgsVtpkVectorTileDataProvider >( providerOptions, flags );
172+
mDataProvider = std::make_unique< QgsVtpkVectorTileDataProvider >( mDataSource, providerOptions, flags );
175173
}
176174
else
177175
{
@@ -281,10 +279,10 @@ bool QgsVectorTileLayer::setupArcgisVectorTileServiceConnection( const QString &
281279
}
282280
}
283281

284-
mSourcePath = tileServiceUri + '/' + mArcgisLayerConfiguration.value( QStringLiteral( "tiles" ) ).toList().value( 0 ).toString();
285-
if ( !QgsVectorTileUtils::checkXYZUrlTemplate( mSourcePath ) )
282+
const QString sourcePath = tileServiceUri + '/' + mArcgisLayerConfiguration.value( QStringLiteral( "tiles" ) ).toList().value( 0 ).toString();
283+
if ( !QgsVectorTileUtils::checkXYZUrlTemplate( sourcePath ) )
286284
{
287-
QgsDebugMsg( QStringLiteral( "Invalid format of URL for XYZ source: " ) + mSourcePath );
285+
QgsDebugMsg( QStringLiteral( "Invalid format of URL for XYZ source: " ) + sourcePath );
288286
return false;
289287
}
290288

@@ -336,6 +334,10 @@ bool QgsVectorTileLayer::setupArcgisVectorTileServiceConnection( const QString &
336334
}
337335
}
338336

337+
const QgsDataProvider::ProviderOptions providerOptions { mTransformContext };
338+
const QgsDataProvider::ReadFlags flags;
339+
mDataProvider = std::make_unique< QgsArcGisVectorTileServiceDataProvider >( mDataSource, sourcePath, providerOptions, flags );
340+
339341
return true;
340342
}
341343

@@ -622,10 +624,10 @@ bool QgsVectorTileLayer::loadDefaultStyleAndSubLayersPrivate( QString &error, QS
622624

623625
if ( mSourceType == QLatin1String( "vtpk" ) )
624626
{
625-
QgsVtpkTiles reader( mSourcePath );
627+
QgsVtpkTiles reader( sourcePath() );
626628
if ( !reader.open() )
627629
{
628-
QgsDebugMsg( QStringLiteral( "failed to open VTPK file: " ) + mSourcePath );
630+
QgsDebugMsg( QStringLiteral( "failed to open VTPK file: " ) + sourcePath() );
629631
return false;
630632
}
631633

@@ -796,10 +798,10 @@ QString QgsVectorTileLayer::loadDefaultMetadata( bool &resultFlag )
796798
}
797799
else if ( mSourceType == QLatin1String( "vtpk" ) )
798800
{
799-
QgsVtpkTiles reader( mSourcePath );
801+
QgsVtpkTiles reader( sourcePath() );
800802
if ( !reader.open() )
801803
{
802-
QgsDebugMsg( QStringLiteral( "failed to open VTPK file: " ) + mSourcePath );
804+
QgsDebugMsg( QStringLiteral( "failed to open VTPK file: " ) + sourcePath() );
803805
resultFlag = false;
804806
}
805807
else
@@ -891,9 +893,20 @@ QString QgsVectorTileLayer::htmlMetadata() const
891893
return info;
892894
}
893895

896+
QString QgsVectorTileLayer::sourcePath() const
897+
{
898+
if ( QgsVectorTileDataProvider *vtProvider = qobject_cast< QgsVectorTileDataProvider * >( mDataProvider.get() ) )
899+
return vtProvider->sourcePath();
900+
901+
return QString();
902+
}
903+
894904
QByteArray QgsVectorTileLayer::getRawTile( QgsTileXYZ tileID )
895905
{
896906
QGIS_PROTECT_QOBJECT_THREAD_ACCESS
907+
QgsVectorTileDataProvider *vtProvider = qobject_cast< QgsVectorTileDataProvider * >( mDataProvider.get() );
908+
if ( !vtProvider )
909+
return QByteArray();
897910

898911
const QgsTileMatrix tileMatrix = mMatrixSet.tileMatrix( tileID.zoomLevel() );
899912
const QgsTileRange tileRange( tileID.column(), tileID.column(), tileID.row(), tileID.row() );
@@ -902,7 +915,7 @@ QByteArray QgsVectorTileLayer::getRawTile( QgsTileXYZ tileID )
902915
dsUri.setEncodedUri( mDataSource );
903916
const QString authcfg = dsUri.authConfigId();
904917

905-
QList<QgsVectorTileRawData> rawTiles = QgsVectorTileLoader::blockingFetchTileRawData( mSourceType, mSourcePath, tileMatrix, QPointF(), tileRange, authcfg, dsUri.httpHeaders() );
918+
QList<QgsVectorTileRawData> rawTiles = QgsVectorTileLoader::blockingFetchTileRawData( mSourceType, vtProvider->sourcePath(), tileMatrix, QPointF(), tileRange, authcfg, dsUri.httpHeaders() );
906919
if ( rawTiles.isEmpty() )
907920
return QByteArray();
908921
return rawTiles.first().data;
@@ -1312,10 +1325,10 @@ void QgsVectorTileLayer::removeSelection()
13121325
// QgsVectorTileDataProvider
13131326
//
13141327
///@cond PRIVATE
1315-
QgsVectorTileDataProvider::QgsVectorTileDataProvider(
1316-
const ProviderOptions &options,
1317-
QgsDataProvider::ReadFlags flags )
1318-
: QgsDataProvider( QString(), options, flags )
1328+
QgsVectorTileDataProvider::QgsVectorTileDataProvider( const QString &uri,
1329+
const ProviderOptions &options,
1330+
QgsDataProvider::ReadFlags flags )
1331+
: QgsDataProvider( uri, options, flags )
13191332
{}
13201333

13211334
QgsCoordinateReferenceSystem QgsVectorTileDataProvider::crs() const
@@ -1367,42 +1380,69 @@ bool QgsVectorTileDataProvider::renderInPreview( const PreviewContext &context )
13671380
// QgsXyzVectorTileDataProvider
13681381
//
13691382

1370-
QgsXyzVectorTileDataProvider::QgsXyzVectorTileDataProvider( const ProviderOptions &providerOptions, ReadFlags flags )
1371-
: QgsVectorTileDataProvider( providerOptions, flags )
1383+
QgsXyzVectorTileDataProvider::QgsXyzVectorTileDataProvider( const QString &uri, const ProviderOptions &providerOptions, ReadFlags flags )
1384+
: QgsVectorTileDataProvider( uri, providerOptions, flags )
13721385
{
13731386

13741387
}
13751388

1389+
QString QgsXyzVectorTileDataProvider::sourcePath() const
1390+
{
1391+
QgsDataSourceUri dsUri;
1392+
dsUri.setEncodedUri( dataSourceUri() );
1393+
return dsUri.param( QStringLiteral( "url" ) );
1394+
}
1395+
13761396
//
13771397
// QgsMbTilesVectorTileDataProvider
13781398
//
13791399

1380-
QgsMbTilesVectorTileDataProvider::QgsMbTilesVectorTileDataProvider( const ProviderOptions &providerOptions, ReadFlags flags )
1381-
: QgsVectorTileDataProvider( providerOptions, flags )
1400+
QgsMbTilesVectorTileDataProvider::QgsMbTilesVectorTileDataProvider( const QString &uri, const ProviderOptions &providerOptions, ReadFlags flags )
1401+
: QgsVectorTileDataProvider( uri, providerOptions, flags )
13821402
{
13831403

13841404
}
13851405

1406+
QString QgsMbTilesVectorTileDataProvider::sourcePath() const
1407+
{
1408+
QgsDataSourceUri dsUri;
1409+
dsUri.setEncodedUri( dataSourceUri() );
1410+
return dsUri.param( QStringLiteral( "url" ) );
1411+
}
1412+
13861413
//
13871414
// QgsVtpkVectorTileDataProvider
13881415
//
13891416

1390-
QgsVtpkVectorTileDataProvider::QgsVtpkVectorTileDataProvider( const ProviderOptions &providerOptions, ReadFlags flags )
1391-
: QgsVectorTileDataProvider( providerOptions, flags )
1417+
QgsVtpkVectorTileDataProvider::QgsVtpkVectorTileDataProvider( const QString &uri, const ProviderOptions &providerOptions, ReadFlags flags )
1418+
: QgsVectorTileDataProvider( uri, providerOptions, flags )
13921419
{
13931420

13941421
}
13951422

1423+
QString QgsVtpkVectorTileDataProvider::sourcePath() const
1424+
{
1425+
QgsDataSourceUri dsUri;
1426+
dsUri.setEncodedUri( dataSourceUri() );
1427+
return dsUri.param( QStringLiteral( "url" ) );
1428+
}
1429+
13961430
//
13971431
// QgsArcGisVectorTileServiceDataProvider
13981432
//
13991433

1400-
QgsArcGisVectorTileServiceDataProvider::QgsArcGisVectorTileServiceDataProvider( const ProviderOptions &providerOptions, ReadFlags flags )
1401-
: QgsVectorTileDataProvider( providerOptions, flags )
1434+
QgsArcGisVectorTileServiceDataProvider::QgsArcGisVectorTileServiceDataProvider( const QString &uri, const QString &sourcePath, const ProviderOptions &providerOptions, ReadFlags flags )
1435+
: QgsVectorTileDataProvider( uri, providerOptions, flags )
1436+
, mSourcePath( sourcePath )
14021437
{
14031438

14041439
}
14051440

1441+
QString QgsArcGisVectorTileServiceDataProvider::sourcePath() const
1442+
{
1443+
return mSourcePath;
1444+
}
1445+
14061446
///@endcond
14071447

14081448

‎src/core/vectortile/qgsvectortilelayer.h

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class CORE_EXPORT QgsVectorTileLayer : public QgsMapLayer
188188
//! Returns type of the data source
189189
QString sourceType() const { return mSourceType; }
190190
//! Returns URL/path of the data source (syntax different to each data source type)
191-
QString sourcePath() const { return mSourcePath; }
191+
QString sourcePath() const;
192192

193193
//! Returns minimum zoom level at which source has any valid tiles (negative = unconstrained)
194194
int sourceMinZoom() const { return mMatrixSet.minimumZoom(); }
@@ -290,8 +290,6 @@ class CORE_EXPORT QgsVectorTileLayer : public QgsMapLayer
290290
private:
291291
//! Type of the data source
292292
QString mSourceType;
293-
//! URL/Path of the data source
294-
QString mSourcePath;
295293

296294
QgsVectorTileMatrixSet mMatrixSet;
297295

@@ -334,7 +332,8 @@ class QgsVectorTileDataProvider : public QgsDataProvider
334332
Q_OBJECT
335333

336334
public:
337-
QgsVectorTileDataProvider( const QgsDataProvider::ProviderOptions &providerOptions,
335+
QgsVectorTileDataProvider( const QString &uri,
336+
const QgsDataProvider::ProviderOptions &providerOptions,
338337
QgsDataProvider::ReadFlags flags );
339338
QgsCoordinateReferenceSystem crs() const override;
340339
QString name() const override;
@@ -343,44 +342,62 @@ class QgsVectorTileDataProvider : public QgsDataProvider
343342
bool isValid() const override;
344343
bool renderInPreview( const QgsDataProvider::PreviewContext &context ) override;
345344

345+
virtual QString sourcePath() const = 0;
346+
346347
};
347348

348349
class QgsXyzVectorTileDataProvider : public QgsVectorTileDataProvider
349350
{
350351
Q_OBJECT
351352

352353
public:
353-
QgsXyzVectorTileDataProvider( const QgsDataProvider::ProviderOptions &providerOptions,
354+
QgsXyzVectorTileDataProvider( const QString &uri,
355+
const QgsDataProvider::ProviderOptions &providerOptions,
354356
QgsDataProvider::ReadFlags flags );
355357

358+
QString sourcePath() const override;
359+
356360
};
357361

358362
class QgsMbTilesVectorTileDataProvider : public QgsVectorTileDataProvider
359363
{
360364
Q_OBJECT
361365

362366
public:
363-
QgsMbTilesVectorTileDataProvider( const QgsDataProvider::ProviderOptions &providerOptions,
367+
QgsMbTilesVectorTileDataProvider( const QString &uri,
368+
const QgsDataProvider::ProviderOptions &providerOptions,
364369
QgsDataProvider::ReadFlags flags );
365370

371+
QString sourcePath() const override;
372+
366373
};
367374

368375
class QgsVtpkVectorTileDataProvider : public QgsVectorTileDataProvider
369376
{
370377
Q_OBJECT
371378

372379
public:
373-
QgsVtpkVectorTileDataProvider( const QgsDataProvider::ProviderOptions &providerOptions,
380+
QgsVtpkVectorTileDataProvider( const QString &uri,
381+
const QgsDataProvider::ProviderOptions &providerOptions,
374382
QgsDataProvider::ReadFlags flags );
383+
384+
QString sourcePath() const override;
375385
};
376386

377387
class QgsArcGisVectorTileServiceDataProvider : public QgsVectorTileDataProvider
378388
{
379389
Q_OBJECT
380390

381391
public:
382-
QgsArcGisVectorTileServiceDataProvider( const QgsDataProvider::ProviderOptions &providerOptions,
392+
QgsArcGisVectorTileServiceDataProvider( const QString &uri,
393+
const QString &sourcePath,
394+
const QgsDataProvider::ProviderOptions &providerOptions,
383395
QgsDataProvider::ReadFlags flags );
396+
QString sourcePath() const override;
397+
398+
private:
399+
400+
QString mSourcePath;
384401
};
385402

386403
///@endcond

0 commit comments

Comments
 (0)
Please sign in to comment.