Skip to content

Commit

Permalink
Add convenience method to directly convert QgsProviderSublayerDetails
Browse files Browse the repository at this point in the history
object to a QgsMapLayer
  • Loading branch information
nyalldawson committed Jun 23, 2021
1 parent 00db282 commit 69debff
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
Expand Up @@ -73,6 +73,24 @@ Returns the layer's URI.
Sets the layer's ``uri``.

.. seealso:: :py:func:`uri`
%End

struct LayerOptions
{

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

QgsCoordinateTransformContext transformContext;
};

QgsMapLayer *toLayer( const LayerOptions &options ) const /Factory/;
%Docstring
Creates a new :py:class:`QgsMapLayer` object associated with the sublayer.

Caller takes ownership of the returned layer.
%End

QString name() const;
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -215,6 +215,7 @@ set(QGIS_CORE_SRCS
providers/qgsdataprovider.cpp
providers/qgsprovidermetadata.cpp
providers/qgsproviderregistry.cpp
providers/qgsprovidersublayerdetails.cpp

providers/arcgis/qgsarcgisportalutils.cpp
providers/arcgis/qgsarcgisrestquery.cpp
Expand Down
27 changes: 27 additions & 0 deletions src/core/providers/qgsprovidersublayerdetails.cpp
@@ -0,0 +1,27 @@
/***************************************************************************
qgsprovidersublayerdetails.cpp
----------------------------
begin : May 2021
copyright : (C) 2021 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsprovidersublayerdetails.h"
#include "qgsmaplayerfactory.h"

QgsProviderSublayerDetails::LayerOptions::LayerOptions( const QgsCoordinateTransformContext &transformContext )
: transformContext( transformContext )
{}

QgsMapLayer *QgsProviderSublayerDetails::toLayer( const LayerOptions &options ) const
{
return QgsMapLayerFactory::createLayer( mUri, mName, mType, mProviderKey, options.transformContext );
}
22 changes: 22 additions & 0 deletions src/core/providers/qgsprovidersublayerdetails.h
Expand Up @@ -19,6 +19,7 @@
#include "qgis_core.h"
#include "qgis.h"
#include "qgswkbtypes.h"
#include "qgscoordinatetransformcontext.h"

#include <QString>
#include <QStringList>
Expand Down Expand Up @@ -86,6 +87,27 @@ class CORE_EXPORT QgsProviderSublayerDetails
*/
void setUri( const QString &uri ) { mUri = uri; }

/**
* Setting options for loading layers.
*/
struct LayerOptions
{

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

QgsCoordinateTransformContext transformContext;
};

/**
* Creates a new QgsMapLayer object associated with the sublayer.
*
* Caller takes ownership of the returned layer.
*/
QgsMapLayer *toLayer( const LayerOptions &options ) const SIP_FACTORY;

/**
* Returns the layer's name.
*
Expand Down
22 changes: 21 additions & 1 deletion tests/src/python/test_qgsprovidersublayerdetails.py
Expand Up @@ -11,15 +11,19 @@
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA
import os

from qgis.core import (
QgsProviderRegistry,
QgsMapLayerType,
QgsWkbTypes,
QgsProviderSublayerDetails,
Qgis
Qgis,
QgsCoordinateTransformContext,
QgsVectorLayer
)
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath

# Convenience instances in case you may need them
# to find the srs.db
Expand Down Expand Up @@ -65,6 +69,22 @@ def testGettersSetters(self):
d.setLayerNumber(13)
self.assertEqual(d.layerNumber(), 13)

def test_to_layer(self):
"""
Test converting sub layer details to a layer
"""
details = QgsProviderSublayerDetails()
details.setUri(os.path.join(unitTestDataPath(), 'lines.shp'))
details.setName('my sub layer')
details.setType(QgsMapLayerType.VectorLayer)
details.setProviderKey('ogr')

options = QgsProviderSublayerDetails.LayerOptions(QgsCoordinateTransformContext())
ml = details.toLayer(options)
self.assertTrue(ml.isValid())
self.assertIsInstance(ml, QgsVectorLayer)
self.assertEqual(ml.name(), 'my sub layer')


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

0 comments on commit 69debff

Please sign in to comment.