1
0
mirror of https://github.com/qgis/QGIS.git synced 2025-04-29 00:03:59 -04:00

Make constructors for QgsVectorLayer and QgsRasterLayer more flexible

...by moving extra arguments to new LayerOptions structs. This allows
us to more easily add new layer constructor options without making
the API cumbersome to use.
This commit is contained in:
Nyall Dawson 2017-11-04 07:54:31 +10:00
parent 61b2f74aae
commit ba62ffce2c
14 changed files with 217 additions and 118 deletions

@ -2080,9 +2080,9 @@ QgsRasterInterface {#qgis_api_break_3_0_QgsRasterInterface}
QgsRasterLayer {#qgis_api_break_3_0_QgsRasterLayer} QgsRasterLayer {#qgis_api_break_3_0_QgsRasterLayer}
-------------- --------------
- The constructor variant with loadDefaultStyleFlag as the 3rd parameter was removed. Use the - The QgsRasterLayer constructor now takes a QgsRasterLayer.LayerOptions argument instead of the old
constructor variant which accepts a data provider string and loadDefaultStyleFlag as the loadDefaultStyle argument.
4th parameter instead. - The constructor variant with loadDefaultStyleFlag as the 3rd parameter was removed.
- setDrawingStyle() was removed. Use setRendererForDrawingStyle() or setRenderer() instead. - setDrawingStyle() was removed. Use setRendererForDrawingStyle() or setRenderer() instead.
- previewAsPixmap() was removed. Use previewAsImage() instead. - previewAsPixmap() was removed. Use previewAsImage() instead.
- updateProgress() had no effect and was removed. - updateProgress() had no effect and was removed.
@ -2551,6 +2551,8 @@ QgsVectorJoinInfo {#qgis_api_break_3_0_QgsVectorJoinInfo}
QgsVectorLayer {#qgis_api_break_3_0_QgsVectorLayer} QgsVectorLayer {#qgis_api_break_3_0_QgsVectorLayer}
-------------- --------------
- The QgsVectorLayer constructor now takes a QgsVectorLayer.LayerOptions argument instead of the old
loadDefaultStyle argument.
- excludeAttributesWMS() and setExcludeAttributesWMS() have been renamed to excludeAttributesWms() and - excludeAttributesWMS() and setExcludeAttributesWMS() have been renamed to excludeAttributesWms() and
setExcludeAttributesWms() setExcludeAttributesWms()
- excludeAttributesWFS() and setExcludeAttributesWFS() have been renamed to excludeAttributesWfs() and - excludeAttributesWFS() and setExcludeAttributesWFS() have been renamed to excludeAttributesWfs() and

@ -314,9 +314,29 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSin
RemoveFromSelection, RemoveFromSelection,
}; };
QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(), struct LayerOptions
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true, {
bool readExtentFromXml = false );
explicit LayerOptions( bool loadDefaultStyle = true, bool readExtentFromXml = false );
%Docstring
Constructor for LayerOptions.
%End
bool loadDefaultStyle;
%Docstring
Set to true if the default layer style should be loaded
%End
bool readExtentFromXml;
%Docstring
If true, the layer extent will be read from XML (i.e. stored in the
project file). If false, the extent will be determined by the provider on layer load.
%End
};
explicit QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
const QString &providerLib = "ogr", const QgsVectorLayer::LayerOptions &options = QgsVectorLayer::LayerOptions() );
%Docstring %Docstring
Constructor - creates a vector layer Constructor - creates a vector layer
@ -328,8 +348,7 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSin
parameters used by the data provider as url query items. parameters used by the data provider as url query items.
\param baseName The name used to represent the layer in the legend \param baseName The name used to represent the layer in the legend
\param providerLib The name of the data provider, e.g., "memory", "postgres" \param providerLib The name of the data provider, e.g., "memory", "postgres"
\param loadDefaultStyleFlag whether to load the default style \param options layer load options
\param readExtentFromXml Read extent from XML if true or let provider determine it if false
%End %End

@ -146,10 +146,24 @@ class QgsRasterLayer : QgsMapLayer
Constructor. Provider is not set. Constructor. Provider is not set.
%End %End
QgsRasterLayer( const QString &uri, struct LayerOptions
{
explicit LayerOptions( bool loadDefaultStyle = true );
%Docstring
Constructor for LayerOptions.
%End
bool loadDefaultStyle;
%Docstring
Set to true if the default layer style should be loaded
%End
};
explicit QgsRasterLayer( const QString &uri,
const QString &baseName = QString(), const QString &baseName = QString(),
const QString &providerKey = "gdal", const QString &providerKey = "gdal",
bool loadDefaultStyleFlag = true ); const QgsRasterLayer::LayerOptions &options = QgsRasterLayer::LayerOptions() );
%Docstring %Docstring
This is the constructor for the RasterLayer class. This is the constructor for the RasterLayer class.

@ -239,10 +239,14 @@ class AlgorithmsTest(object):
if filepath in self.vector_layer_params: if filepath in self.vector_layer_params:
return self.vector_layer_params[filepath] return self.vector_layer_params[filepath]
lyr = QgsVectorLayer(filepath, param['name'], 'ogr', False) options = QgsVectorLayer.LayerOptions()
options.loadDefaultStyle = False
lyr = QgsVectorLayer(filepath, param['name'], 'ogr', options)
self.vector_layer_params[filepath] = lyr self.vector_layer_params[filepath] = lyr
elif param['type'] == 'raster': elif param['type'] == 'raster':
lyr = QgsRasterLayer(filepath, param['name'], 'gdal', False) options = QgsRasterLayer.LayerOptions()
options.loadDefaultStyle = False
lyr = QgsRasterLayer(filepath, param['name'], 'gdal', options)
self.assertTrue(lyr.isValid(), 'Could not load layer "{}" from param {}'.format(filepath, param)) self.assertTrue(lyr.isValid(), 'Could not load layer "{}" from param {}'.format(filepath, param))
QgsProject.instance().addMapLayer(lyr) QgsProject.instance().addMapLayer(lyr)

