Skip to content
Permalink
Browse files
Rework QgsMapLayerFactory.createLayer API so that we can specify addi…
…tional options
  • Loading branch information
nyalldawson committed Jul 12, 2021
1 parent 665f6a5 commit 220bcb01142b832043e20b5250a2a1d7836e1b65
@@ -84,6 +84,8 @@ Constructor for LayerOptions with ``transformContext``.
%End

QgsCoordinateTransformContext transformContext;

bool loadDefaultStyle;
};

QgsMapLayer *toLayer( const LayerOptions &options ) const /Factory/;
@@ -43,8 +43,21 @@ Converts a map layer ``type`` to a string value.
.. seealso:: :py:func:`typeFromString`
%End

static QgsMapLayer *createLayer( const QString &uri, const QString &name, QgsMapLayerType type,
const QString &provider = QString(), const QgsCoordinateTransformContext &transformContext = QgsCoordinateTransformContext() ) /Factory/;
struct LayerOptions
{

explicit LayerOptions( const QgsCoordinateTransformContext &transformContext );
%Docstring
Constructor for LayerOptions with ``transformContext``.
%End

QgsCoordinateTransformContext transformContext;

bool loadDefaultStyle;
};

static QgsMapLayer *createLayer( const QString &uri, const QString &name, QgsMapLayerType type, const LayerOptions &options,
const QString &provider = QString() ) /Factory/;
%Docstring
Creates a map layer, given a ``uri``, ``name``, layer ``type`` and ``provider`` name.

@@ -5741,6 +5741,7 @@ QgsMapLayer *QgisApp::addSublayers( const QList<QgsProviderSublayerDetails> &lay
for ( const QgsProviderSublayerDetails &sublayer : std::as_const( sortedLayers ) )
{
QgsProviderSublayerDetails::LayerOptions options( QgsProject::instance()->transformContext() );
options.loadDefaultStyle = false;

std::unique_ptr<QgsMapLayer> layer( sublayer.toLayer( options ) );
if ( !layer )
@@ -5802,12 +5803,11 @@ void QgisApp::postProcessAddedLayer( QgsMapLayer *layer )
switch ( layer->type() )
{
case QgsMapLayerType::VectorLayer:
break;
case QgsMapLayerType::RasterLayer:
{
QgsRasterLayer *rasterLayer = qobject_cast< QgsRasterLayer *>( layer );
bool ok = false;
rasterLayer->loadDefaultMetadata( ok );
layer->loadDefaultStyle( ok );
layer->loadDefaultMetadata( ok );
break;
}

@@ -21,7 +21,9 @@

QgsMapLayer *QgsProviderSublayerDetails::toLayer( const LayerOptions &options ) const
{
return QgsMapLayerFactory::createLayer( mUri, mName, mType, mProviderKey, options.transformContext );
QgsMapLayerFactory::LayerOptions layerOptions( options.transformContext );
layerOptions.loadDefaultStyle = options.loadDefaultStyle;
return QgsMapLayerFactory::createLayer( mUri, mName, mType, layerOptions, mProviderKey );
}

bool QgsProviderSublayerDetails::operator==( const QgsProviderSublayerDetails &other ) const
@@ -100,7 +100,11 @@ class CORE_EXPORT QgsProviderSublayerDetails
: transformContext( transformContext )
{}

//! Coordinate transform context
QgsCoordinateTransformContext transformContext;

//! Set to TRUE if the default layer style should be loaded
bool loadDefaultStyle = true;
};

/**
@@ -67,42 +67,48 @@ QString QgsMapLayerFactory::typeToString( QgsMapLayerType type )
return QString();
}

QgsMapLayer *QgsMapLayerFactory::createLayer( const QString &uri, const QString &name, QgsMapLayerType type, const QString &provider, const QgsCoordinateTransformContext &transformContext )
QgsMapLayer *QgsMapLayerFactory::createLayer( const QString &uri, const QString &name, QgsMapLayerType type, const LayerOptions &options, const QString &provider )
{
switch ( type )
{
case QgsMapLayerType::VectorLayer:
{
QgsVectorLayer::LayerOptions options;
options.transformContext = transformContext;
return new QgsVectorLayer( uri, name, provider, options );
QgsVectorLayer::LayerOptions vectorOptions;
vectorOptions.transformContext = options.transformContext;
vectorOptions.loadDefaultStyle = options.loadDefaultStyle;
return new QgsVectorLayer( uri, name, provider, vectorOptions );
}

case QgsMapLayerType::RasterLayer:
{
QgsRasterLayer::LayerOptions options;
options.transformContext = transformContext;
return new QgsRasterLayer( uri, name, provider, options );
QgsRasterLayer::LayerOptions rasterOptions;
rasterOptions.transformContext = options.transformContext;
rasterOptions.loadDefaultStyle = options.loadDefaultStyle;
return new QgsRasterLayer( uri, name, provider, rasterOptions );
}

case QgsMapLayerType::MeshLayer:
{
QgsMeshLayer::LayerOptions options;
options.transformContext = transformContext;
return new QgsMeshLayer( uri, name, provider, options );
QgsMeshLayer::LayerOptions meshOptions;
meshOptions.transformContext = options.transformContext;
return new QgsMeshLayer( uri, name, provider, meshOptions );
}

case QgsMapLayerType::VectorTileLayer:
return new QgsVectorTileLayer( uri, name );

case QgsMapLayerType::AnnotationLayer:
return new QgsAnnotationLayer( name, QgsAnnotationLayer::LayerOptions( transformContext ) );
{
QgsAnnotationLayer::LayerOptions annotationOptions( options.transformContext );
return new QgsAnnotationLayer( name, annotationOptions );
}

case QgsMapLayerType::PointCloudLayer:
{
QgsPointCloudLayer::LayerOptions options;
options.transformContext = transformContext;
return new QgsPointCloudLayer( uri, name, provider, options );
QgsPointCloudLayer::LayerOptions pointCloudOptions;
pointCloudOptions.loadDefaultStyle = options.loadDefaultStyle;
pointCloudOptions.transformContext = options.transformContext;
return new QgsPointCloudLayer( uri, name, provider, pointCloudOptions );
}

case QgsMapLayerType::PluginLayer:
@@ -53,15 +53,37 @@ class CORE_EXPORT QgsMapLayerFactory
*/
static QString typeToString( QgsMapLayerType type );

/**
* Setting options for loading layers.
*
* \since QGIS 3.22
*/
struct LayerOptions
{

/**
* Constructor for LayerOptions with \a transformContext.
*/
explicit LayerOptions( const QgsCoordinateTransformContext &transformContext )
: transformContext( transformContext )
{}

//! Transform context
QgsCoordinateTransformContext transformContext;

//! Set to TRUE if the default layer style should be loaded
bool loadDefaultStyle = true;
};

/**
* Creates a map layer, given a \a uri, \a name, layer \a type and \a provider name.
*
* Caller takes ownership of the returned layer.
*
* \since QGIS 3.22
*/
static QgsMapLayer *createLayer( const QString &uri, const QString &name, QgsMapLayerType type,
const QString &provider = QString(), const QgsCoordinateTransformContext &transformContext = QgsCoordinateTransformContext() ) SIP_FACTORY;
static QgsMapLayer *createLayer( const QString &uri, const QString &name, QgsMapLayerType type, const LayerOptions &options,
const QString &provider = QString() ) SIP_FACTORY;

};

@@ -23,7 +23,8 @@
QgsPointCloudLayer,
QgsAnnotationLayer,
QgsVectorTileLayer,
QgsDataSourceUri
QgsDataSourceUri,
QgsCoordinateTransformContext
)
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath
@@ -77,31 +78,32 @@ def testTypeToString(self):

