Skip to content
Permalink
Browse files

Add QgsProviderMetadata::encodeUri

  • Loading branch information
manisandro committed Nov 2, 2019
1 parent c4a2bb0 commit 20ea2ae4ae0176b2cfa19eafd889e9be3ce7ff7b
@@ -217,6 +217,23 @@ Breaks a provider data source URI into its component paths (e.g. file path, laye
this function may not be supported by all providers, an empty map will be returned in such case

.. versionadded:: 3.10
%End

virtual QString encodeUri( const QVariantMap &parts );
%Docstring
Reassembles a provider data source URI from its component paths (e.g. file path, layer name).

:param parts: parts as returned by decodeUri

:return: datasource uri string

.. note::

this function may not be supported by all providers, an empty string will be returned in such case

.. seealso:: :py:func:`decodeUri`

.. versionadded:: 3.12
%End

virtual QList< QgsDataItemProvider * > dataItemProviders() const /Factory/;
@@ -143,6 +143,24 @@ Breaks a provider data source URI into its component paths (e.g. file path, laye
this function may not be supported by all providers, an empty map will be returned in such case

.. versionadded:: 3.4
%End

QString encodeUri( const QString &providerKey, const QVariantMap &parts );
%Docstring
Reassembles a provider data source URI from its component paths (e.g. file path, layer name).

:param providerKey: identifier of the provider
:param parts: parts as returned by decodeUri

:return: datasource uri string

.. note::

this function may not be supported by all providers, an empty string will be returned in such case

.. seealso:: :py:func:`decodeUri`

.. versionadded:: 3.12
%End

QWidget *createSelectionWidget( const QString &providerKey, QWidget *parent = 0, Qt::WindowFlags fl = Qt::WindowFlags(), QgsProviderRegistry::WidgetMode widgetMode = QgsProviderRegistry::WidgetMode::None ) /Deprecated/;
@@ -2092,6 +2092,13 @@ QVariantMap QgsGdalProviderMetadata::decodeUri( const QString &uri )
return uriComponents;
}

QString QgsGdalProviderMetadata::encodeUri( const QVariantMap &parts )
{
QString path = parts.value( QStringLiteral( "path" ) ).toString();
QString layerName = parts.value( QStringLiteral( "layerName" ) ).toString();
return path + ( !layerName.isEmpty() ? QStringLiteral( "|%1" ).arg( layerName ) : QString() );
}


QgsGdalProvider *QgsGdalProviderMetadata::createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options )
{
@@ -348,6 +348,7 @@ class QgsGdalProviderMetadata: public QgsProviderMetadata
public:
QgsGdalProviderMetadata();
QVariantMap decodeUri( const QString &uri ) override;
QString encodeUri( const QVariantMap &parts ) override;
QgsGdalProvider *createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options ) override;
QgsGdalProvider *createRasterDataProvider(
const QString &uri,
@@ -3378,6 +3378,14 @@ QVariantMap QgsOgrProviderMetadata::decodeUri( const QString &uri )
return uriComponents;
}

QString QgsOgrProviderMetadata::encodeUri( const QVariantMap &parts )
{
QString path = parts.value( QStringLiteral( "path" ) ).toString();
QString layerName = parts.value( QStringLiteral( "layerName" ) ).toString();
QString layerId = parts.value( QStringLiteral( "layerId" ) ).toString();
return path + ( !layerName.isEmpty() ? QStringLiteral( "|layername=%1" ).arg( layerName ) : !layerId.isEmpty() ? QStringLiteral( "|layerid=%1" ).arg( layerId ) : QString() );
}

QString QgsOgrProviderUtils::fileVectorFilters()
{
return createFilters( QStringLiteral( "file" ) );
@@ -744,6 +744,7 @@ class QgsOgrProviderMetadata: public QgsProviderMetadata
QList< QgsDataItemProvider * > dataItemProviders() const override;
QgsOgrProvider *createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options ) override;
QVariantMap decodeUri( const QString &uri ) override;
QString encodeUri( const QVariantMap &parts ) override;
QString filters( FilterType type ) override;
QgsVectorLayerExporter::ExportError createEmptyLayer(
const QString &uri,
@@ -94,6 +94,11 @@ QVariantMap QgsProviderMetadata::decodeUri( const QString & )
return QVariantMap();
}

QString QgsProviderMetadata::encodeUri( const QVariantMap & )
{
return QString();
}

