Skip to content
Permalink
Browse files
Add convenience method to directly convert QgsProviderSublayerDetails
object to a QgsMapLayer
  • Loading branch information
nyalldawson committed Jun 23, 2021
1 parent 00db282 commit 69debff405f44dc2eb8bc043745752c20e9fc263
@@ -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;
@@ -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
@@ -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 );
}
@@ -19,6 +19,7 @@
#include "qgis_core.h"
#include "qgis.h"
#include "qgswkbtypes.h"
#include "qgscoordinatetransformcontext.h"

#include <QString>
#include <QStringList>
@@ -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.
*
@@ -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
@@ -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.