def testCreateLayer(self):
# create vector
ml = QgsMapLayerFactory.createLayer(os.path.join(unitTestDataPath(), 'lines.shp'), 'lines', QgsMapLayerType.VectorLayer, 'ogr')
options = QgsMapLayerFactory.LayerOptions(QgsCoordinateTransformContext())
ml = QgsMapLayerFactory.createLayer(os.path.join(unitTestDataPath(), 'lines.shp'), 'lines', QgsMapLayerType.VectorLayer, options, 'ogr')
self.assertTrue(ml.isValid())
self.assertIsInstance(ml, QgsVectorLayer)
self.assertEqual(ml.name(), 'lines')

# create raster
ml = QgsMapLayerFactory.createLayer(os.path.join(unitTestDataPath(), 'landsat.tif'), 'rl', QgsMapLayerType.RasterLayer, 'gdal')
ml = QgsMapLayerFactory.createLayer(os.path.join(unitTestDataPath(), 'landsat.tif'), 'rl', QgsMapLayerType.RasterLayer, options, 'gdal')
self.assertTrue(ml.isValid())
self.assertIsInstance(ml, QgsRasterLayer)
self.assertEqual(ml.name(), 'rl')

# create mesh
ml = QgsMapLayerFactory.createLayer(os.path.join(unitTestDataPath(), 'mesh', 'lines.2dm'), 'ml', QgsMapLayerType.MeshLayer, 'mdal')
ml = QgsMapLayerFactory.createLayer(os.path.join(unitTestDataPath(), 'mesh', 'lines.2dm'), 'ml', QgsMapLayerType.MeshLayer, options, 'mdal')
self.assertTrue(ml.isValid())
self.assertIsInstance(ml, QgsMeshLayer)
self.assertEqual(ml.name(), 'ml')

# create point cloud
ml = QgsMapLayerFactory.createLayer(os.path.join(unitTestDataPath(), 'point_clouds', 'ept', 'rgb', 'ept.json'), 'pcl', QgsMapLayerType.PointCloudLayer, 'ept')
ml = QgsMapLayerFactory.createLayer(os.path.join(unitTestDataPath(), 'point_clouds', 'ept', 'rgb', 'ept.json'), 'pcl', QgsMapLayerType.PointCloudLayer, options, 'ept')
self.assertTrue(ml.isValid())
self.assertIsInstance(ml, QgsPointCloudLayer)
self.assertEqual(ml.name(), 'pcl')

# annotation layer
ml = QgsMapLayerFactory.createLayer('', 'al', QgsMapLayerType.AnnotationLayer)
ml = QgsMapLayerFactory.createLayer('', 'al', QgsMapLayerType.AnnotationLayer, options)
self.assertTrue(ml.isValid())
self.assertIsInstance(ml, QgsAnnotationLayer)
self.assertEqual(ml.name(), 'al')
@@ -111,7 +113,7 @@ def testCreateLayer(self):
ds.setParam("type", "xyz")
ds.setParam("url", "file://{}/{{z}}-{{x}}-{{y}}.pbf".format(os.path.join(unitTestDataPath(), 'vector_tile')))
ds.setParam("zmax", "1")
ml = QgsMapLayerFactory.createLayer(ds.encodedUri().data().decode(), 'vtl', QgsMapLayerType.VectorTileLayer)
ml = QgsMapLayerFactory.createLayer(ds.encodedUri().data().decode(), 'vtl', QgsMapLayerType.VectorTileLayer, options)
self.assertTrue(ml.isValid())
self.assertIsInstance(ml, QgsVectorTileLayer)
self.assertEqual(ml.name(), 'vtl')

0 comments on commit 220bcb0

Please sign in to comment.