QgsVectorLayerExporter::ExportError QgsProviderMetadata::createEmptyLayer(
const QString &, const QgsFields &,
QgsWkbTypes::Type, const QgsCoordinateReferenceSystem &,
@@ -276,6 +276,16 @@ class CORE_EXPORT QgsProviderMetadata
*/
virtual QVariantMap decodeUri( const QString &uri );

/**
* Reassembles a provider data source URI from its component paths (e.g. file path, layer name).
* \param parts parts as returned by decodeUri
* \returns datasource uri string
* \note this function may not be supported by all providers, an empty string will be returned in such case
* \see decodeUri()
* \since QGIS 3.12
*/
virtual QString encodeUri( const QVariantMap &parts );

/**
* Returns data item providers. Caller is responsible for ownership of the item providers
* \see QgsProviderGuiMetadata::dataItemGuiProviders()
@@ -405,6 +405,15 @@ QVariantMap QgsProviderRegistry::decodeUri( const QString &providerKey, const QS
return QVariantMap();
}

QString QgsProviderRegistry::encodeUri( const QString &providerKey, const QVariantMap &parts )
{
QgsProviderMetadata *meta = findMetadata_( mProviders, providerKey );
if ( meta )
return meta->encodeUri( parts );
else
return QString();
}

QgsVectorLayerExporter::ExportError QgsProviderRegistry::createEmptyLayer( const QString &providerKey,
const QString &uri,
const QgsFields &fields,
@@ -172,6 +172,17 @@ class CORE_EXPORT QgsProviderRegistry
*/
QVariantMap decodeUri( const QString &providerKey, const QString &uri );

/**
* Reassembles a provider data source URI from its component paths (e.g. file path, layer name).
* \param providerKey identifier of the provider
* \param parts parts as returned by decodeUri
* \returns datasource uri string
* \note this function may not be supported by all providers, an empty string will be returned in such case
* \see decodeUri()
* \since QGIS 3.12
*/
QString encodeUri( const QString &providerKey, const QVariantMap &parts );

/**
* Returns a new widget for selecting layers from a provider.
* Either the \a parent widget must be set or the caller becomes
@@ -375,6 +375,13 @@ QVariantMap QgsAfsProviderMetadata::decodeUri( const QString &uri )
return components;
}

QString QgsAfsProviderMetadata::encodeUri( const QVariantMap &parts )
{
QgsDataSourceUri dsUri;
dsUri.setParam( QStringLiteral( "url" ), parts.value( QStringLiteral( "url" ) ).toString() );
return dsUri.uri();
}

QgsAfsProvider *QgsAfsProviderMetadata::createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options )
{
return new QgsAfsProvider( uri, options );
@@ -101,6 +101,7 @@ class QgsAfsProviderMetadata: public QgsProviderMetadata
QgsAfsProviderMetadata();
QList<QgsDataItemProvider *> dataItemProviders() const override;
QVariantMap decodeUri( const QString &uri ) override;
QString encodeUri( const QVariantMap &parts ) override;
QgsAfsProvider *createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options ) override;

};
@@ -1261,6 +1261,12 @@ QVariantMap QgsAmsProviderMetadata::decodeUri( const QString &uri )
return components;
}

QString QgsAmsProviderMetadata::encodeUri( const QVariantMap &parts )
{
QgsDataSourceUri dsUri;
dsUri.setParam( QStringLiteral( "url" ), parts.value( QStringLiteral( "url" ) ).toString() );
return dsUri.uri();
}

QGISEXTERN QgsProviderMetadata *providerMetadataFactory()
{
@@ -223,6 +223,7 @@ class QgsAmsProviderMetadata: public QgsProviderMetadata
QList<QgsDataItemProvider *> dataItemProviders() const override;
QgsAmsProvider *createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options ) override;
QVariantMap decodeUri( const QString &uri ) override;
QString encodeUri( const QVariantMap &parts ) override;
};

#endif // QGSMAPSERVERPROVIDER_H
@@ -1204,6 +1204,11 @@ QVariantMap QgsDelimitedTextProviderMetadata::decodeUri( const QString &uri )
return components;
}

QString QgsDelimitedTextProviderMetadata::encodeUri( const QVariantMap &parts )
{
return QStringLiteral( "file://%1" ).arg( parts.value( QStringLiteral( "path" ) ).toString() );
}

QgsDataProvider *QgsDelimitedTextProviderMetadata::createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options )
{
return new QgsDelimitedTextProvider( uri, options );
@@ -240,6 +240,7 @@ class QgsDelimitedTextProviderMetadata: public QgsProviderMetadata
QgsDelimitedTextProviderMetadata();
QgsDataProvider *createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options ) override;
QVariantMap decodeUri( const QString &uri ) override;
QString encodeUri( const QVariantMap &parts ) override;
};

#endif
@@ -5532,6 +5532,14 @@ QgsSpatiaLiteProvider *QgsSpatiaLiteProviderMetadata::createProvider(
return new QgsSpatiaLiteProvider( uri, options );
}

QString QgsSpatiaLiteProviderMetadata::encodeUri( const QVariantMap &parts )
{
QgsDataSourceUri dsUri;
dsUri.setDatabase( parts.value( QStringLiteral( "path" ) ).toString() );
dsUri.setTable( parts.value( QStringLiteral( "layerName" ) ).toString() );
return dsUri.uri();
}


QgsVectorLayerExporter::ExportError QgsSpatiaLiteProviderMetadata::createEmptyLayer(
const QString &uri,
@@ -398,6 +398,7 @@ class QgsSpatiaLiteProviderMetadata: public QgsProviderMetadata
int listStyles( const QString &uri, QStringList &ids, QStringList &names,
QStringList &descriptions, QString &errCause ) override;
QVariantMap decodeUri( const QString &uri ) override;
QString encodeUri( const QVariantMap &parts ) override;
QgsSpatiaLiteProvider *createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options ) override;

QgsVectorLayerExporter::ExportError createEmptyLayer( const QString &uri, const QgsFields &fields,
@@ -45,6 +45,7 @@ class TestQgsGdalProvider : public QObject
void cleanup() {}// will be called after every testfunction.

void decodeUri(); // test decode URI implementation
void encodeUri(); // test encode URI implementation
void scaleDataType(); //test resultant data types for int raster with float scale (#11573)
void warpedVrt(); //test loading raster which requires a warped vrt
void noData();
@@ -108,6 +109,16 @@ void TestQgsGdalProvider::decodeUri()
QCOMPARE( components[QStringLiteral( "layerName" )].toString(), QStringLiteral( "layer_name" ) );
}

void TestQgsGdalProvider::encodeUri()
{
QVariantMap parts;
parts.insert( QStringLiteral( "path" ), QStringLiteral( "/home/user/test.gpkg" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "gdal" ), parts ), QStringLiteral( "/home/user/test.gpkg" ) );

parts.insert( QStringLiteral( "layerName" ), QStringLiteral( "layername" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "gdal" ), parts ), QStringLiteral( "/home/user/test.gpkg|layername" ) );
}

void TestQgsGdalProvider::scaleDataType()
{
QString rasterWithOffset = QStringLiteral( TEST_DATA_DIR ) + "/int_raster_with_scale.tif";
@@ -48,6 +48,7 @@ class TestQgsOgrProvider : public QObject

void setupProxy();
void decodeUri();
void encodeUri();
void testThread();
//! Test GPKG data items rename
void testGpkgDataItemRename();
@@ -135,6 +136,26 @@ void TestQgsOgrProvider::decodeUri()
QCOMPARE( parts.value( QStringLiteral( "layerName" ) ).toString(), QString( "a_layer" ) );
}

void TestQgsOgrProvider::encodeUri()
{
QVariantMap parts;
parts.insert( QStringLiteral( "path" ), QStringLiteral( "/home/user/test.gpkg" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "gdal" ), parts ), QStringLiteral( "/home/user/test.gpkg" ) );

// layerName only
parts.insert( QStringLiteral( "layerName" ), QStringLiteral( "test" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "gdal" ), parts ), QStringLiteral( "/home/user/test.gpkg|layername=test" ) );
parts.remove( QStringLiteral( "layerName" ) );

// layerId only
parts.insert( QStringLiteral( "layerId" ), QStringLiteral( "testid" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "gdal" ), parts ), QStringLiteral( "/home/user/test.gpkg|layerid=testid" ) );

// Both layerName and layerId: layerName takes precedence
parts.insert( QStringLiteral( "layerName" ), QStringLiteral( "test" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "gdal" ), parts ), QStringLiteral( "/home/user/test.gpkg|layername=test|layername=test" ) );

}

class ReadVectorLayer : public QThread
{

0 comments on commit 20ea2ae

Please sign in to comment.
You can’t perform that action at this time.