Skip to content
Permalink
Browse files

Add QgsMapLayer::isSpatial() method

The current approach of testing !vl || vl->geometryType() != NoGeometry
is not intuitive and has been the source of 2 recent bugs.

Replacing these tests with the new isSpatial() function makes it
immediately obvious what is being tested. It also allows for
non-spatial plugin layers to be correctly handled by overriding
this method.
  • Loading branch information
nyalldawson committed Mar 16, 2016
1 parent 7de0757 commit 5e0862605144f212782e73e377550c8358bacfc6
@@ -273,6 +273,11 @@ class QgsMapLayer : QObject
/** True if the layer can be edited */
virtual bool isEditable() const;

/** Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated with it.
* @note added in QGIS 2.16
*/
virtual bool isSpatial() const;

/** Sets state from Dom document
@param layerElement The Dom element corresponding to ``maplayer'' tag
@note
@@ -975,6 +975,8 @@ class QgsVectorLayer : QgsMapLayer
/** Returns true if the provider is in editing mode */
virtual bool isEditable() const;

virtual bool isSpatial() const;

/** Returns true if the provider is in read-only mode */
virtual bool isReadOnly() const;

@@ -144,6 +144,8 @@ class QgsRasterLayer : QgsMapLayer
/** Returns a list with classification items (Text and color) */
QList< QPair< QString, QColor > > legendSymbologyItems() const;

virtual bool isSpatial() const;

/** \brief Obtain GDAL Metadata for this layer */
QString metadata();

@@ -101,7 +101,7 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
// duplicate layer
QAction* duplicateLayersAction = menu->addAction( QgsApplication::getThemeIcon( "/mActionDuplicateLayer.svg" ), tr( "&Duplicate" ), QgisApp::instance(), SLOT( duplicateLayers() ) );

if ( !vlayer || vlayer->geometryType() != QGis::NoGeometry )
if ( layer->isSpatial() )
{
// set layer scale visibility
menu->addAction( tr( "&Set Layer Scale Visibility" ), QgisApp::instance(), SLOT( setLayerScaleVisibility() ) );
@@ -289,6 +289,11 @@ class CORE_EXPORT QgsMapLayer : public QObject
/** True if the layer can be edited */
virtual bool isEditable() const;

/** Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated with it.
* @note added in QGIS 2.16
*/
virtual bool isSpatial() const { return true; }

/** Sets state from Dom document
@param layerElement The Dom element corresponding to ``maplayer'' tag
@note
@@ -2618,6 +2618,11 @@ bool QgsVectorLayer::isEditable() const
return ( mEditBuffer && mDataProvider );
}

bool QgsVectorLayer::isSpatial() const
{
return geometryType() != QGis::NoGeometry;
}

bool QgsVectorLayer::isReadOnly() const
{
return mReadOnly;
@@ -1088,6 +1088,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Returns true if the provider is in editing mode */
virtual bool isEditable() const override;

virtual bool isSpatial() const override;

This comment has been minimized.

Copy link
@3nids

3nids Mar 17, 2016

Member

that makes hasGeometryType redundant. Do you want to deprecate it?


/** Returns true if the provider is in read-only mode */
virtual bool isReadOnly() const;

@@ -280,6 +280,8 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
/** Returns a list with classification items (Text and color) */
QgsLegendColorList legendSymbologyItems() const;

virtual bool isSpatial() const override { return true; }

/** \brief Obtain GDAL Metadata for this layer */
QString metadata() override;

@@ -143,8 +143,7 @@ void QgsLayerTreeMapCanvasBridge::setCanvasLayers()
if ( !layerNode->layer() )
continue;

QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layerNode->layer() );
if ( !vl || vl->geometryType() != QGis::NoGeometry )
if ( layerNode->layer()->isSpatial() )
{
mCanvas->setDestinationCrs( layerNode->layer()->crs() );
mCanvas->setMapUnits( layerNode->layer()->crs().mapUnits() );
@@ -69,6 +69,7 @@ class TestQgsRasterLayer : public QObject
void cleanup() {} // will be called after every testfunction.

void isValid();
void isSpatial();
void pseudoColor();
void colorRamp1();
void colorRamp2();
@@ -205,6 +206,11 @@ void TestQgsRasterLayer::isValid()
QVERIFY( render( "raster" ) );
}

void TestQgsRasterLayer::isSpatial()
{
QVERIFY( mpRasterLayer->isSpatial() );
}

void TestQgsRasterLayer::pseudoColor()
{
QgsRasterShader* rasterShader = new QgsRasterShader();
@@ -104,6 +104,7 @@ class TestQgsVectorLayer : public QObject
void uniqueValues();
void minimumValue();
void maximumValue();
void isSpatial();
};

void TestQgsVectorLayer::initTestCase()
@@ -341,5 +342,13 @@ void TestQgsVectorLayer::maximumValue()
QCOMPARE( vLayer->maximumValue( 1000 ), QVariant() );
}

void TestQgsVectorLayer::isSpatial()
{
QVERIFY( mpPointsLayer->isSpatial() );
QVERIFY( mpPolysLayer->isSpatial() );
QVERIFY( mpLinesLayer->isSpatial() );
QVERIFY( !mpNonSpatialLayer->isSpatial() );
}

QTEST_MAIN( TestQgsVectorLayer )
#include "testqgsvectorlayer.moc"

0 comments on commit 5e08626

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