Skip to content
Permalink
Browse files

Add method to get QgsLayerItem::LayerType from a map layer

  • Loading branch information
nyalldawson committed Oct 30, 2018
1 parent f65c845 commit 5c5f2ed7e38f49a9cb38e16bbd24ebf31dd40c85
@@ -433,6 +433,13 @@ Item that represents a layer that can be opened with one of the providers
QgsMapLayer::LayerType mapLayerType() const;
%Docstring
Returns QgsMapLayer.LayerType
%End

static LayerType typeFromMapLayer( QgsMapLayer *layer );
%Docstring
Returns the layer item type corresponding to a :py:class:`QgsMapLayer` ``layer``.

.. versionadded:: 3.6
%End

QString uri() const;
@@ -45,6 +45,7 @@
#include "qgssettings.h"
#include "qgsanimatedicon.h"
#include "qgsproject.h"
#include "qgsvectorlayer.h"

// use GDAL VSI mechanism
#define CPL_SUPRESS_CPLUSPLUS //#spellok
@@ -636,6 +637,43 @@ QgsMapLayer::LayerType QgsLayerItem::mapLayerType() const
return QgsMapLayer::VectorLayer; // no warnings
}

QgsLayerItem::LayerType QgsLayerItem::typeFromMapLayer( QgsMapLayer *layer )
{
switch ( layer->type() )
{
case QgsMapLayer::VectorLayer:
{
switch ( qobject_cast< QgsVectorLayer * >( layer )->geometryType() )
{
case QgsWkbTypes::PointGeometry:
return Point;

case QgsWkbTypes::LineGeometry:
return Line;

case QgsWkbTypes::PolygonGeometry:
return Polygon;

case QgsWkbTypes::NullGeometry:
return TableLayer;

case QgsWkbTypes::UnknownGeometry:
return Vector;
}

return Vector; // no warnings
}

case QgsMapLayer::RasterLayer:
return Raster;
case QgsMapLayer::PluginLayer:
return Plugin;
case QgsMapLayer::MeshLayer:
return Mesh;
}
return Vector; // no warnings
}

QString QgsLayerItem::layerTypeAsString( QgsLayerItem::LayerType layerType )
{
static int enumIdx = staticMetaObject.indexOfEnumerator( "LayerType" );
@@ -463,6 +463,12 @@ class CORE_EXPORT QgsLayerItem : public QgsDataItem
//! Returns QgsMapLayer::LayerType
QgsMapLayer::LayerType mapLayerType() const;

/**
* Returns the layer item type corresponding to a QgsMapLayer \a layer.
* \since QGIS 3.6
*/
static LayerType typeFromMapLayer( QgsMapLayer *layer );

//! Returns layer uri or empty string if layer cannot be created
QString uri() const { return mUri; }

@@ -21,6 +21,8 @@
//qgis includes...
#include "qgsdataitem.h"
#include "qgsvectorlayer.h"
#include "qgsrasterlayer.h"
#include "qgsmeshlayer.h"
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgssettings.h"
@@ -44,10 +46,12 @@ class TestQgsDataItem : public QObject

void testValid();
void testDirItemChildren();
void testLayerItemType();

private:
QgsDirectoryItem *mDirItem = nullptr;
QString mScanItemsSetting;
QString mTestDataDir;
bool isValidDirItem( QgsDirectoryItem *item );
};

@@ -63,6 +67,9 @@ void TestQgsDataItem::initTestCase()
QgsApplication::initQgis();
QgsApplication::showSettings();

QString dataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
mTestDataDir = dataDir + '/';

// Set up the QgsSettings environment
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
@@ -176,5 +183,44 @@ void TestQgsDataItem::testDirItemChildren()
}
}

void TestQgsDataItem::testLayerItemType()
{
std::unique_ptr< QgsMapLayer > layer = qgis::make_unique< QgsVectorLayer >( mTestDataDir + "polys.shp",
QString(), QStringLiteral( "ogr" ) );
QVERIFY( layer->isValid() );
QCOMPARE( QgsLayerItem::typeFromMapLayer( layer.get() ), QgsLayerItem::Polygon );

layer = qgis::make_unique< QgsVectorLayer >( mTestDataDir + "points.shp",
QString(), QStringLiteral( "ogr" ) );
QVERIFY( layer->isValid() );
QCOMPARE( QgsLayerItem::typeFromMapLayer( layer.get() ), QgsLayerItem::Point );

layer = qgis::make_unique< QgsVectorLayer >( mTestDataDir + "lines.shp",
QString(), QStringLiteral( "ogr" ) );
QVERIFY( layer->isValid() );
QCOMPARE( QgsLayerItem::typeFromMapLayer( layer.get() ), QgsLayerItem::Line );

layer = qgis::make_unique< QgsVectorLayer >( mTestDataDir + "nonspatial.dbf",
QString(), QStringLiteral( "ogr" ) );
QVERIFY( layer->isValid() );
QCOMPARE( QgsLayerItem::typeFromMapLayer( layer.get() ), QgsLayerItem::TableLayer );

layer = qgis::make_unique< QgsVectorLayer >( mTestDataDir + "invalid.dbf",
QString(), QStringLiteral( "ogr" ) );
QCOMPARE( QgsLayerItem::typeFromMapLayer( layer.get() ), QgsLayerItem::Vector );

layer = qgis::make_unique< QgsRasterLayer >( mTestDataDir + "rgb256x256.png",
QString(), QStringLiteral( "gdal" ) );
QVERIFY( layer->isValid() );
QCOMPARE( QgsLayerItem::typeFromMapLayer( layer.get() ), QgsLayerItem::Raster );

layer = qgis::make_unique< QgsMeshLayer >( mTestDataDir + "mesh/quad_and_triangle.2dm",
QString(), QStringLiteral( "mdal" ) );
QVERIFY( layer->isValid() );
QCOMPARE( QgsLayerItem::typeFromMapLayer( layer.get() ), QgsLayerItem::Mesh );


}

QGSTEST_MAIN( TestQgsDataItem )
#include "testqgsdataitem.moc"

0 comments on commit 5c5f2ed

Please sign in to comment.
You can’t perform that action at this time.