Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 5e08626
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions python/core/qgsmaplayer.sip
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions python/core/raster/qgsrasterlayer.sip
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -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() ) );
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsmaplayer.h
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions src/core/raster/qgsrasterlayer.h
Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions src/gui/layertree/qgslayertreemapcanvasbridge.cpp
Expand Up @@ -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() );
Expand Down
6 changes: 6 additions & 0 deletions tests/src/core/testqgsrasterlayer.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -205,6 +206,11 @@ void TestQgsRasterLayer::isValid()
QVERIFY( render( "raster" ) );
}

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

void TestQgsRasterLayer::pseudoColor()
{
QgsRasterShader* rasterShader = new QgsRasterShader();
Expand Down
9 changes: 9 additions & 0 deletions tests/src/core/testqgsvectorlayer.cpp
Expand Up @@ -104,6 +104,7 @@ class TestQgsVectorLayer : public QObject
void uniqueValues();
void minimumValue();
void maximumValue();
void isSpatial();
};

void TestQgsVectorLayer::initTestCase()
Expand Down Expand Up @@ -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.