Skip to content

Commit 3103b00

Browse files
authoredFeb 1, 2019
fix identify for mesh layers (#9047)
1 parent 4495699 commit 3103b00

File tree

6 files changed

+285
-26
lines changed

6 files changed

+285
-26
lines changed
 

‎python/gui/auto_generated/qgsmaptoolidentify.sip.in

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ after selecting a point, performs the identification:
3939
{
4040
VectorLayer,
4141
RasterLayer,
42+
MeshLayer,
4243
AllLayers
4344
};
4445
typedef QFlags<QgsMapToolIdentify::Type> LayerType;
@@ -103,9 +104,9 @@ this has been made private and two publics methods are offered
103104
:param x: x coordinates of mouseEvent
104105
:param y: y coordinates of mouseEvent
105106
:param mode: Identification mode. Can use Qgis default settings or a defined mode.
106-
:param layerType: Only performs identification in a certain type of layers (raster, vector). Default value is AllLayers.
107+
:param layerType: Only performs identification in a certain type of layers (raster, vector, mesh). Default value is AllLayers.
107108

108-
:return: a list of IdentifyResult*
109+
:return: a list of IdentifyResult
109110
%End
110111

111112
QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, LayerType layerType );
@@ -144,9 +145,9 @@ this has been made private and two publics methods are offered
144145
:param y: y coordinates of mouseEvent
145146
:param mode: Identification mode. Can use Qgis default settings or a defined mode.
146147
:param layerList: Performs the identification within the given list of layers.
147-
:param layerType: Only performs identification in a certain type of layers (raster, vector).
148+
:param layerType: Only performs identification in a certain type of layers (raster, vector, mesh).
148149

149-
:return: a list of IdentifyResult*
150+
:return: a list of IdentifyResult
150151
%End
151152

152153

@@ -158,6 +159,15 @@ Call the right method depending on layer type
158159
bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, QgsPointXY point, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
159160
bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsPointXY &point );
160161

162+
bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsPointXY &point );
163+
%Docstring
164+
Identifies data from active scalar and vector dataset from the mesh layer
165+
166+
Works only if layer was already rendered (triangular mesh is created)
167+
168+
.. versionadded:: 3.6
169+
%End
170+
161171
QMap< QString, QString > derivedAttributesForPoint( const QgsPoint &point );
162172
%Docstring
163173
Returns derived attributes map for a clicked point in map coordinates. May be 2D or 3D point.

‎src/app/qgsidentifyresultsdialog.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "qgsmapcanvas.h"
3434
#include "qgsmaplayeractionregistry.h"
3535
#include "qgsmaplayer.h"
36+
#include "qgsmeshlayer.h"
3637
#include "qgsnetworkaccessmanager.h"
3738
#include "qgsproject.h"
3839
#include "qgsrasterdataprovider.h"
@@ -476,6 +477,9 @@ void QgsIdentifyResultsDialog::addFeature( const QgsMapToolIdentify::IdentifyRes
476477
break;
477478

478479
case QgsMapLayer::MeshLayer:
480+
addFeature( qobject_cast<QgsMeshLayer *>( result.mLayer ), result.mLabel, result.mAttributes, result.mDerivedAttributes );
481+
break;
482+
479483
case QgsMapLayer::PluginLayer:
480484
break;
481485
}
@@ -923,6 +927,49 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
923927
}
924928
}
925929

