Skip to content

Commit

Permalink
Rework QgsMapLayerFactory.createLayer API so that we can specify addi…
Browse files Browse the repository at this point in the history
…tional options
  • Loading branch information
nyalldawson committed Jul 12, 2021
1 parent 665f6a5 commit 220bcb0
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 29 deletions.
Expand Up @@ -84,6 +84,8 @@ Constructor for LayerOptions with ``transformContext``.
%End

QgsCoordinateTransformContext transformContext;

bool loadDefaultStyle;
};

QgsMapLayer *toLayer( const LayerOptions &options ) const /Factory/;
Expand Down
17 changes: 15 additions & 2 deletions python/core/auto_generated/qgsmaplayerfactory.sip.in
Expand Up @@ -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.

Expand Down
6 changes: 3 additions & 3 deletions src/app/qgisapp.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion src/core/providers/qgsprovidersublayerdetails.cpp
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/core/providers/qgsprovidersublayerdetails.h
Expand Up @@ -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;
};

/**
Expand Down
34 changes: 20 additions & 14 deletions src/core/qgsmaplayerfactory.cpp
Expand Up @@ -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:
Expand Down
26 changes: 24 additions & 2 deletions src/core/qgsmaplayerfactory.h
Expand Up @@ -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;

};

Expand Down
16 changes: 9 additions & 7 deletions tests/src/python/test_qgsmaplayerfactory.py
Expand Up @@ -23,7 +23,8 @@
QgsPointCloudLayer,
QgsAnnotationLayer,
QgsVectorTileLayer,
QgsDataSourceUri
QgsDataSourceUri,
QgsCoordinateTransformContext
)
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit 220bcb0

Please sign in to comment.