Skip to content

Commit

Permalink
Add encodeUri/decodeUri tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Sep 27, 2021
1 parent 619aa7c commit 52a0529
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/core/vectortile/qgsvectortilelayer.cpp
Expand Up @@ -53,6 +53,7 @@ void QgsVectorTileLayer::setDataSourcePrivate( const QString &dataSource, const
{
mDataSource = dataSource;
mLayerName = baseName;
mDataProvider.reset();

setValid( loadDataSource() );
}
Expand Down Expand Up @@ -128,7 +129,7 @@ bool QgsVectorTileLayer::loadDataSource()

setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ) );

const QgsDataProvider::ProviderOptions providerOptions;
const QgsDataProvider::ProviderOptions providerOptions { transformContext() };
const QgsDataProvider::ReadFlags flags;
mDataProvider.reset( new QgsVectorTileDataProvider( providerOptions, flags ) );
mProviderKey = mDataProvider->name();
Expand Down
38 changes: 37 additions & 1 deletion src/core/vectortile/qgsvectortileprovidermetadata.cpp
Expand Up @@ -67,22 +67,58 @@ QVariantMap QgsVectorTileProviderMetadata::decodeUri( const QString &uri ) const

QVariantMap uriComponents;
uriComponents.insert( QStringLiteral( "type" ), dsUri.param( QStringLiteral( "type" ) ) );
if ( uriComponents[ QStringLiteral( "type" ) ] == QStringLiteral( "mbtiles" ) )
if ( dsUri.hasParam( QStringLiteral( "serviceType" ) ) )
uriComponents.insert( QStringLiteral( "serviceType" ), dsUri.param( QStringLiteral( "serviceType" ) ) );

if ( uriComponents[ QStringLiteral( "type" ) ] == QStringLiteral( "mbtiles" ) ||
( uriComponents[ QStringLiteral( "type" ) ] == QStringLiteral( "xyz" ) &&
!dsUri.param( QStringLiteral( "url" ) ).startsWith( QStringLiteral( "http" ) ) ) )
{
uriComponents.insert( QStringLiteral( "path" ), dsUri.param( QStringLiteral( "url" ) ) );
}
else
{
uriComponents.insert( QStringLiteral( "url" ), dsUri.param( QStringLiteral( "url" ) ) );
}

if ( dsUri.hasParam( QStringLiteral( "zmin" ) ) )
uriComponents.insert( QStringLiteral( "zmin" ), dsUri.param( QStringLiteral( "zmin" ) ) );
if ( dsUri.hasParam( QStringLiteral( "zmax" ) ) )
uriComponents.insert( QStringLiteral( "zmax" ), dsUri.param( QStringLiteral( "zmax" ) ) );

if ( dsUri.hasParam( QStringLiteral( "referer" ) ) )
uriComponents.insert( QStringLiteral( "referer" ), dsUri.param( QStringLiteral( "referer" ) ) );
if ( dsUri.hasParam( QStringLiteral( "styleUrl" ) ) )
uriComponents.insert( QStringLiteral( "styleUrl" ), dsUri.param( QStringLiteral( "styleUrl" ) ) );

const QString authcfg = dsUri.authConfigId();
if ( !authcfg.isEmpty() )
uriComponents.insert( QStringLiteral( "authcfg" ), authcfg );

return uriComponents;
}