930+
void QgsIdentifyResultsDialog::addFeature( QgsMeshLayer *layer,
931+
const QString &label,
932+
const QMap< QString, QString > &attributes,
933+
const QMap< QString, QString > &derivedAttributes )
934+
{
935+
QTreeWidgetItem *layItem = layerItem( layer );
936+
937+
if ( !layItem )
938+
{
939+
layItem = new QTreeWidgetItem( QStringList() << QString::number( lstResults->topLevelItemCount() ) << layer->name() );
940+
layItem->setData( 0, Qt::UserRole, QVariant::fromValue( qobject_cast<QObject *>( layer ) ) );
941+
942+
lstResults->addTopLevelItem( layItem );
943+
connect( layer, &QObject::destroyed, this, &QgsIdentifyResultsDialog::layerDestroyed );
944+
connect( layer, &QgsMapLayer::crsChanged, this, &QgsIdentifyResultsDialog::layerDestroyed );
945+
}
946+
947+
QgsIdentifyResultsFeatureItem *featItem = new QgsIdentifyResultsFeatureItem( QgsFields(),
948+
QgsFeature(),
949+
layer->crs(),
950+
QStringList() << label << QString() );
951+
layItem->addChild( featItem );
952+
953+
// attributes
954+
for ( QMap<QString, QString>::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
955+
{
956+
featItem->addChild( new QTreeWidgetItem( QStringList() << it.key() << it.value() ) );
957+
}
958+
959+
if ( derivedAttributes.size() >= 0 )
960+
{
961+
QgsTreeWidgetItem *derivedItem = new QgsTreeWidgetItem( QStringList() << tr( "(Derived)" ) );
962+
derivedItem->setData( 0, Qt::UserRole, "derived" );
963+
derivedItem->setAlwaysOnTopPriority( 0 );
964+
featItem->addChild( derivedItem );
965+
966+
for ( QMap< QString, QString>::const_iterator it = derivedAttributes.begin(); it != derivedAttributes.end(); ++it )
967+
{
968+
derivedItem->addChild( new QTreeWidgetItem( QStringList() << it.key() << it.value() ) );
969+
}
970+
}
971+
}
972+
926973
void QgsIdentifyResultsDialog::editingToggled()
927974
{
928975
QTreeWidgetItem *layItem = layerItem( sender() );
@@ -1397,6 +1444,14 @@ QgsRasterLayer *QgsIdentifyResultsDialog::rasterLayer( QTreeWidgetItem *item )
13971444
return qobject_cast<QgsRasterLayer *>( item->data( 0, Qt::UserRole ).value<QObject *>() );
13981445
}
13991446

1447+
QgsMeshLayer *QgsIdentifyResultsDialog::meshLayer( QTreeWidgetItem *item )
1448+
{
1449+
item = layerItem( item );
1450+
if ( !item )
1451+
return nullptr;
1452+
return qobject_cast<QgsMeshLayer *>( item->data( 0, Qt::UserRole ).value<QObject *>() );
1453+
}
1454+
14001455
QTreeWidgetItem *QgsIdentifyResultsDialog::retrieveAttributes( QTreeWidgetItem *item, QgsAttributeMap &attributes, int &idx )
14011456
{
14021457
QTreeWidgetItem *featItem = featureItem( item );

‎src/app/qgsidentifyresultsdialog.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class QgsVectorLayer;
4545
class QgsRasterLayer;
4646
class QgsHighlight;
4747
class QgsMapCanvas;
48+
class QgsMeshLayer;
4849
class QgsDockWidget;
4950
class QgsMapLayerAction;
5051
class QgsEditorWidgetSetup;
@@ -123,18 +124,21 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
123124

124125
public:
125126

126-
//! Constructor - takes it own copy of the QgsAttributeAction so
127-
// that it is independent of whoever created it.
127+
/**
128+
* Constructor -
129+
* takes its own copy of the QgsAttributeAction so
130+
* that it is independent of whoever created it.
131+
*/
128132
QgsIdentifyResultsDialog( QgsMapCanvas *canvas, QWidget *parent = nullptr, Qt::WindowFlags f = nullptr );
129133

130134
~QgsIdentifyResultsDialog() override;
131135

132-
//! Add add feature from vector layer
136+
//! Adds feature from vector layer
133137
void addFeature( QgsVectorLayer *layer,
134138
const QgsFeature &f,
135139
const QMap< QString, QString > &derivedAttributes );
136140

137-
//! Add add feature from other layer
141+
//! Adds feature from raster layer
138142
void addFeature( QgsRasterLayer *layer,
139143
const QString &label,
140144
const QMap< QString, QString > &attributes,
@@ -143,7 +147,16 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
143147
const QgsFeature &feature = QgsFeature(),
144148
const QMap<QString, QVariant> &params = ( QMap<QString, QVariant>() ) );
145149

146-
//! Add feature from identify results
150+
/**
151+
* Adds results from mesh layer
152+
* \since QGIS 3.6
153+
*/
154+
void addFeature( QgsMeshLayer *layer,
155+
const QString &label,
156+
const QMap< QString, QString > &attributes,
157+
const QMap< QString, QString > &derivedAttributes );
158+
159+
//! Adds feature from identify results
147160
void addFeature( const QgsMapToolIdentify::IdentifyResult &result );
148161

149162
//! Map tool was deactivated
@@ -261,6 +274,7 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
261274
QgsMapLayer *layer( QTreeWidgetItem *item );
262275
QgsVectorLayer *vectorLayer( QTreeWidgetItem *item );
263276
QgsRasterLayer *rasterLayer( QTreeWidgetItem *item );
277+
QgsMeshLayer *meshLayer( QTreeWidgetItem *item );
264278
QTreeWidgetItem *featureItem( QTreeWidgetItem *item );
265279
QTreeWidgetItem *layerItem( QTreeWidgetItem *item );
266280
QTreeWidgetItem *layerItem( QObject *layer );

‎src/gui/qgsmaptoolidentify.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "qgsmaptoolidentify.h"
2828
#include "qgsmaptopixel.h"
2929
#include "qgsmessageviewer.h"
30+
#include "qgsmeshlayer.h"
3031
#include "qgsmaplayer.h"
3132
#include "qgsrasterdataprovider.h"
3233
#include "qgsrasterlayer.h"
@@ -214,6 +215,10 @@ bool QgsMapToolIdentify::identifyLayer( QList<IdentifyResult> *results, QgsMapLa
214215
{
215216
return identifyVectorLayer( results, qobject_cast<QgsVectorLayer *>( layer ), geometry );
216217
}
218+
else if ( layer->type() == QgsMapLayer::MeshLayer && layerType.testFlag( MeshLayer ) )
219+
{
220+
return identifyMeshLayer( results, qobject_cast<QgsMeshLayer *>( layer ), geometry );
221+
}
217222
else
218223
{
219224
return false;
@@ -225,6 +230,89 @@ bool QgsMapToolIdentify::identifyVectorLayer( QList<QgsMapToolIdentify::Identify
225230
return identifyVectorLayer( results, layer, QgsGeometry::fromPointXY( point ) );
226231
}
227232

233+
bool QgsMapToolIdentify::identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsGeometry &geometry )
234+
{
235+
const QgsPointXY point = geometry.asPoint(); // mesh layers currently only support identification by point
236+
return identifyMeshLayer( results, layer, point );
237+
}
238+
239+
bool QgsMapToolIdentify::identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsPointXY &point )
240+
{
241+
QgsDebugMsgLevel( "point = " + point.toString(), 4 );
242+
if ( !layer || !layer->dataProvider() )
243+
return false;
244+
245+
const QgsMeshRendererSettings rendererSettings = layer->rendererSettings();
246+
const QgsMeshDatasetIndex scalarDatasetIndex = rendererSettings.activeScalarDataset();
247+
const QgsMeshDatasetIndex vectorDatasetIndex = rendererSettings.activeVectorDataset();
248+
if ( ! scalarDatasetIndex.isValid() && ! vectorDatasetIndex.isValid() )
249+
return false;
250+
251+
QMap< QString, QString > scalarAttributes, vectorAttributes;
252+
253+
QString scalarGroup;
254+
if ( scalarDatasetIndex.isValid() )
255+
{
256+
scalarGroup = layer->dataProvider()->datasetGroupMetadata( scalarDatasetIndex.group() ).name();
257+
258+
const QgsMeshDatasetValue scalarValue = layer->datasetValue( scalarDatasetIndex, point );
259+
const double scalar = scalarValue.scalar();
260+
if ( std::isnan( scalar ) )
261+
scalarAttributes.insert( tr( "Scalar Value" ), tr( "no data" ) );
262+
else
263+
scalarAttributes.insert( tr( "Scalar Value" ), QString::number( scalar ) );
264+
}
265+
266+
QString vectorGroup;
267+
if ( vectorDatasetIndex.isValid() )
268+
{
269+
vectorGroup = layer->dataProvider()->datasetGroupMetadata( vectorDatasetIndex.group() ).name();
270+
271+
const QgsMeshDatasetValue vectorValue = layer->datasetValue( vectorDatasetIndex, point );
272+
const double vectorX = vectorValue.x();
273+
const double vectorY = vectorValue.y();
274+
275+
if ( std::isnan( vectorX ) || std::isnan( vectorY ) )
276+
vectorAttributes.insert( tr( "Vector Value" ), tr( "no data" ) );
277+
else
278+
{
279+
vectorAttributes.insert( tr( "Vector Magnitude" ), QString::number( vectorValue.scalar() ) );
280+
vectorAttributes.insert( tr( "Vector x-component" ), QString::number( vectorY ) );
281+
vectorAttributes.insert( tr( "Vector y-component" ), QString::number( vectorX ) );
282+
}
283+
}
284+
285+
const QMap< QString, QString > derivedAttributes = derivedAttributesForPoint( QgsPoint( point ) );
286+
if ( scalarGroup == vectorGroup )
287+
{
288+
const IdentifyResult result( qobject_cast<QgsMapLayer *>( layer ),
289+
scalarGroup,
290+
vectorAttributes,
291+
derivedAttributes );
292+
results->append( result );
293+
}
294+
else
295+
{
296+
if ( !scalarGroup.isEmpty() )
297+
{
298+
const IdentifyResult result( qobject_cast<QgsMapLayer *>( layer ),
299+
scalarGroup,
300+
scalarAttributes,
301+
derivedAttributes );
302+
results->append( result );
303+
}
304+
if ( !vectorGroup.isEmpty() )
305+
{
306+
const IdentifyResult result( qobject_cast<QgsMapLayer *>( layer ),
307+
vectorGroup,
308+
vectorAttributes,
309+
derivedAttributes );
310+
results->append( result );
311+
}
312+
}
313+
return true;
314+
}
315+
228316
QMap<QString, QString> QgsMapToolIdentify::derivedAttributesForPoint( const QgsPoint &point )
229317
{
230318
QMap< QString, QString > derivedAttributes;

‎src/gui/qgsmaptoolidentify.h

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class QgsRasterLayer;
3030
class QgsVectorLayer;
3131
class QgsMapLayer;
3232
class QgsMapCanvas;
33+
class QgsMeshLayer;
3334
class QgsHighlight;
3435
class QgsIdentifyMenu;
3536
class QgsDistanceArea;
@@ -63,7 +64,8 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
6364
{
6465
VectorLayer = 1,
6566
RasterLayer = 2,
66-
AllLayers = VectorLayer | RasterLayer
67+
MeshLayer = 4, //!< \since QGIS 3.6
68+
AllLayers = VectorLayer | RasterLayer | MeshLayer
6769
};
6870
Q_DECLARE_FLAGS( LayerType, Type )
6971
Q_FLAG( LayerType )
@@ -114,13 +116,14 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
114116

115117
/**
116118
* Performs the identification.
117-
To avoid being forced to specify IdentifyMode with a list of layers
118-
this has been made private and two publics methods are offered
119-
\param x x coordinates of mouseEvent
120-
\param y y coordinates of mouseEvent
121-
\param mode Identification mode. Can use Qgis default settings or a defined mode.
122-
\param layerType Only performs identification in a certain type of layers (raster, vector). Default value is AllLayers.
123-
\returns a list of IdentifyResult*/
119+
* To avoid being forced to specify IdentifyMode with a list of layers
120+
* this has been made private and two publics methods are offered
121+
* \param x x coordinates of mouseEvent
122+
* \param y y coordinates of mouseEvent
123+
* \param mode Identification mode. Can use Qgis default settings or a defined mode.
124+
* \param layerType Only performs identification in a certain type of layers (raster, vector, mesh). Default value is AllLayers.
125+
* \returns a list of IdentifyResult
126+
*/
124127
QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );
125128

126129
//! Performs identification based on a geometry (in map coordinates)
@@ -147,14 +150,15 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
147150

148151
/**
149152
* Performs the identification.
150-
To avoid being forced to specify IdentifyMode with a list of layers
151-
this has been made private and two publics methods are offered
152-
\param x x coordinates of mouseEvent
153-
\param y y coordinates of mouseEvent
154-
\param mode Identification mode. Can use Qgis default settings or a defined mode.
155-
\param layerList Performs the identification within the given list of layers.
156-
\param layerType Only performs identification in a certain type of layers (raster, vector).
157-
\returns a list of IdentifyResult*/
153+
* To avoid being forced to specify IdentifyMode with a list of layers
154+
* this has been made private and two publics methods are offered
155+
* \param x x coordinates of mouseEvent
156+
* \param y y coordinates of mouseEvent
157+
* \param mode Identification mode. Can use Qgis default settings or a defined mode.
158+
* \param layerList Performs the identification within the given list of layers.
159+
* \param layerType Only performs identification in a certain type of layers (raster, vector, mesh).
160+
* \returns a list of IdentifyResult
161+
*/
158162
QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType = AllLayers );
159163

160164
QgsIdentifyMenu *mIdentifyMenu = nullptr;
@@ -165,6 +169,14 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
165169
bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, QgsPointXY point, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
166170
bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsPointXY &point );
167171