@ -167,7 +167,9 @@ void QgsDwgImportDialog::pbLoadDatabase_clicked()
bool lblVisible = false; bool lblVisible = false;
std::unique_ptr<QgsVectorLayer> d( new QgsVectorLayer( QStringLiteral( "%1|layername=drawing" ).arg( leDatabase->text() ), QStringLiteral( "layers" ), QStringLiteral( "ogr" ), false ) ); QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
std::unique_ptr<QgsVectorLayer> d( new QgsVectorLayer( QStringLiteral( "%1|layername=drawing" ).arg( leDatabase->text() ), QStringLiteral( "layers" ), QStringLiteral( "ogr" ), options ) );
if ( d && d->isValid() ) if ( d && d->isValid() )
{ {
int idxPath = d->fields().lookupField( QStringLiteral( "path" ) ); int idxPath = d->fields().lookupField( QStringLiteral( "path" ) );
@ -202,7 +204,7 @@ void QgsDwgImportDialog::pbLoadDatabase_clicked()
lblMessage->setVisible( lblVisible ); lblMessage->setVisible( lblVisible );
std::unique_ptr<QgsVectorLayer> l( new QgsVectorLayer( QStringLiteral( "%1|layername=layers" ).arg( leDatabase->text() ), QStringLiteral( "layers" ), QStringLiteral( "ogr" ), false ) ); std::unique_ptr<QgsVectorLayer> l( new QgsVectorLayer( QStringLiteral( "%1|layername=layers" ).arg( leDatabase->text() ), QStringLiteral( "layers" ), QStringLiteral( "ogr" ), options ) );
if ( l && l->isValid() ) if ( l && l->isValid() )
{ {
int idxName = l->fields().lookupField( QStringLiteral( "name" ) ); int idxName = l->fields().lookupField( QStringLiteral( "name" ) );
@ -278,7 +280,9 @@ void QgsDwgImportDialog::pbImportDrawing_clicked()
QgsVectorLayer *QgsDwgImportDialog::layer( QgsLayerTreeGroup *layerGroup, const QString &layerFilter, const QString &table ) QgsVectorLayer *QgsDwgImportDialog::layer( QgsLayerTreeGroup *layerGroup, const QString &layerFilter, const QString &table )
{ {
QgsVectorLayer *l = new QgsVectorLayer( QStringLiteral( "%1|layername=%2" ).arg( leDatabase->text(), table ), table, QStringLiteral( "ogr" ), false ); QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsVectorLayer *l = new QgsVectorLayer( QStringLiteral( "%1|layername=%2" ).arg( leDatabase->text(), table ), table, QStringLiteral( "ogr" ), options );
l->setSubsetString( QStringLiteral( "%1space=0 AND block=-1" ).arg( layerFilter ) ); l->setSubsetString( QStringLiteral( "%1space=0 AND block=-1" ).arg( layerFilter ) );
if ( l->featureCount() == 0 ) if ( l->featureCount() == 0 )

@ -4162,7 +4162,9 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
// create the layer // create the layer
QgsVectorLayer *layer = new QgsVectorLayer( src, base, QStringLiteral( "ogr" ), false ); QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( src, base, QStringLiteral( "ogr" ), options );
Q_CHECK_PTR( layer ); Q_CHECK_PTR( layer );
if ( ! layer ) if ( ! layer )
@ -4636,7 +4638,9 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
QString name = fileName + " " + def.layerName; QString name = fileName + " " + def.layerName;
if ( !layerGeometryType.isEmpty() ) if ( !layerGeometryType.isEmpty() )
name += " " + layerGeometryType; name += " " + layerGeometryType;
QgsVectorLayer *layer = new QgsVectorLayer( composedURI, name, QStringLiteral( "ogr" ), false ); QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( composedURI, name, QStringLiteral( "ogr" ), options );
if ( layer && layer->isValid() ) if ( layer && layer->isValid() )
{ {
myList << layer; myList << layer;
@ -4714,7 +4718,9 @@ void QgisApp::addDatabaseLayers( QStringList const &layerPathList, QString const
// create the layer // create the layer
QgsDataSourceUri uri( layerPath ); QgsDataSourceUri uri( layerPath );
QgsVectorLayer *layer = new QgsVectorLayer( uri.uri( false ), uri.table(), providerKey, false ); QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( uri.uri( false ), uri.table(), providerKey, options );
Q_CHECK_PTR( layer ); Q_CHECK_PTR( layer );
if ( ! layer ) if ( ! layer )
@ -9968,7 +9974,9 @@ QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const Q
} }
// create the layer // create the layer
QgsVectorLayer *layer = new QgsVectorLayer( vectorLayerPath, baseName, providerKey, false ); QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( vectorLayerPath, baseName, providerKey, options );
if ( authok && layer && layer->isValid() ) if ( authok && layer && layer->isValid() )
{ {

@ -174,12 +174,16 @@ QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string )
QString name = fi.baseName(); QString name = fi.baseName();
// brute force attempt to load a matching layer // brute force attempt to load a matching layer
std::unique_ptr< QgsVectorLayer > layer( new QgsVectorLayer( string, name, QStringLiteral( "ogr" ), false ) ); QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
std::unique_ptr< QgsVectorLayer > layer( new QgsVectorLayer( string, name, QStringLiteral( "ogr" ), options ) );
if ( layer->isValid() ) if ( layer->isValid() )
{ {
return layer.release(); return layer.release();
} }
std::unique_ptr< QgsRasterLayer > rasterLayer( new QgsRasterLayer( string, name, QStringLiteral( "gdal" ), false ) ); QgsRasterLayer::LayerOptions rasterOptions;
rasterOptions.loadDefaultStyle = false;
std::unique_ptr< QgsRasterLayer > rasterLayer( new QgsRasterLayer( string, name, QStringLiteral( "gdal" ), rasterOptions ) );
if ( rasterLayer->isValid() ) if ( rasterLayer->isValid() )
{ {
return rasterLayer.release(); return rasterLayer.release();

@ -340,7 +340,9 @@ void QgsProjectFileTransform::transform0110to1000()
QString providerKey = providerNode.toElement().text(); QString providerKey = providerNode.toElement().text();
//create the layer to get the provider for int->fieldName conversion //create the layer to get the provider for int->fieldName conversion
QgsVectorLayer *layer = new QgsVectorLayer( dataSource, QLatin1String( "" ), providerKey, false ); QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( dataSource, QLatin1String( "" ), providerKey, options );
if ( !layer->isValid() ) if ( !layer->isValid() )
{ {
delete layer; delete layer;

@ -136,13 +136,12 @@ typedef bool deleteStyleById_t(
QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath, QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
const QString &baseName, const QString &baseName,
const QString &providerKey, const QString &providerKey,
bool loadDefaultStyleFlag, const LayerOptions &options )
bool readExtentFromXml )
: QgsMapLayer( VectorLayer, baseName, vectorLayerPath ) : QgsMapLayer( VectorLayer, baseName, vectorLayerPath )
, mProviderKey( providerKey ) , mProviderKey( providerKey )
, mAuxiliaryLayer( nullptr ) , mAuxiliaryLayer( nullptr )
, mAuxiliaryLayerKey( QString() ) , mAuxiliaryLayerKey( QString() )
, mReadExtentFromXml( readExtentFromXml ) , mReadExtentFromXml( options.readExtentFromXml )
{ {
mActions = new QgsActionManager( this ); mActions = new QgsActionManager( this );
mConditionalStyles = new QgsConditionalLayerStyles(); mConditionalStyles = new QgsConditionalLayerStyles();
@ -153,7 +152,7 @@ QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
// if we're given a provider type, try to create and bind one to this layer // if we're given a provider type, try to create and bind one to this layer
if ( !vectorLayerPath.isEmpty() && !mProviderKey.isEmpty() ) if ( !vectorLayerPath.isEmpty() && !mProviderKey.isEmpty() )
{ {
setDataSource( vectorLayerPath, baseName, providerKey, loadDefaultStyleFlag ); setDataSource( vectorLayerPath, baseName, providerKey, options.loadDefaultStyle );
} }
connect( this, &QgsVectorLayer::selectionChanged, this, [ = ] { emit repaintRequested(); } ); connect( this, &QgsVectorLayer::selectionChanged, this, [ = ] { emit repaintRequested(); } );

@ -381,6 +381,32 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
RemoveFromSelection, //!< Remove from current selection RemoveFromSelection, //!< Remove from current selection
}; };
/**
* Setting options for loading vector layers.
* \since QGIS 3.0
*/
struct LayerOptions
{
/**
* Constructor for LayerOptions.
*/
explicit LayerOptions( bool loadDefaultStyle = true, bool readExtentFromXml = false )
: loadDefaultStyle( loadDefaultStyle )
, readExtentFromXml( readExtentFromXml )
{}
//! Set to true if the default layer style should be loaded
bool loadDefaultStyle = true;
/**
* If true, the layer extent will be read from XML (i.e. stored in the
* project file). If false, the extent will be determined by the provider on layer load.
*/
bool readExtentFromXml = false;
};
/** /**
* Constructor - creates a vector layer * Constructor - creates a vector layer
* *
@ -392,13 +418,10 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* parameters used by the data provider as url query items. * parameters used by the data provider as url query items.
* \param baseName The name used to represent the layer in the legend * \param baseName The name used to represent the layer in the legend
* \param providerLib The name of the data provider, e.g., "memory", "postgres" * \param providerLib The name of the data provider, e.g., "memory", "postgres"
* \param loadDefaultStyleFlag whether to load the default style * \param options layer load options
* \param readExtentFromXml Read extent from XML if true or let provider determine it if false
*
*/ */
QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(), explicit QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true, const QString &providerLib = "ogr", const QgsVectorLayer::LayerOptions &options = QgsVectorLayer::LayerOptions() );
bool readExtentFromXml = false );
virtual ~QgsVectorLayer(); virtual ~QgsVectorLayer();

@ -108,7 +108,7 @@ QgsRasterLayer::QgsRasterLayer()
QgsRasterLayer::QgsRasterLayer( const QString &uri, QgsRasterLayer::QgsRasterLayer( const QString &uri,
const QString &baseName, const QString &baseName,
const QString &providerKey, const QString &providerKey,
bool loadDefaultStyleFlag ) const LayerOptions &options )
: QgsMapLayer( RasterLayer, baseName, uri ) : QgsMapLayer( RasterLayer, baseName, uri )
// Constant that signals property not used. // Constant that signals property not used.
, QSTRING_NOT_SET( QStringLiteral( "Not Set" ) ) , QSTRING_NOT_SET( QStringLiteral( "Not Set" ) )
@ -122,7 +122,7 @@ QgsRasterLayer::QgsRasterLayer( const QString &uri,
// load default style // load default style
bool defaultLoadedFlag = false; bool defaultLoadedFlag = false;
if ( mValid && loadDefaultStyleFlag ) if ( mValid && options.loadDefaultStyle )
{ {
loadDefaultStyle( defaultLoadedFlag ); loadDefaultStyle( defaultLoadedFlag );
} }

@ -166,6 +166,24 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
//! \brief Constructor. Provider is not set. //! \brief Constructor. Provider is not set.
QgsRasterLayer(); QgsRasterLayer();
/**
* Setting options for loading raster layers.
* \since QGIS 3.0
*/
struct LayerOptions
{
/**
* Constructor for LayerOptions.
*/
explicit LayerOptions( bool loadDefaultStyle = true )
: loadDefaultStyle( loadDefaultStyle )
{}
//! Set to true if the default layer style should be loaded
bool loadDefaultStyle = true;
};
/** /**
* \brief This is the constructor for the RasterLayer class. * \brief This is the constructor for the RasterLayer class.
* *
@ -183,10 +201,10 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
* *
* - * -
* */ * */
QgsRasterLayer( const QString &uri, explicit QgsRasterLayer( const QString &uri,
const QString &baseName = QString(), const QString &baseName = QString(),
const QString &providerKey = "gdal", const QString &providerKey = "gdal",
bool loadDefaultStyleFlag = true ); const QgsRasterLayer::LayerOptions &options = QgsRasterLayer::LayerOptions() );
~QgsRasterLayer(); ~QgsRasterLayer();

@ -229,7 +229,9 @@ QList<QgsOgrDbLayerInfo *> QgsOgrLayerItem::subLayers( const QString &path, cons
} }
} }
// Raster layers // Raster layers
QgsRasterLayer rlayer( path, QStringLiteral( "gdal_tmp" ), QStringLiteral( "gdal" ), false ); QgsRasterLayer::LayerOptions options;
options.loadDefaultStyle = false;
QgsRasterLayer rlayer( path, QStringLiteral( "gdal_tmp" ), QStringLiteral( "gdal" ), options );
if ( !rlayer.dataProvider()->subLayers( ).empty() ) if ( !rlayer.dataProvider()->subLayers( ).empty() )
{ {
const QStringList layers( rlayer.dataProvider()->subLayers( ) ); const QStringList layers( rlayer.dataProvider()->subLayers( ) );

@ -86,11 +86,11 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
pass pass
def test_CsvNoGeometry(self): def test_CsvNoGeometry(self):
l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
l2 = QgsVectorLayer("?layer_ref=" + l1.id(), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=" + l1.id(), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
QgsProject.instance().removeMapLayer(l1.id()) QgsProject.instance().removeMapLayer(l1.id())
@ -100,7 +100,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
source = QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no" source = QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no"
d = QgsVirtualLayerDefinition() d = QgsVirtualLayerDefinition()
d.addSource("t", source, "delimitedtext") d.addSource("t", source, "delimitedtext")
l = QgsVectorLayer(d.toString(), "vtab", "virtual", False) l = QgsVectorLayer(d.toString(), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
def test_source_escaping2(self): def test_source_escaping2(self):
@ -123,7 +123,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
source = "dbname='%s' table=\"test\" (geometry) sql=" % fn source = "dbname='%s' table=\"test\" (geometry) sql=" % fn
d = QgsVirtualLayerDefinition() d = QgsVirtualLayerDefinition()
d.addSource("t", source, "spatialite") d.addSource("t", source, "spatialite")
l = QgsVectorLayer(d.toString(), "vtab", "virtual", False) l = QgsVectorLayer(d.toString(), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
# the source contains ':' and single quotes # the source contains ':' and single quotes
@ -132,42 +132,42 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
source = "dbname='%s' table=\"test\" (geometry) sql=" % fn source = "dbname='%s' table=\"test\" (geometry) sql=" % fn
d = QgsVirtualLayerDefinition() d = QgsVirtualLayerDefinition()
d.addSource("t", source, "spatialite") d.addSource("t", source, "spatialite")
l = QgsVectorLayer(d.toString(), "vtab", "virtual", False) l = QgsVectorLayer(d.toString(), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
def test_DynamicGeometry(self): def test_DynamicGeometry(self):
l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/testextpt.txt")).toString() + "?type=csv&delimiter=%7C&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/testextpt.txt")).toString() + "?type=csv&delimiter=%7C&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
query = toPercent("select *,makepoint(x,y) as geom from vtab1") query = toPercent("select *,makepoint(x,y) as geom from vtab1")
l2 = QgsVectorLayer("?layer_ref=%s&query=%s&geometry=geom:point:0&uid=id" % (l1.id(), query), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=%s&query=%s&geometry=geom:point:0&uid=id" % (l1.id(), query), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
QgsProject.instance().removeMapLayer(l1) QgsProject.instance().removeMapLayer(l1)
def test_ShapefileWithGeometry(self): def test_ShapefileWithGeometry(self):
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", False) l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
# use a temporary file # use a temporary file
l2 = QgsVectorLayer("?layer_ref=" + l1.id(), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=" + l1.id(), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
l2 = QgsVectorLayer("?layer_ref=%s:nn" % l1.id(), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=%s:nn" % l1.id(), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
QgsProject.instance().removeMapLayer(l1.id()) QgsProject.instance().removeMapLayer(l1.id())
def test_Query(self): def test_Query(self):
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", False) l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
ref_sum = sum(f.attributes()[0] for f in l1.getFeatures()) ref_sum = sum(f.attributes()[0] for f in l1.getFeatures())
query = toPercent("SELECT * FROM vtab1") query = toPercent("SELECT * FROM vtab1")
l2 = QgsVectorLayer("?layer_ref=%s&geometry=geometry:3:4326&query=%s&uid=OBJECTID" % (l1.id(), query), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=%s&geometry=geometry:3:4326&query=%s&uid=OBJECTID" % (l1.id(), query), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 3) self.assertEqual(l2.dataProvider().wkbType(), 3)
@ -179,7 +179,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
self.assertEqual(ref_sum, ref_sum3) self.assertEqual(ref_sum, ref_sum3)
# the same, without specifying the geometry column name # the same, without specifying the geometry column name
l2 = QgsVectorLayer("?layer_ref=%s&query=%s&uid=OBJECTID" % (l1.id(), query), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=%s&query=%s&uid=OBJECTID" % (l1.id(), query), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 6) self.assertEqual(l2.dataProvider().wkbType(), 6)
ref_sum2 = sum(f.attributes()[0] for f in l2.getFeatures()) ref_sum2 = sum(f.attributes()[0] for f in l2.getFeatures())
@ -191,7 +191,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
# with two geometry columns # with two geometry columns
query = toPercent("SELECT *,geometry as geom FROM vtab1") query = toPercent("SELECT *,geometry as geom FROM vtab1")
l2 = QgsVectorLayer("?layer_ref=%s&query=%s&uid=OBJECTID&geometry=geom:3:4326" % (l1.id(), query), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=%s&query=%s&uid=OBJECTID&geometry=geom:3:4326" % (l1.id(), query), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 3) self.assertEqual(l2.dataProvider().wkbType(), 3)
ref_sum2 = sum(f.attributes()[0] for f in l2.getFeatures()) ref_sum2 = sum(f.attributes()[0] for f in l2.getFeatures())
@ -202,7 +202,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
self.assertEqual(ref_sum, ref_sum3) self.assertEqual(ref_sum, ref_sum3)
# with two geometry columns, but no geometry column specified (will take the first) # with two geometry columns, but no geometry column specified (will take the first)
l2 = QgsVectorLayer("?layer_ref=%s&query=%s&uid=OBJECTID" % (l1.id(), query), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=%s&query=%s&uid=OBJECTID" % (l1.id(), query), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 6) self.assertEqual(l2.dataProvider().wkbType(), 6)
ref_sum2 = sum(f.attributes()[0] for f in l2.getFeatures()) ref_sum2 = sum(f.attributes()[0] for f in l2.getFeatures())
@ -214,7 +214,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
# the same, without geometry # the same, without geometry
query = toPercent("SELECT * FROM ww") query = toPercent("SELECT * FROM ww")
l2 = QgsVectorLayer("?layer_ref=%s:ww&query=%s&uid=ObJeCtId&nogeometry" % (l1.id(), query), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=%s:ww&query=%s&uid=ObJeCtId&nogeometry" % (l1.id(), query), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 100) # NoGeometry self.assertEqual(l2.dataProvider().wkbType(), 100) # NoGeometry
ref_sum2 = sum(f.attributes()[0] for f in l2.getFeatures()) ref_sum2 = sum(f.attributes()[0] for f in l2.getFeatures())
@ -223,46 +223,46 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
self.assertEqual(ref_sum, ref_sum3) self.assertEqual(ref_sum, ref_sum3)
# check that it fails when a query has a wrong geometry column # check that it fails when a query has a wrong geometry column
l2 = QgsVectorLayer("?layer_ref=%s&query=%s&geometry=geo" % (l1.id(), query), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=%s&query=%s&geometry=geo" % (l1.id(), query), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), False) self.assertEqual(l2.isValid(), False)
QgsProject.instance().removeMapLayer(l1.id()) QgsProject.instance().removeMapLayer(l1.id())
def test_QueryUrlEncoding(self): def test_QueryUrlEncoding(self):
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", False) l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
query = toPercent("SELECT * FROM vtab1") query = toPercent("SELECT * FROM vtab1")
l2 = QgsVectorLayer("?layer_ref=%s&query=%s&uid=ObjectId&nogeometry" % (l1.id(), query), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=%s&query=%s&uid=ObjectId&nogeometry" % (l1.id(), query), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
QgsProject.instance().removeMapLayer(l1.id()) QgsProject.instance().removeMapLayer(l1.id())
def test_QueryTableName(self): def test_QueryTableName(self):
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", False) l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
query = toPercent("SELECT * FROM vt") query = toPercent("SELECT * FROM vt")
l2 = QgsVectorLayer("?layer_ref=%s:vt&query=%s&uid=ObJeCtId&nogeometry" % (l1.id(), query), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=%s:vt&query=%s&uid=ObJeCtId&nogeometry" % (l1.id(), query), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 100) # NoGeometry self.assertEqual(l2.dataProvider().wkbType(), 100) # NoGeometry
QgsProject.instance().removeMapLayer(l1.id()) QgsProject.instance().removeMapLayer(l1.id())
def test_Join(self): def test_Join(self):
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "points.shp"), "points", "ogr", False) l1 = QgsVectorLayer(os.path.join(self.testDataDir, "points.shp"), "points", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
l2 = QgsVectorLayer(os.path.join(self.testDataDir, "points_relations.shp"), "points_relations", "ogr", False) l2 = QgsVectorLayer(os.path.join(self.testDataDir, "points_relations.shp"), "points_relations", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
QgsProject.instance().addMapLayer(l2) QgsProject.instance().addMapLayer(l2)
ref_sum = sum(f.attributes()[1] for f in l2.getFeatures()) ref_sum = sum(f.attributes()[1] for f in l2.getFeatures())
# use a temporary file # use a temporary file
query = toPercent("select id,Pilots,vtab1.geometry from vtab1,vtab2 where intersects(vtab1.geometry,vtab2.geometry)") query = toPercent("select id,Pilots,vtab1.geometry from vtab1,vtab2 where intersects(vtab1.geometry,vtab2.geometry)")
l3 = QgsVectorLayer("?layer_ref=%s&layer_ref=%s&uid=id&query=%s&geometry=geometry:1:4326" % (l1.id(), l2.id(), query), "vtab", "virtual", False) l3 = QgsVectorLayer("?layer_ref=%s&layer_ref=%s&uid=id&query=%s&geometry=geometry:1:4326" % (l1.id(), l2.id(), query), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l3.isValid(), True) self.assertEqual(l3.isValid(), True)
self.assertEqual(l3.dataProvider().wkbType(), 1) self.assertEqual(l3.dataProvider().wkbType(), 1)
self.assertEqual(l3.dataProvider().fields().count(), 2) self.assertEqual(l3.dataProvider().fields().count(), 2)
@ -281,7 +281,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
(5, "MULTILINESTRING", "((0 0,1 0),(0 1,1 1))"), (5, "MULTILINESTRING", "((0 0,1 0),(0 1,1 1))"),
(6, "MULTIPOLYGON", "(((0 0,1 0,1 1,0 0)),((2 2,3 0,3 3,2 2)))")] (6, "MULTIPOLYGON", "(((0 0,1 0,1 1,0 0)),((2 2,3 0,3 3,2 2)))")]
for wkb_type, wkt_type, wkt in geo: for wkb_type, wkt_type, wkt in geo:
l = QgsVectorLayer("%s?crs=epsg:4326" % wkt_type, "m1", "memory", False) l = QgsVectorLayer("%s?crs=epsg:4326" % wkt_type, "m1", "memory", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
QgsProject.instance().addMapLayer(l) QgsProject.instance().addMapLayer(l)
@ -291,7 +291,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
f1.setGeometry(g) f1.setGeometry(g)
l.dataProvider().addFeatures([f1]) l.dataProvider().addFeatures([f1])
l2 = QgsVectorLayer("?layer_ref=%s" % l.id(), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=%s" % l.id(), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().featureCount(), 1) self.assertEqual(l2.dataProvider().featureCount(), 1)
self.assertEqual(l2.dataProvider().wkbType(), wkb_type) self.assertEqual(l2.dataProvider().wkbType(), wkb_type)
@ -300,17 +300,17 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
def test_embeddedLayer(self): def test_embeddedLayer(self):
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp")) source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
l = QgsVectorLayer("?layer=ogr:%s" % source, "vtab", "virtual", False) l = QgsVectorLayer("?layer=ogr:%s" % source, "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
l = QgsVectorLayer("?layer=ogr:%s:nn" % source, "vtab", "virtual", False) l = QgsVectorLayer("?layer=ogr:%s:nn" % source, "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
def test_filter_rect(self): def test_filter_rect(self):
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp")) source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
query = toPercent("select * from vtab where _search_frame_=BuildMbr(-2.10,49.38,-1.3,49.99,4326)") query = toPercent("select * from vtab where _search_frame_=BuildMbr(-2.10,49.38,-1.3,49.99,4326)")
l2 = QgsVectorLayer("?layer=ogr:%s:vtab&query=%s&uid=objectid" % (source, query), "vtab2", "virtual", False) l2 = QgsVectorLayer("?layer=ogr:%s:vtab&query=%s&uid=objectid" % (source, query), "vtab2", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().featureCount(), 1) self.assertEqual(l2.dataProvider().featureCount(), 1)
a = [fit.attributes()[4] for fit in l2.getFeatures()] a = [fit.attributes()[4] for fit in l2.getFeatures()]
@ -318,11 +318,11 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
def test_recursiveLayer(self): def test_recursiveLayer(self):
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp")) source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
l = QgsVectorLayer("?layer=ogr:%s" % source, "vtab", "virtual", False) l = QgsVectorLayer("?layer=ogr:%s" % source, "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
QgsProject.instance().addMapLayer(l) QgsProject.instance().addMapLayer(l)
l2 = QgsVectorLayer("?layer_ref=" + l.id(), "vtab2", "virtual", False) l2 = QgsVectorLayer("?layer_ref=" + l.id(), "vtab2", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
QgsProject.instance().removeMapLayer(l.id()) QgsProject.instance().removeMapLayer(l.id())
@ -331,17 +331,17 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
df = QgsVirtualLayerDefinition() df = QgsVirtualLayerDefinition()
df.addSource("vtab", os.path.join(self.testDataDir, "france_parts.shp"), "ogr") df.addSource("vtab", os.path.join(self.testDataDir, "france_parts.shp"), "ogr")
df.setGeometryWkbType(QgsWkbTypes.NoGeometry) df.setGeometryWkbType(QgsWkbTypes.NoGeometry)
l2 = QgsVectorLayer(df.toString(), "vtab2", "virtual", False) l2 = QgsVectorLayer(df.toString(), "vtab2", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 100) # NoGeometry self.assertEqual(l2.dataProvider().wkbType(), 100) # NoGeometry
def test_reopen(self): def test_reopen(self):
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp")) source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString() tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString()
l = QgsVectorLayer("%s?layer=ogr:%s:vtab" % (tmp, source), "vtab2", "virtual", False) l = QgsVectorLayer("%s?layer=ogr:%s:vtab" % (tmp, source), "vtab2", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
l2 = QgsVectorLayer(tmp, "tt", "virtual", False) l2 = QgsVectorLayer(tmp, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 6) self.assertEqual(l2.dataProvider().wkbType(), 6)
self.assertEqual(l2.dataProvider().featureCount(), 4) self.assertEqual(l2.dataProvider().featureCount(), 4)
@ -349,10 +349,10 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
def test_reopen2(self): def test_reopen2(self):
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp")) source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString() tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString()
l = QgsVectorLayer("%s?layer=ogr:%s:vtab&nogeometry" % (tmp, source), "vtab2", "virtual", False) l = QgsVectorLayer("%s?layer=ogr:%s:vtab&nogeometry" % (tmp, source), "vtab2", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
l2 = QgsVectorLayer(tmp, "tt", "virtual", False) l2 = QgsVectorLayer(tmp, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 100) self.assertEqual(l2.dataProvider().wkbType(), 100)
self.assertEqual(l2.dataProvider().featureCount(), 4) self.assertEqual(l2.dataProvider().featureCount(), 4)
@ -361,10 +361,10 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp")) source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString() tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString()
query = toPercent("SELECT * FROM vtab") query = toPercent("SELECT * FROM vtab")
l = QgsVectorLayer("%s?layer=ogr:%s:vtab&query=%s&uid=objectid&geometry=geometry:3:4326" % (tmp, source, query), "vtab2", "virtual", False) l = QgsVectorLayer("%s?layer=ogr:%s:vtab&query=%s&uid=objectid&geometry=geometry:3:4326" % (tmp, source, query), "vtab2", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
l2 = QgsVectorLayer(tmp, "tt", "virtual", False) l2 = QgsVectorLayer(tmp, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 3) self.assertEqual(l2.dataProvider().wkbType(), 3)
self.assertEqual(l2.dataProvider().featureCount(), 4) self.assertEqual(l2.dataProvider().featureCount(), 4)
@ -377,10 +377,10 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp")) source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString() tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString()
query = toPercent("SELECT * FROM vtab") query = toPercent("SELECT * FROM vtab")
l = QgsVectorLayer("%s?layer=ogr:%s:vtab&query=%s&uid=objectid&nogeometry" % (tmp, source, query), "vtab2", "virtual", False) l = QgsVectorLayer("%s?layer=ogr:%s:vtab&query=%s&uid=objectid&nogeometry" % (tmp, source, query), "vtab2", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
l2 = QgsVectorLayer(tmp, "tt", "virtual", False) l2 = QgsVectorLayer(tmp, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 100) self.assertEqual(l2.dataProvider().wkbType(), 100)
self.assertEqual(l2.dataProvider().featureCount(), 4) self.assertEqual(l2.dataProvider().featureCount(), 4)
@ -390,11 +390,11 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
self.assertEqual(suma, 3064.0) self.assertEqual(suma, 3064.0)
def test_refLayer(self): def test_refLayer(self):
l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
l2 = QgsVectorLayer("?layer_ref=" + l1.id(), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=" + l1.id(), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
# now delete the layer # now delete the layer
@ -403,14 +403,14 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
print((sum([f.id() for f in l2.getFeatures()]))) print((sum([f.id() for f in l2.getFeatures()])))
def test_refLayers(self): def test_refLayers(self):
l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
# cf qgis bug #12266 # cf qgis bug #12266
for i in range(10): for i in range(10):
q = toPercent("select * from t" + str(i)) q = toPercent("select * from t" + str(i))
l2 = QgsVectorLayer("?layer_ref=%s:t%d&query=%s&uid=id" % (l1.id(), i, q), "vtab", "virtual", False) l2 = QgsVectorLayer("?layer_ref=%s:t%d&query=%s&uid=id" % (l1.id(), i, q), "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
QgsProject.instance().addMapLayer(l2) QgsProject.instance().addMapLayer(l2)
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
s = sum([f.id() for f in l2.dataProvider().getFeatures()]) # NOQA s = sum([f.id() for f in l2.dataProvider().getFeatures()]) # NOQA
@ -418,18 +418,18 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
QgsProject.instance().removeMapLayer(l2.id()) QgsProject.instance().removeMapLayer(l2.id())
def test_refLayers2(self): def test_refLayers2(self):
l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
# referenced layers cannot be stored ! # referenced layers cannot be stored !
tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString() tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString()
l2 = QgsVectorLayer("%s?layer_ref=%s" % (tmp, l1.id()), "tt", "virtual", False) l2 = QgsVectorLayer("%s?layer_ref=%s" % (tmp, l1.id()), "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), False) self.assertEqual(l2.isValid(), False)
self.assertEqual("Cannot store referenced layers" in l2.dataProvider().error().message(), True) self.assertEqual("Cannot store referenced layers" in l2.dataProvider().error().message(), True)
def test_sql(self): def test_sql(self):
l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
@ -440,7 +440,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
self.assertEqual(s, 15) self.assertEqual(s, 15)
def test_sql2(self): def test_sql2(self):
l2 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", False) l2 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
QgsProject.instance().addMapLayer(l2) QgsProject.instance().addMapLayer(l2)
@ -497,20 +497,20 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
self.assertEqual(l5.dataProvider().featureCount(), 1) self.assertEqual(l5.dataProvider().featureCount(), 1)
def test_sql3(self): def test_sql3(self):
l2 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", False) l2 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
QgsProject.instance().addMapLayer(l2) QgsProject.instance().addMapLayer(l2)
# unnamed column # unnamed column
query = toPercent("SELECT count(*)") query = toPercent("SELECT count(*)")
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", False) l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l4.isValid(), True) self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().fields().at(0).name(), "count(*)") self.assertEqual(l4.dataProvider().fields().at(0).name(), "count(*)")
self.assertEqual(l4.dataProvider().fields().at(0).type(), QVariant.Int) self.assertEqual(l4.dataProvider().fields().at(0).type(), QVariant.Int)
def test_sql_field_types(self): def test_sql_field_types(self):
query = toPercent("SELECT 42 as t, 'ok'||'ok' as t2, GeomFromText('') as t3, 3.14*2 as t4") query = toPercent("SELECT 42 as t, 'ok'||'ok' as t2, GeomFromText('') as t3, 3.14*2 as t4")
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", False) l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l4.isValid(), True) self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().fields().at(0).name(), "t") self.assertEqual(l4.dataProvider().fields().at(0).name(), "t")
self.assertEqual(l4.dataProvider().fields().at(0).type(), QVariant.Int) self.assertEqual(l4.dataProvider().fields().at(0).type(), QVariant.Int)
@ -523,7 +523,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
# with type annotations # with type annotations
query = toPercent("SELECT '42.0' as t /*:real*/, 3 as t2/*:text */, GeomFromText('') as t3 /*:multiPoInT:4326 */, 3.14*2 as t4/*:int*/") query = toPercent("SELECT '42.0' as t /*:real*/, 3 as t2/*:text */, GeomFromText('') as t3 /*:multiPoInT:4326 */, 3.14*2 as t4/*:int*/")
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", False) l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l4.isValid(), True) self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().fields().at(0).name(), "t") self.assertEqual(l4.dataProvider().fields().at(0).name(), "t")
self.assertEqual(l4.dataProvider().fields().at(0).type(), QVariant.Double) self.assertEqual(l4.dataProvider().fields().at(0).type(), QVariant.Double)
@ -541,13 +541,13 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
# with type annotations and url options # with type annotations and url options
query = toPercent("SELECT 1 as id /*:int*/, geomfromtext('point(0 0)',4326) as geometry/*:point:4326*/") query = toPercent("SELECT 1 as id /*:int*/, geomfromtext('point(0 0)',4326) as geometry/*:point:4326*/")
l4 = QgsVectorLayer("?query=%s&geometry=geometry" % query, "tt", "virtual", False) l4 = QgsVectorLayer("?query=%s&geometry=geometry" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l4.isValid(), True) self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().wkbType(), 1) # point self.assertEqual(l4.dataProvider().wkbType(), 1) # point
# with type annotations and url options (2) # with type annotations and url options (2)
query = toPercent("SELECT 1 as id /*:int*/, 3.14 as f, geomfromtext('point(0 0)',4326) as geometry/*:point:4326*/") query = toPercent("SELECT 1 as id /*:int*/, 3.14 as f, geomfromtext('point(0 0)',4326) as geometry/*:point:4326*/")
l4 = QgsVectorLayer("?query=%s&geometry=geometry&field=id:text" % query, "tt", "virtual", False) l4 = QgsVectorLayer("?query=%s&geometry=geometry&field=id:text" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l4.isValid(), True) self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().fields().at(0).name(), "id") self.assertEqual(l4.dataProvider().fields().at(0).name(), "id")
self.assertEqual(l4.dataProvider().fields().at(0).type(), QVariant.String) self.assertEqual(l4.dataProvider().fields().at(0).type(), QVariant.String)
@ -557,40 +557,40 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
def test_sql3b(self): def test_sql3b(self):
query = toPercent("SELECT GeomFromText('POINT(0 0)') as geom") query = toPercent("SELECT GeomFromText('POINT(0 0)') as geom")
l4 = QgsVectorLayer("?query=%s&geometry=geom" % query, "tt", "virtual", False) l4 = QgsVectorLayer("?query=%s&geometry=geom" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l4.isValid(), True) self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().wkbType(), 1) self.assertEqual(l4.dataProvider().wkbType(), 1)
# forced geometry type # forced geometry type
query = toPercent("SELECT GeomFromText('POINT(0 0)') as geom") query = toPercent("SELECT GeomFromText('POINT(0 0)') as geom")
l4 = QgsVectorLayer("?query=%s&geometry=geom:point:0" % query, "tt", "virtual", False) l4 = QgsVectorLayer("?query=%s&geometry=geom:point:0" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l4.isValid(), True) self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().wkbType(), 1) self.assertEqual(l4.dataProvider().wkbType(), 1)
query = toPercent("SELECT CastToPoint(GeomFromText('POINT(0 0)')) as geom") query = toPercent("SELECT CastToPoint(GeomFromText('POINT(0 0)')) as geom")
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", False) l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l4.isValid(), True) self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().wkbType(), 1) self.assertEqual(l4.dataProvider().wkbType(), 1)
def test_sql4(self): def test_sql4(self):
l2 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", False) l2 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
QgsProject.instance().addMapLayer(l2) QgsProject.instance().addMapLayer(l2)
query = toPercent("SELECT OBJECTId from france_parts") query = toPercent("SELECT OBJECTId from france_parts")
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", False) l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l4.isValid(), True) self.assertEqual(l4.isValid(), True)
s = sum(f.attributes()[0] for f in l4.getFeatures()) s = sum(f.attributes()[0] for f in l4.getFeatures())
self.assertEqual(s, 10659) self.assertEqual(s, 10659)
def test_layer_name(self): def test_layer_name(self):
# test space and upper case # test space and upper case
l2 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "FranCe parts", "ogr", False) l2 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "FranCe parts", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
QgsProject.instance().addMapLayer(l2) QgsProject.instance().addMapLayer(l2)
query = toPercent('SELECT OBJECTId from "FranCe parts"') query = toPercent('SELECT OBJECTId from "FranCe parts"')
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", False) l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l4.isValid(), True) self.assertEqual(l4.isValid(), True)
s = sum(f.attributes()[0] for f in l4.getFeatures()) s = sum(f.attributes()[0] for f in l4.getFeatures())
self.assertEqual(s, 10659) self.assertEqual(s, 10659)
@ -598,14 +598,14 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
def test_encoding(self): def test_encoding(self):
# changes encoding on a shapefile (the only provider supporting setEncoding) # changes encoding on a shapefile (the only provider supporting setEncoding)
source = toPercent(os.path.join(self.testDataDir, "shp_latin1.dbf")) source = toPercent(os.path.join(self.testDataDir, "shp_latin1.dbf"))
l = QgsVectorLayer("?layer=ogr:%s:fp:latin1" % source, "vtab", "virtual", False) l = QgsVectorLayer("?layer=ogr:%s:fp:latin1" % source, "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
for f in l.getFeatures(): for f in l.getFeatures():
self.assertEqual(f.attributes()[1], "accents éàè") self.assertEqual(f.attributes()[1], "accents éàè")
# use UTF-8 now # use UTF-8 now
l = QgsVectorLayer("?layer=ogr:%s:fp:UTF-8" % source, "vtab", "virtual", False) l = QgsVectorLayer("?layer=ogr:%s:fp:UTF-8" % source, "vtab", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
for f in l.getFeatures(): for f in l.getFeatures():
self.assertEqual(f.attributes()[1], "accents \ufffd\ufffd\ufffd") # invalid unicode characters self.assertEqual(f.attributes()[1], "accents \ufffd\ufffd\ufffd") # invalid unicode characters
@ -613,7 +613,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
def test_rowid(self): def test_rowid(self):
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp")) source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
query = toPercent("select rowid as uid, * from vtab limit 1 offset 3") query = toPercent("select rowid as uid, * from vtab limit 1 offset 3")
l = QgsVectorLayer("?layer=ogr:%s:vtab&query=%s" % (source, query), "vtab2", "virtual", False) l = QgsVectorLayer("?layer=ogr:%s:vtab&query=%s" % (source, query), "vtab2", "virtual", QgsVectorLayer.LayerOptions(False))
# the last line must have a fixed rowid (not an autoincrement) # the last line must have a fixed rowid (not an autoincrement)
for f in l.getFeatures(): for f in l.getFeatures():
lid = f.attributes()[0] lid = f.attributes()[0]
@ -621,21 +621,21 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
def test_geometry_conversion(self): def test_geometry_conversion(self):
query = toPercent("select geomfromtext('multipoint((0 0),(1 1))') as geom") query = toPercent("select geomfromtext('multipoint((0 0),(1 1))') as geom")
l = QgsVectorLayer("?query=%s&geometry=geom:multipoint:0" % query, "tt", "virtual", False) l = QgsVectorLayer("?query=%s&geometry=geom:multipoint:0" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
for f in l.getFeatures(): for f in l.getFeatures():
self.assertEqual(f.geometry().exportToWkt().lower().startswith("multipoint"), True) self.assertEqual(f.geometry().exportToWkt().lower().startswith("multipoint"), True)
self.assertEqual("),(" in f.geometry().exportToWkt(), True) # has two points self.assertEqual("),(" in f.geometry().exportToWkt(), True) # has two points
query = toPercent("select geomfromtext('multipolygon(((0 0,1 0,1 1,0 1,0 0)),((0 1,1 1,1 2,0 2,0 1)))') as geom") query = toPercent("select geomfromtext('multipolygon(((0 0,1 0,1 1,0 1,0 0)),((0 1,1 1,1 2,0 2,0 1)))') as geom")
l = QgsVectorLayer("?query=%s&geometry=geom:multipolygon:0" % query, "tt", "virtual", False) l = QgsVectorLayer("?query=%s&geometry=geom:multipolygon:0" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
for f in l.getFeatures(): for f in l.getFeatures():
self.assertEqual(f.geometry().exportToWkt().lower().startswith("multipolygon"), True) self.assertEqual(f.geometry().exportToWkt().lower().startswith("multipolygon"), True)
self.assertEqual(")),((" in f.geometry().exportToWkt(), True) # has two polygons self.assertEqual(")),((" in f.geometry().exportToWkt(), True) # has two polygons
query = toPercent("select geomfromtext('multilinestring((0 0,1 0,1 1,0 1,0 0),(0 1,1 1,1 2,0 2,0 1))') as geom") query = toPercent("select geomfromtext('multilinestring((0 0,1 0,1 1,0 1,0 0),(0 1,1 1,1 2,0 2,0 1))') as geom")
l = QgsVectorLayer("?query=%s&geometry=geom:multilinestring:0" % query, "tt", "virtual", False) l = QgsVectorLayer("?query=%s&geometry=geom:multilinestring:0" % query, "tt", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l.isValid(), True) self.assertEqual(l.isValid(), True)
for f in l.getFeatures(): for f in l.getFeatures():
self.assertEqual(f.geometry().exportToWkt().lower().startswith("multilinestring"), True) self.assertEqual(f.geometry().exportToWkt().lower().startswith("multilinestring"), True)
@ -667,12 +667,12 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
def test_ProjectDependencies(self): def test_ProjectDependencies(self):
# make a virtual layer with living references and save it to a project # make a virtual layer with living references and save it to a project
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", False) l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
query = toPercent("SELECT * FROM france_parts") query = toPercent("SELECT * FROM france_parts")
l2 = QgsVectorLayer("?query=%s" % query, "aa", "virtual", False) l2 = QgsVectorLayer("?query=%s" % query, "aa", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l2.isValid(), True) self.assertEqual(l2.isValid(), True)
QgsProject.instance().addMapLayer(l2) QgsProject.instance().addMapLayer(l2)
@ -681,7 +681,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
self.assertEqual(ll0.layerId().startswith('france_parts'), True) self.assertEqual(ll0.layerId().startswith('france_parts'), True)
query = toPercent("SELECT t1.objectid, t2.name_0 FROM france_parts as t1, aa as t2") query = toPercent("SELECT t1.objectid, t2.name_0 FROM france_parts as t1, aa as t2")
l3 = QgsVectorLayer("?query=%s" % query, "bb", "virtual", False) l3 = QgsVectorLayer("?query=%s" % query, "bb", "virtual", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l3.isValid(), True) self.assertEqual(l3.isValid(), True)
QgsProject.instance().addMapLayer(l3) QgsProject.instance().addMapLayer(l3)
@ -735,7 +735,7 @@ class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):
self.assertEqual(ids, []) self.assertEqual(ids, [])
def test_layer_with_accents(self): def test_layer_with_accents(self):
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "françéà", "ogr", False) l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "françéà", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)