QString QgsVectorTileProviderMetadata::encodeUri( const QVariantMap &parts ) const
{
QgsDataSourceUri dsUri;
dsUri.setParam( QStringLiteral( "type" ), parts.value( QStringLiteral( "type" ) ).toString() );
if ( parts.contains( QStringLiteral( "serviceType" ) ) )
dsUri.setParam( QStringLiteral( "serviceType" ), parts[ QStringLiteral( "serviceType" ) ].toString() );
dsUri.setParam( QStringLiteral( "url" ), parts.value( parts.contains( QStringLiteral( "path" ) ) ? QStringLiteral( "path" ) : QStringLiteral( "url" ) ).toString() );

if ( parts.contains( QStringLiteral( "zmin" ) ) )
dsUri.setParam( QStringLiteral( "zmin" ), parts[ QStringLiteral( "zmin" ) ].toString() );
if ( parts.contains( QStringLiteral( "zmax" ) ) )
dsUri.setParam( QStringLiteral( "zmax" ), parts[ QStringLiteral( "zmax" ) ].toString() );

if ( parts.contains( QStringLiteral( "referer" ) ) )
dsUri.setParam( QStringLiteral( "referer" ), parts[ QStringLiteral( "referer" ) ].toString() );
if ( parts.contains( QStringLiteral( "styleUrl" ) ) )
dsUri.setParam( QStringLiteral( "styleUrl" ), parts[ QStringLiteral( "styleUrl" ) ].toString() );

if ( parts.contains( QStringLiteral( "authcfg" ) ) )
dsUri.setAuthConfigId( parts[ QStringLiteral( "authcfg" ) ].toString() );

return dsUri.encodedUri();
}

Expand Down
32 changes: 31 additions & 1 deletion tests/src/python/test_qgsvectortile.py
Expand Up @@ -26,7 +26,9 @@
from qgis.core import (QgsVectorLayer,
QgsVectorTileWriter,
QgsDataSourceUri,
QgsTileXYZ)
QgsTileXYZ,
QgsProviderRegistry,
QgsProviderMetadata)

from pathlib import Path

Expand Down Expand Up @@ -87,6 +89,34 @@ def testSingleTileEncode(self):
# Compare binary data
self.assertEqual(ascii(data.data()), ascii(output))

def testEncodeDecodeUri(self):
""" Test encodeUri/decodeUri metadata functions """
md = QgsProviderRegistry.instance().providerMetadata('vectortile')

uri = 'type=mbtiles&url=/my/file.mbtiles'
parts = md.decodeUri(uri)
self.assertEqual(parts, {'type': 'mbtiles', 'path': '/my/file.mbtiles'})

parts['path'] = '/my/new/file.mbtiles'
uri = md.encodeUri(parts)
self.assertEqual(uri, 'type=mbtiles&url=/my/new/file.mbtiles')

uri = 'type=xyz&url=https://fake.server/%7Bx%7D/%7By%7D/%7Bz%7D.png&zmin=0&zmax=2'
parts = md.decodeUri(uri)
self.assertEqual(parts, {'type': 'xyz', 'url': 'https://fake.server/{x}/{y}/{z}.png', 'zmin': '0', 'zmax': '2'})

parts['url'] = 'https://fake.new.server/{x}/{y}/{z}.png'
uri = md.encodeUri(parts)
self.assertEqual(uri, 'type=xyz&url=https://fake.new.server/%7Bx%7D/%7By%7D/%7Bz%7D.png&zmax=2&zmin=0')

uri = 'type=xyz&serviceType=arcgis&url=https://fake.server/%7Bx%7D/%7By%7D/%7Bz%7D.png&zmax=2&referer=https://qgis.org/&styleUrl=https://qgis.org/'
parts = md.decodeUri(uri)
self.assertEqual(parts, {'type': 'xyz', 'serviceType': 'arcgis', 'url': 'https://fake.server/{x}/{y}/{z}.png', 'zmax': '2', 'referer': 'https://qgis.org/', 'styleUrl': 'https://qgis.org/'})

parts['url'] = 'https://fake.new.server/{x}/{y}/{z}.png'
uri = md.encodeUri(parts)
self.assertEqual(uri, 'referer=https://qgis.org/&serviceType=arcgis&styleUrl=https://qgis.org/&type=xyz&url=https://fake.new.server/%7Bx%7D/%7By%7D/%7Bz%7D.png&zmax=2')


if __name__ == '__main__':
unittest.main()

0 comments on commit 52a0529

Please sign in to comment.