172+
/**
173+
* Identifies data from active scalar and vector dataset from the mesh layer
174+
*
175+
* Works only if layer was already rendered (triangular mesh is created)
176+
* \since QGIS 3.6
177+
*/
178+
bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsPointXY &point );
179+
168180
//! Returns derived attributes map for a clicked point in map coordinates. May be 2D or 3D point.
169181
QMap< QString, QString > derivedAttributesForPoint( const QgsPoint &point );
170182

@@ -194,6 +206,7 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
194206
bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers );
195207
bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
196208
bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsGeometry &geometry );
209+
bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsGeometry &geometry );
197210

198211
/**
199212
* Desired units for distance display.

‎tests/src/app/testqgsmaptoolidentifyaction.cpp

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgsvectordataprovider.h"
2424
#include "qgsproject.h"
2525
#include "qgsmapcanvas.h"
26+
#include "qgsmeshlayer.h"
2627
#include "qgsunittypes.h"
2728
#include "qgsmaptoolidentifyaction.h"
2829
#include "qgssettings.h"
@@ -54,6 +55,7 @@ class TestQgsMapToolIdentifyAction : public QObject
5455
void areaCalculation(); //test calculation of derived area attribute
5556
void identifyRasterFloat32(); // test pixel identification and decimal precision
5657
void identifyRasterFloat64(); // test pixel identification and decimal precision
58+
void identifyMesh(); // test identification for mesh layer
5759
void identifyInvalidPolygons(); // test selecting invalid polygons
5860
void clickxy(); // test if clicked_x and clicked_y variables are propagated
5961
void closestPoint();
@@ -67,6 +69,7 @@ class TestQgsMapToolIdentifyAction : public QObject
6769

6870
QString testIdentifyRaster( QgsRasterLayer *layer, double xGeoref, double yGeoref );
6971
QList<QgsMapToolIdentify::IdentifyResult> testIdentifyVector( QgsVectorLayer *layer, double xGeoref, double yGeoref );
72+
QList<QgsMapToolIdentify::IdentifyResult> testIdentifyMesh( QgsMeshLayer *layer, double xGeoref, double yGeoref );
7073

7174
// Release return with delete []
7275
unsigned char *
@@ -89,7 +92,6 @@ class TestQgsMapToolIdentifyAction : public QObject
8992
geom.fromWkb( wkb, wkbsize );
9093
return geom;
9194
}
92-
9395
};
9496

9597
void TestQgsMapToolIdentifyAction::initTestCase()
@@ -502,6 +504,15 @@ QString TestQgsMapToolIdentifyAction::testIdentifyRaster( QgsRasterLayer *layer,
502504
return result[0].mAttributes[QStringLiteral( "Band 1" )];
503505
}
504506

507+
// private
508+
QList<QgsMapToolIdentify::IdentifyResult> TestQgsMapToolIdentifyAction::testIdentifyMesh( QgsMeshLayer *layer, double xGeoref, double yGeoref )
509+
{
510+
std::unique_ptr< QgsMapToolIdentifyAction > action( new QgsMapToolIdentifyAction( canvas ) );
511+
QgsPointXY mapPoint = canvas->getCoordinateTransform()->transform( xGeoref, yGeoref );
512+
QList<QgsMapToolIdentify::IdentifyResult> result = action->identify( mapPoint.x(), mapPoint.y(), QList<QgsMapLayer *>() << layer );
513+
return result;
514+
}
515+
505516
// private
506517
QList<QgsMapToolIdentify::IdentifyResult>
507518
TestQgsMapToolIdentifyAction::testIdentifyVector( QgsVectorLayer *layer, double xGeoref, double yGeoref )
@@ -571,6 +582,74 @@ void TestQgsMapToolIdentifyAction::identifyRasterFloat64()
571582
QCOMPARE( testIdentifyRaster( tempLayer.get(), 6.5, 0.5 ), QString( "1.2345678901234" ) );
572583
}
573584

585+
void TestQgsMapToolIdentifyAction::identifyMesh()
586+
{
587+
//create a temporary layer
588+
const QString mesh = QStringLiteral( TEST_DATA_DIR ) + "/mesh/quad_and_triangle.2dm";
589+
QgsMeshLayer *tempLayer = new QgsMeshLayer( mesh, "testlayer", "mdal" );
590+
QVERIFY( tempLayer->isValid() );
591+
const QString vectorDs = QStringLiteral( TEST_DATA_DIR ) + "/mesh/quad_and_triangle_vertex_vector.dat";
592+
tempLayer->dataProvider()->addDataset( vectorDs );
593+
594+
// we need to setup renderer otherwise triangular mesh
595+
// will not be populated and identify will not work
596+
QgsMapSettings mapSettings;
597+
mapSettings.setExtent( tempLayer->extent() );
598+
mapSettings.setDestinationCrs( tempLayer->crs() );
599+
mapSettings.setOutputDpi( 96 );
600+
601+
// here we check that datasets automatically get our default color ramp applied ("Plasma")
602+
QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
603+
tempLayer->createMapRenderer( context );
604+
605+
// only scalar dataset
606+
QgsMeshRendererSettings settings = tempLayer->rendererSettings();
607+
settings.setActiveScalarDataset( QgsMeshDatasetIndex( 0, 0 ) );
608+
tempLayer->setRendererSettings( settings );
609+
QList<QgsMapToolIdentify::IdentifyResult> results;
610+
611+
results = testIdentifyMesh( tempLayer, 500, 500 );
612+
QCOMPARE( results.size(), 1 );
613+
QCOMPARE( results[0].mAttributes[ QStringLiteral( "Scalar Value" )], QStringLiteral( "no data" ) );
614+
results = testIdentifyMesh( tempLayer, 2400, 2400 );
615+
QCOMPARE( results.size(), 1 );
616+
QCOMPARE( results[0].mAttributes[ QStringLiteral( "Scalar Value" )], QStringLiteral( "42" ) );
617+
618+
// scalar + vector same
619+
settings.setActiveScalarDataset( QgsMeshDatasetIndex( 1, 0 ) );
620+
settings.setActiveVectorDataset( QgsMeshDatasetIndex( 1, 0 ) );
621+
tempLayer->setRendererSettings( settings );
622+
results = testIdentifyMesh( tempLayer, 500, 500 );
623+
QCOMPARE( results.size(), 1 );
624+
QCOMPARE( results[0].mAttributes[ QStringLiteral( "Vector Value" )], QStringLiteral( "no data" ) );
625+
results = testIdentifyMesh( tempLayer, 2400, 2400 );
626+
QCOMPARE( results.size(), 1 );
627+
QCOMPARE( results[0].mAttributes[ QStringLiteral( "Vector Magnitude" )], QStringLiteral( "3" ) );
628+
QCOMPARE( results[0].mAttributes[ QStringLiteral( "Vector x-component" )], QStringLiteral( "1.8" ) );
629+
QCOMPARE( results[0].mAttributes[ QStringLiteral( "Vector y-component" )], QStringLiteral( "2.4" ) );
630+
631+
// scalar + vector different
632+
settings.setActiveScalarDataset( QgsMeshDatasetIndex( 0, 0 ) );
633+
settings.setActiveVectorDataset( QgsMeshDatasetIndex( 1, 0 ) );
634+
tempLayer->setRendererSettings( settings );
635+
results = testIdentifyMesh( tempLayer, 2400, 2400 );
636+
QCOMPARE( results.size(), 2 );
637+
QCOMPARE( results[0].mAttributes[ QStringLiteral( "Scalar Value" )], QStringLiteral( "42" ) );
638+
QCOMPARE( results[1].mAttributes[ QStringLiteral( "Vector Magnitude" )], QStringLiteral( "3" ) );
639+
QCOMPARE( results[1].mAttributes[ QStringLiteral( "Vector x-component" )], QStringLiteral( "1.8" ) );
640+
QCOMPARE( results[1].mAttributes[ QStringLiteral( "Vector y-component" )], QStringLiteral( "2.4" ) );
641+
642+
// only vector
643+
settings.setActiveScalarDataset( QgsMeshDatasetIndex() );
644+
settings.setActiveVectorDataset( QgsMeshDatasetIndex( 1, 0 ) );
645+
tempLayer->setRendererSettings( settings );
646+
results = testIdentifyMesh( tempLayer, 2400, 2400 );
647+
QCOMPARE( results.size(), 1 );
648+
QCOMPARE( results[0].mAttributes[ QStringLiteral( "Vector Magnitude" )], QStringLiteral( "3" ) );
649+
QCOMPARE( results[0].mAttributes[ QStringLiteral( "Vector x-component" )], QStringLiteral( "1.8" ) );
650+
QCOMPARE( results[0].mAttributes[ QStringLiteral( "Vector y-component" )], QStringLiteral( "2.4" ) );
651+
}
652+
574653
void TestQgsMapToolIdentifyAction::identifyInvalidPolygons()
575654
{
576655
//create a temporary layer

0 commit comments

Comments
 (0)
Please sign in to comment.