/
qgsmaptoolidentify.cpp
1065 lines (913 loc) · 41.3 KB
/
qgsmaptoolidentify.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsmaptoolidentify.cpp - map tool for identifying features
---------------------
begin : January 2006
copyright : (C) 2006 by Martin Dobias
email : wonder.sk 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 "qgsapplication.h"
#include "qgsdistancearea.h"
#include "qgsfeature.h"
#include "qgsfeatureiterator.h"
#include "qgsfeaturestore.h"
#include "qgsfields.h"
#include "qgsgeometry.h"
#include "qgsgeometryengine.h"
#include "qgsidentifymenu.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaptoolidentify.h"
#include "qgsmaptopixel.h"
#include "qgsmessageviewer.h"
#include "qgsmeshlayer.h"
#include "qgsmaplayer.h"
#include "qgsrasterdataprovider.h"
#include "qgsrasterlayer.h"
#include "qgsrasteridentifyresult.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsvectortilelayer.h"
#include "qgsvectortilemvtdecoder.h"
#include "qgsvectortileutils.h"
#include "qgsproject.h"
#include "qgsrenderer.h"
#include "qgstiles.h"
#include "qgsgeometryutils.h"
#include "qgsgeometrycollection.h"
#include "qgscurve.h"
#include "qgscoordinateutils.h"
#include "qgsexception.h"
#include "qgssettings.h"
#include "qgsexpressioncontextutils.h"
#include <QMouseEvent>
#include <QCursor>
#include <QPixmap>
#include <QStatusBar>
#include <QVariant>
#include <QMenu>
QgsMapToolIdentify::QgsMapToolIdentify( QgsMapCanvas *canvas )
: QgsMapTool( canvas )
, mIdentifyMenu( new QgsIdentifyMenu( mCanvas ) )
, mLastMapUnitsPerPixel( -1.0 )
, mCoordinatePrecision( 6 )
{
setCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::Identify ) );
}
QgsMapToolIdentify::~QgsMapToolIdentify()
{
delete mIdentifyMenu;
}
void QgsMapToolIdentify::canvasMoveEvent( QgsMapMouseEvent *e )
{
Q_UNUSED( e )
}
void QgsMapToolIdentify::canvasPressEvent( QgsMapMouseEvent *e )
{
Q_UNUSED( e )
}
void QgsMapToolIdentify::canvasReleaseEvent( QgsMapMouseEvent *e )
{
Q_UNUSED( e )
}
QList<QgsMapToolIdentify::IdentifyResult> QgsMapToolIdentify::identify( int x, int y, const QList<QgsMapLayer *> &layerList, IdentifyMode mode )
{
return identify( x, y, mode, layerList, AllLayers );
}
QList<QgsMapToolIdentify::IdentifyResult> QgsMapToolIdentify::identify( int x, int y, IdentifyMode mode, LayerType layerType )
{
return identify( x, y, mode, QList<QgsMapLayer *>(), layerType );
}
QList<QgsMapToolIdentify::IdentifyResult> QgsMapToolIdentify::identify( int x, int y, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType )
{
return identify( QgsGeometry::fromPointXY( toMapCoordinates( QPoint( x, y ) ) ), mode, layerList, layerType );
}
QList<QgsMapToolIdentify::IdentifyResult> QgsMapToolIdentify::identify( const QgsGeometry &geometry, IdentifyMode mode, LayerType layerType )
{
return identify( geometry, mode, QList<QgsMapLayer *>(), layerType );
}
QList<QgsMapToolIdentify::IdentifyResult> QgsMapToolIdentify::identify( const QgsGeometry &geometry, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType )
{
QList<IdentifyResult> results;
mLastGeometry = geometry;
mLastExtent = mCanvas->extent();
mLastMapUnitsPerPixel = mCanvas->mapUnitsPerPixel();
mCoordinatePrecision = QgsCoordinateUtils::calculateCoordinatePrecision( mLastMapUnitsPerPixel, mCanvas->mapSettings().destinationCrs() );
if ( mode == DefaultQgsSetting )
{
QgsSettings settings;
mode = settings.enumValue( QStringLiteral( "Map/identifyMode" ), ActiveLayer );
}
if ( mode == LayerSelection )
{
QPoint canvasPt = toCanvasCoordinates( geometry.asPoint() );
int x = canvasPt.x(), y = canvasPt.y();
QList<IdentifyResult> results = identify( x, y, TopDownAll, layerList, layerType );
QPoint globalPos = mCanvas->mapToGlobal( QPoint( x + 5, y + 5 ) );
return mIdentifyMenu->exec( results, globalPos );
}
else if ( mode == ActiveLayer && layerList.isEmpty() )
{
QgsMapLayer *layer = mCanvas->currentLayer();
if ( !layer )
{
emit identifyMessage( tr( "No active layer. To identify features, you must choose an active layer." ) );
return results;
}
QApplication::setOverrideCursor( Qt::WaitCursor );
identifyLayer( &results, layer, mLastGeometry, mLastExtent, mLastMapUnitsPerPixel, layerType );
}
else
{
QApplication::setOverrideCursor( Qt::WaitCursor );
int layerCount;
if ( layerList.isEmpty() )
layerCount = mCanvas->layerCount();
else
layerCount = layerList.count();
for ( int i = 0; i < layerCount; i++ )
{
QgsMapLayer *layer = nullptr;
if ( layerList.isEmpty() )
layer = mCanvas->layer( i );
else
layer = layerList.value( i );
emit identifyProgress( i, mCanvas->layerCount() );
emit identifyMessage( tr( "Identifying on %1…" ).arg( layer->name() ) );
if ( !layer->flags().testFlag( QgsMapLayer::Identifiable ) )
continue;
if ( identifyLayer( &results, layer, mLastGeometry, mLastExtent, mLastMapUnitsPerPixel, layerType ) )
{
if ( mode == TopDownStopAtFirst )
break;
}
}
emit identifyProgress( mCanvas->layerCount(), mCanvas->layerCount() );
emit identifyMessage( tr( "Identifying done." ) );
}
QApplication::restoreOverrideCursor();
return results;
}
void QgsMapToolIdentify::setCanvasPropertiesOverrides( double searchRadiusMapUnits )
{
mOverrideCanvasSearchRadius = searchRadiusMapUnits;
}
void QgsMapToolIdentify::restoreCanvasPropertiesOverrides()
{
mOverrideCanvasSearchRadius = -1;
}
void QgsMapToolIdentify::activate()
{
QgsMapTool::activate();
}
void QgsMapToolIdentify::deactivate()
{
QgsMapTool::deactivate();
}
bool QgsMapToolIdentify::identifyLayer( QList<IdentifyResult> *results, QgsMapLayer *layer, const QgsPointXY &point, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType )
{
return identifyLayer( results, layer, QgsGeometry::fromPointXY( point ), viewExtent, mapUnitsPerPixel, layerType );
}
bool QgsMapToolIdentify::identifyLayer( QList<IdentifyResult> *results, QgsMapLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType )
{
if ( layer->type() == QgsMapLayerType::RasterLayer && layerType.testFlag( RasterLayer ) )
{
return identifyRasterLayer( results, qobject_cast<QgsRasterLayer *>( layer ), geometry, viewExtent, mapUnitsPerPixel );
}
else if ( layer->type() == QgsMapLayerType::VectorLayer && layerType.testFlag( VectorLayer ) )
{
return identifyVectorLayer( results, qobject_cast<QgsVectorLayer *>( layer ), geometry );
}
else if ( layer->type() == QgsMapLayerType::MeshLayer && layerType.testFlag( MeshLayer ) )
{
return identifyMeshLayer( results, qobject_cast<QgsMeshLayer *>( layer ), geometry );
}
else if ( layer->type() == QgsMapLayerType::VectorTileLayer && layerType.testFlag( VectorTileLayer ) )
{
return identifyVectorTileLayer( results, qobject_cast<QgsVectorTileLayer *>( layer ), geometry );
}
else
{
return false;
}
}
bool QgsMapToolIdentify::identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsPointXY &point )
{
return identifyVectorLayer( results, layer, QgsGeometry::fromPointXY( point ) );
}
bool QgsMapToolIdentify::identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsGeometry &geometry )
{
const QgsPointXY point = geometry.asPoint(); // mesh layers currently only support identification by point
return identifyMeshLayer( results, layer, point );
}
bool QgsMapToolIdentify::identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsPointXY &point )
{
QgsDebugMsgLevel( "point = " + point.toString(), 4 );
if ( !layer || !layer->dataProvider() )
return false;
const QgsMeshRendererSettings rendererSettings = layer->rendererSettings();
const QgsMeshDatasetIndex scalarDatasetIndex = layer->activeScalarDatasetAtTime( mCanvas->temporalRange() );
const QgsMeshDatasetIndex vectorDatasetIndex = layer->activeVectorDatasetAtTime( mCanvas->temporalRange() );
if ( ! scalarDatasetIndex.isValid() && ! vectorDatasetIndex.isValid() )
return false;
QMap< QString, QString > scalarAttributes, vectorAttributes, raw3dAttributes;
double searchRadius = mOverrideCanvasSearchRadius < 0 ? searchRadiusMU( mCanvas ) : mOverrideCanvasSearchRadius;
QString scalarGroup;
if ( scalarDatasetIndex.isValid() )
{
scalarGroup = layer->dataProvider()->datasetGroupMetadata( scalarDatasetIndex.group() ).name();
const QgsMeshDatasetValue scalarValue = layer->datasetValue( scalarDatasetIndex, point, searchRadius );
const double scalar = scalarValue.scalar();
if ( std::isnan( scalar ) )
scalarAttributes.insert( tr( "Scalar Value" ), tr( "no data" ) );
else
scalarAttributes.insert( tr( "Scalar Value" ), QString::number( scalar ) );
}
QString vectorGroup;
if ( vectorDatasetIndex.isValid() )
{
vectorGroup = layer->dataProvider()->datasetGroupMetadata( vectorDatasetIndex.group() ).name();
const QgsMeshDatasetValue vectorValue = layer->datasetValue( vectorDatasetIndex, point, searchRadius );
const double vectorX = vectorValue.x();
const double vectorY = vectorValue.y();
if ( std::isnan( vectorX ) || std::isnan( vectorY ) )
vectorAttributes.insert( tr( "Vector Value" ), tr( "no data" ) );
else
{
vectorAttributes.insert( tr( "Vector Magnitude" ), QString::number( vectorValue.scalar() ) );
vectorAttributes.insert( tr( "Vector x-component" ), QString::number( vectorY ) );
vectorAttributes.insert( tr( "Vector y-component" ), QString::number( vectorX ) );
}
}
const QMap< QString, QString > derivedAttributes = derivedAttributesForPoint( QgsPoint( point ) );
if ( scalarGroup == vectorGroup )
{
const IdentifyResult result( qobject_cast<QgsMapLayer *>( layer ),
scalarGroup,
vectorAttributes,
derivedAttributes );
results->append( result );
}
else
{
if ( !scalarGroup.isEmpty() )
{
const IdentifyResult result( qobject_cast<QgsMapLayer *>( layer ),
scalarGroup,
scalarAttributes,
derivedAttributes );
results->append( result );
}
if ( !vectorGroup.isEmpty() )
{
const IdentifyResult result( qobject_cast<QgsMapLayer *>( layer ),
vectorGroup,
vectorAttributes,
derivedAttributes );
results->append( result );
}
}
return true;
}
bool QgsMapToolIdentify::identifyVectorTileLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorTileLayer *layer, const QgsGeometry &geometry )
{
if ( !layer || !layer->isSpatial() )
return false;
if ( !layer->isInScaleRange( mCanvas->mapSettings().scale() ) )
{
QgsDebugMsgLevel( QStringLiteral( "Out of scale limits" ), 2 );
return false;
}
QgsTemporaryCursorOverride waitCursor( Qt::WaitCursor );
QMap< QString, QString > commonDerivedAttributes;
QgsGeometry selectionGeom = geometry;
bool isPointOrRectangle;
QgsPointXY point;
bool isSingleClick = selectionGeom.type() == QgsWkbTypes::PointGeometry;
if ( isSingleClick )
{
isPointOrRectangle = true;
point = selectionGeom.asPoint();
commonDerivedAttributes = derivedAttributesForPoint( QgsPoint( point ) );
}
else
{
// we have a polygon - maybe it is a rectangle - in such case we can avoid costly insterestion tests later
isPointOrRectangle = QgsGeometry::fromRect( selectionGeom.boundingBox() ).isGeosEqual( selectionGeom );
}
int featureCount = 0;
QgsFeatureList featureList;
std::unique_ptr<QgsGeometryEngine> selectionGeomPrepared;
// toLayerCoordinates will throw an exception for an 'invalid' point.
// For example, if you project a world map onto a globe using EPSG 2163
// and then click somewhere off the globe, an exception will be thrown.
try
{
QgsRectangle r;
if ( isSingleClick )
{
double sr = mOverrideCanvasSearchRadius < 0 ? searchRadiusMU( mCanvas ) : mOverrideCanvasSearchRadius;
r = toLayerCoordinates( layer, QgsRectangle( point.x() - sr, point.y() - sr, point.x() + sr, point.y() + sr ) );
}
else
{
r = toLayerCoordinates( layer, selectionGeom.boundingBox() );
if ( !isPointOrRectangle )
{
QgsCoordinateTransform ct( mCanvas->mapSettings().destinationCrs(), layer->crs(), mCanvas->mapSettings().transformContext() );
if ( ct.isValid() )
selectionGeom.transform( ct );
// use prepared geometry for faster intersection test
selectionGeomPrepared.reset( QgsGeometry::createGeometryEngine( selectionGeom.constGet() ) );
}
}
int tileZoom = QgsVectorTileUtils::scaleToZoomLevel( mCanvas->scale(), layer->sourceMinZoom(), layer->sourceMaxZoom() );
QgsTileMatrix tileMatrix = QgsTileMatrix::fromWebMercator( tileZoom );
QgsTileRange tileRange = tileMatrix.tileRangeFromExtent( r );
for ( int row = tileRange.startRow(); row <= tileRange.endRow(); ++row )
{
for ( int col = tileRange.startColumn(); col <= tileRange.endColumn(); ++col )
{
QgsTileXYZ tileID( col, row, tileZoom );
QByteArray data = layer->getRawTile( tileID );
if ( data.isEmpty() )
continue; // failed to get data
QgsVectorTileMVTDecoder decoder;
if ( !decoder.decode( tileID, data ) )
continue; // failed to decode
QMap<QString, QgsFields> perLayerFields;
const QStringList layerNames = decoder.layers();
for ( const QString &layerName : layerNames )
{
QSet<QString> fieldNames = QSet<QString>::fromList( decoder.layerFieldNames( layerName ) );
perLayerFields[layerName] = QgsVectorTileUtils::makeQgisFields( fieldNames );
}
const QgsVectorTileFeatures features = decoder.layerFeatures( perLayerFields, QgsCoordinateTransform() );
const QStringList featuresLayerNames = features.keys();
for ( const QString &layerName : featuresLayerNames )
{
const QgsFields fFields = perLayerFields[layerName];
const QVector<QgsFeature> &layerFeatures = features[layerName];
for ( const QgsFeature &f : layerFeatures )
{
if ( f.geometry().intersects( r ) && ( !selectionGeomPrepared || selectionGeomPrepared->intersects( f.geometry().constGet() ) ) )
{
QMap< QString, QString > derivedAttributes = commonDerivedAttributes;
derivedAttributes.insert( tr( "Feature ID" ), FID_TO_STRING( f.id() ) );
results->append( IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), layerName, fFields, f, derivedAttributes ) );
featureCount++;
}
}
}
}
}
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse )
// catch exception for 'invalid' point and proceed with no features found
QgsDebugMsg( QStringLiteral( "Caught CRS exception %1" ).arg( cse.what() ) );
}
return featureCount > 0;
}
QMap<QString, QString> QgsMapToolIdentify::derivedAttributesForPoint( const QgsPoint &point )
{
QMap< QString, QString > derivedAttributes;
derivedAttributes.insert( tr( "(clicked coordinate X)" ), formatXCoordinate( point ) );
derivedAttributes.insert( tr( "(clicked coordinate Y)" ), formatYCoordinate( point ) );
if ( point.is3D() )
derivedAttributes.insert( tr( "(clicked coordinate Z)" ), QString::number( point.z(), 'f' ) );
return derivedAttributes;
}
bool QgsMapToolIdentify::identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsGeometry &geometry )
{
if ( !layer || !layer->isSpatial() )
return false;
if ( !layer->isInScaleRange( mCanvas->mapSettings().scale() ) )
{
QgsDebugMsg( QStringLiteral( "Out of scale limits" ) );
return false;
}
QApplication::setOverrideCursor( Qt::WaitCursor );
QMap< QString, QString > commonDerivedAttributes;
QgsGeometry selectionGeom = geometry;
bool isPointOrRectangle;
QgsPointXY point;
bool isSingleClick = selectionGeom.type() == QgsWkbTypes::PointGeometry;
if ( isSingleClick )
{
isPointOrRectangle = true;
point = selectionGeom.asPoint();
commonDerivedAttributes = derivedAttributesForPoint( QgsPoint( point ) );
}
else
{
// we have a polygon - maybe it is a rectangle - in such case we can avoid costly insterestion tests later
isPointOrRectangle = QgsGeometry::fromRect( selectionGeom.boundingBox() ).isGeosEqual( selectionGeom );
}
int featureCount = 0;
QgsFeatureList featureList;
std::unique_ptr<QgsGeometryEngine> selectionGeomPrepared;
// toLayerCoordinates will throw an exception for an 'invalid' point.
// For example, if you project a world map onto a globe using EPSG 2163
// and then click somewhere off the globe, an exception will be thrown.
try
{
QgsRectangle r;
if ( isSingleClick )
{
double sr = mOverrideCanvasSearchRadius < 0 ? searchRadiusMU( mCanvas ) : mOverrideCanvasSearchRadius;
r = toLayerCoordinates( layer, QgsRectangle( point.x() - sr, point.y() - sr, point.x() + sr, point.y() + sr ) );
}
else
{
r = toLayerCoordinates( layer, selectionGeom.boundingBox() );
if ( !isPointOrRectangle )
{
QgsCoordinateTransform ct( mCanvas->mapSettings().destinationCrs(), layer->crs(), mCanvas->mapSettings().transformContext() );
if ( ct.isValid() )
selectionGeom.transform( ct );
// use prepared geometry for faster intersection test
selectionGeomPrepared.reset( QgsGeometry::createGeometryEngine( selectionGeom.constGet() ) );
}
}
QgsFeatureIterator fit = layer->getFeatures( QgsFeatureRequest().setFilterRect( r ).setFlags( QgsFeatureRequest::ExactIntersect ) );
QgsFeature f;
while ( fit.nextFeature( f ) )
{
if ( !selectionGeomPrepared || selectionGeomPrepared->intersects( f.geometry().constGet() ) )
featureList << QgsFeature( f );
}
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse )
// catch exception for 'invalid' point and proceed with no features found
QgsDebugMsg( QStringLiteral( "Caught CRS exception %1" ).arg( cse.what() ) );
}
bool filter = false;
QgsRenderContext context( QgsRenderContext::fromMapSettings( mCanvas->mapSettings() ) );
context.expressionContext() << QgsExpressionContextUtils::layerScope( layer );
std::unique_ptr< QgsFeatureRenderer > renderer( layer->renderer() ? layer->renderer()->clone() : nullptr );
if ( renderer )
{
// setup scale for scale dependent visibility (rule based)
renderer->startRender( context, layer->fields() );
filter = renderer->capabilities() & QgsFeatureRenderer::Filter;
}
for ( const QgsFeature &feature : qgis::as_const( featureList ) )
{
QMap< QString, QString > derivedAttributes = commonDerivedAttributes;
QgsFeatureId fid = feature.id();
context.expressionContext().setFeature( feature );
if ( filter && !renderer->willRenderFeature( feature, context ) )
continue;
featureCount++;
if ( isSingleClick )
derivedAttributes.unite( featureDerivedAttributes( feature, layer, toLayerCoordinates( layer, point ) ) );
derivedAttributes.insert( tr( "Feature ID" ), fid < 0 ? tr( "new feature" ) : FID_TO_STRING( fid ) );
results->append( IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), feature, derivedAttributes ) );
}
if ( renderer )
{
renderer->stopRender( context );
}
QgsDebugMsgLevel( "Feature count on identify: " + QString::number( featureCount ), 2 );
QApplication::restoreOverrideCursor();
return featureCount > 0;
}
void QgsMapToolIdentify::closestVertexAttributes( const QgsAbstractGeometry &geometry, QgsVertexId vId, QgsMapLayer *layer, QMap< QString, QString > &derivedAttributes )
{
QString str = QLocale().toString( vId.vertex + 1 );
derivedAttributes.insert( tr( "Closest vertex number" ), str );
QgsPoint closestPoint = geometry.vertexAt( vId );
QgsPointXY closestPointMapCoords = mCanvas->mapSettings().layerToMapCoordinates( layer, QgsPointXY( closestPoint.x(), closestPoint.y() ) );
derivedAttributes.insert( tr( "Closest vertex X" ), formatXCoordinate( closestPointMapCoords ) );
derivedAttributes.insert( tr( "Closest vertex Y" ), formatYCoordinate( closestPointMapCoords ) );
if ( closestPoint.is3D() )
{
str = QLocale().toString( closestPoint.z(), 'g', 10 );
derivedAttributes.insert( tr( "Closest vertex Z" ), str );
}
if ( closestPoint.isMeasure() )
{
str = QLocale().toString( closestPoint.m(), 'g', 10 );
derivedAttributes.insert( tr( "Closest vertex M" ), str );
}
if ( vId.type == QgsVertexId::CurveVertex )
{
double radius, centerX, centerY;
QgsVertexId vIdBefore = vId;
--vIdBefore.vertex;
QgsVertexId vIdAfter = vId;
++vIdAfter.vertex;
QgsGeometryUtils::circleCenterRadius( geometry.vertexAt( vIdBefore ), geometry.vertexAt( vId ),
geometry.vertexAt( vIdAfter ), radius, centerX, centerY );
derivedAttributes.insert( QStringLiteral( "Closest vertex radius" ), QLocale().toString( radius ) );
}
}
void QgsMapToolIdentify::closestPointAttributes( const QgsAbstractGeometry &geometry, const QgsPointXY &layerPoint, QMap<QString, QString> &derivedAttributes )
{
QgsPoint closestPoint = QgsGeometryUtils::closestPoint( geometry, QgsPoint( layerPoint ) );
derivedAttributes.insert( tr( "Closest X" ), formatXCoordinate( closestPoint ) );
derivedAttributes.insert( tr( "Closest Y" ), formatYCoordinate( closestPoint ) );
if ( closestPoint.is3D() )
{
const QString str = QLocale().toString( closestPoint.z(), 'g', 10 );
derivedAttributes.insert( tr( "Interpolated Z" ), str );
}
if ( closestPoint.isMeasure() )
{
const QString str = QLocale().toString( closestPoint.m(), 'g', 10 );
derivedAttributes.insert( tr( "Interpolated M" ), str );
}
}
QString QgsMapToolIdentify::formatCoordinate( const QgsPointXY &canvasPoint ) const
{
return QgsCoordinateUtils::formatCoordinateForProject( QgsProject::instance(), canvasPoint, mCanvas->mapSettings().destinationCrs(),
mCoordinatePrecision );
}
QString QgsMapToolIdentify::formatXCoordinate( const QgsPointXY &canvasPoint ) const
{
QString coordinate = formatCoordinate( canvasPoint );
return coordinate.split( ',' ).at( 0 );
}
QString QgsMapToolIdentify::formatYCoordinate( const QgsPointXY &canvasPoint ) const
{
QString coordinate = formatCoordinate( canvasPoint );
return coordinate.split( ',' ).at( 1 );
}
QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( const QgsFeature &feature, QgsMapLayer *layer, const QgsPointXY &layerPoint )
{
// Calculate derived attributes and insert:
// measure distance or area depending on geometry type
QMap< QString, QString > derivedAttributes;
// init distance/area calculator
QString ellipsoid = QgsProject::instance()->ellipsoid();
QgsDistanceArea calc;
calc.setEllipsoid( ellipsoid );
calc.setSourceCrs( layer->crs(), QgsProject::instance()->transformContext() );
QgsWkbTypes::Type wkbType = QgsWkbTypes::NoGeometry;
QgsWkbTypes::GeometryType geometryType = QgsWkbTypes::NullGeometry;
QgsVertexId vId;
QgsPoint closestPoint;
if ( feature.hasGeometry() )
{
geometryType = feature.geometry().type();
wkbType = feature.geometry().wkbType();
//find closest vertex to clicked point
closestPoint = QgsGeometryUtils::closestVertex( *feature.geometry().constGet(), QgsPoint( layerPoint ), vId );
}
if ( QgsWkbTypes::isMultiType( wkbType ) )
{
QString str = QLocale().toString( static_cast<const QgsGeometryCollection *>( feature.geometry().constGet() )->numGeometries() );
derivedAttributes.insert( tr( "Parts" ), str );
str = QLocale().toString( vId.part + 1 );
derivedAttributes.insert( tr( "Part number" ), str );
}
QgsUnitTypes::DistanceUnit cartesianDistanceUnits = QgsUnitTypes::unitType( layer->crs().mapUnits() ) == QgsUnitTypes::unitType( displayDistanceUnits() )
? displayDistanceUnits() : layer->crs().mapUnits();
QgsUnitTypes::AreaUnit cartesianAreaUnits = QgsUnitTypes::unitType( QgsUnitTypes::distanceToAreaUnit( layer->crs().mapUnits() ) ) == QgsUnitTypes::unitType( displayAreaUnits() )
? displayAreaUnits() : QgsUnitTypes::distanceToAreaUnit( layer->crs().mapUnits() );
if ( geometryType == QgsWkbTypes::LineGeometry )
{
double dist = calc.measureLength( feature.geometry() );
dist = calc.convertLengthMeasurement( dist, displayDistanceUnits() );
QString str;
if ( ellipsoid != geoNone() )
{
str = formatDistance( dist );
derivedAttributes.insert( tr( "Length (Ellipsoidal — %1)" ).arg( ellipsoid ), str );
}
str = formatDistance( feature.geometry().constGet()->length()
* QgsUnitTypes::fromUnitToUnitFactor( layer->crs().mapUnits(), cartesianDistanceUnits ), cartesianDistanceUnits );
if ( !QgsWkbTypes::hasZ( feature.geometry().wkbType() ) )
derivedAttributes.insert( tr( "Length (Cartesian)" ), str );
else
derivedAttributes.insert( tr( "Length (Cartesian — 2D)" ), str );
if ( QgsWkbTypes::hasZ( feature.geometry().wkbType() ) && QgsWkbTypes::flatType( feature.geometry().wkbType() ) == QgsWkbTypes::LineString )
{
str = formatDistance( qgsgeometry_cast< const QgsLineString * >( feature.geometry().constGet() )->length3D()
* QgsUnitTypes::fromUnitToUnitFactor( layer->crs().mapUnits(), cartesianDistanceUnits ), cartesianDistanceUnits );
derivedAttributes.insert( tr( "Length (Cartesian — 3D)" ), str );
}
const QgsAbstractGeometry *geom = feature.geometry().constGet();
if ( geom )
{
str = QLocale().toString( geom->nCoordinates() );
derivedAttributes.insert( tr( "Vertices" ), str );
//add details of closest vertex to identify point
closestVertexAttributes( *geom, vId, layer, derivedAttributes );
closestPointAttributes( *geom, layerPoint, derivedAttributes );
if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geom ) )
{
// Add the start and end points in as derived attributes
QgsPointXY pnt = mCanvas->mapSettings().layerToMapCoordinates( layer, QgsPointXY( curve->startPoint().x(), curve->startPoint().y() ) );
str = formatXCoordinate( pnt );
derivedAttributes.insert( tr( "firstX", "attributes get sorted; translation for lastX should be lexically larger than this one" ), str );
str = formatYCoordinate( pnt );
derivedAttributes.insert( tr( "firstY" ), str );
pnt = mCanvas->mapSettings().layerToMapCoordinates( layer, QgsPointXY( curve->endPoint().x(), curve->endPoint().y() ) );
str = formatXCoordinate( pnt );
derivedAttributes.insert( tr( "lastX", "attributes get sorted; translation for firstX should be lexically smaller than this one" ), str );
str = formatYCoordinate( pnt );
derivedAttributes.insert( tr( "lastY" ), str );
}
}
}
else if ( geometryType == QgsWkbTypes::PolygonGeometry )
{
double area = calc.measureArea( feature.geometry() );
area = calc.convertAreaMeasurement( area, displayAreaUnits() );
QString str;
if ( ellipsoid != geoNone() )
{
str = formatArea( area );
derivedAttributes.insert( tr( "Area (Ellipsoidal — %1)" ).arg( ellipsoid ), str );
}
str = formatArea( feature.geometry().area()
* QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::distanceToAreaUnit( layer->crs().mapUnits() ), cartesianAreaUnits ), cartesianAreaUnits );
derivedAttributes.insert( tr( "Area (Cartesian)" ), str );
if ( ellipsoid != geoNone() )
{
double perimeter = calc.measurePerimeter( feature.geometry() );
perimeter = calc.convertLengthMeasurement( perimeter, displayDistanceUnits() );
str = formatDistance( perimeter );
derivedAttributes.insert( tr( "Perimeter (Ellipsoidal — %1)" ).arg( ellipsoid ), str );
}
str = formatDistance( feature.geometry().constGet()->perimeter()
* QgsUnitTypes::fromUnitToUnitFactor( layer->crs().mapUnits(), cartesianDistanceUnits ), cartesianDistanceUnits );
derivedAttributes.insert( tr( "Perimeter (Cartesian)" ), str );
str = QLocale().toString( feature.geometry().constGet()->nCoordinates() );
derivedAttributes.insert( tr( "Vertices" ), str );
//add details of closest vertex to identify point
closestVertexAttributes( *feature.geometry().constGet(), vId, layer, derivedAttributes );
closestPointAttributes( *feature.geometry().constGet(), layerPoint, derivedAttributes );
}
else if ( geometryType == QgsWkbTypes::PointGeometry )
{
if ( QgsWkbTypes::flatType( wkbType ) == QgsWkbTypes::Point )
{
// Include the x and y coordinates of the point as a derived attribute
QgsPointXY pnt = mCanvas->mapSettings().layerToMapCoordinates( layer, feature.geometry().asPoint() );
QString str = formatXCoordinate( pnt );
derivedAttributes.insert( tr( "X" ), str );
str = formatYCoordinate( pnt );
derivedAttributes.insert( tr( "Y" ), str );
if ( QgsWkbTypes::hasZ( wkbType ) )
{
str = QLocale().toString( static_cast<const QgsPoint *>( feature.geometry().constGet() )->z(), 'g', 10 );
derivedAttributes.insert( tr( "Z" ), str );
}
if ( QgsWkbTypes::hasM( wkbType ) )
{
str = QLocale().toString( static_cast<const QgsPoint *>( feature.geometry().constGet() )->m(), 'g', 10 );
derivedAttributes.insert( tr( "M" ), str );
}
}
else
{
//multipart
//add details of closest vertex to identify point
const QgsAbstractGeometry *geom = feature.geometry().constGet();
{
closestVertexAttributes( *geom, vId, layer, derivedAttributes );
}
}
}
return derivedAttributes;
}
bool QgsMapToolIdentify::identifyRasterLayer( QList<IdentifyResult> *results, QgsRasterLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel )
{
QgsPointXY point = geometry.asPoint(); // raster layers currently only support identification by point
return identifyRasterLayer( results, layer, point, viewExtent, mapUnitsPerPixel );
}
bool QgsMapToolIdentify::identifyRasterLayer( QList<IdentifyResult> *results, QgsRasterLayer *layer, QgsPointXY point, const QgsRectangle &viewExtent, double mapUnitsPerPixel )
{
QgsDebugMsg( "point = " + point.toString() );
if ( !layer )
return false;
QgsRasterDataProvider *dprovider = layer->dataProvider();
if ( !dprovider )
return false;
int capabilities = dprovider->capabilities();
if ( !( capabilities & QgsRasterDataProvider::Identify ) )
return false;
QgsPointXY pointInCanvasCrs = point;
try
{
point = toLayerCoordinates( layer, point );
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse )
QgsDebugMsg( QStringLiteral( "coordinate not reprojectable: %1" ).arg( cse.what() ) );
return false;
}
QgsDebugMsg( QStringLiteral( "point = %1 %2" ).arg( point.x() ).arg( point.y() ) );
if ( !layer->extent().contains( point ) )
return false;
QMap< QString, QString > attributes, derivedAttributes;
QgsRaster::IdentifyFormat format = QgsRasterDataProvider::identifyFormatFromName( layer->customProperty( QStringLiteral( "identify/format" ) ).toString() );
// check if the format is really supported otherwise use first supported format
if ( !( QgsRasterDataProvider::identifyFormatToCapability( format ) & capabilities ) )
{
if ( capabilities & QgsRasterInterface::IdentifyFeature ) format = QgsRaster::IdentifyFormatFeature;
else if ( capabilities & QgsRasterInterface::IdentifyValue ) format = QgsRaster::IdentifyFormatValue;
else if ( capabilities & QgsRasterInterface::IdentifyHtml ) format = QgsRaster::IdentifyFormatHtml;
else if ( capabilities & QgsRasterInterface::IdentifyText ) format = QgsRaster::IdentifyFormatText;
else return false;
}
QgsRasterIdentifyResult identifyResult;
// We can only use current map canvas context (extent, width, height) if layer is not reprojected,
if ( dprovider->crs() != mCanvas->mapSettings().destinationCrs() )
{
// To get some reasonable response for point/line WMS vector layers we must
// use a context with approximately a resolution in layer CRS units
// corresponding to current map canvas resolution (for examplei UMN Mapserver
// in msWMSFeatureInfo() -> msQueryByRect() is using requested pixel
// + TOLERANCE (layer param) for feature selection)
//
QgsRectangle r;
r.setXMinimum( pointInCanvasCrs.x() - mapUnitsPerPixel / 2. );
r.setXMaximum( pointInCanvasCrs.x() + mapUnitsPerPixel / 2. );
r.setYMinimum( pointInCanvasCrs.y() - mapUnitsPerPixel / 2. );
r.setYMaximum( pointInCanvasCrs.y() + mapUnitsPerPixel / 2. );
r = toLayerCoordinates( layer, r ); // will be a bit larger
// Mapserver (6.0.3, for example) does not work with 1x1 pixel box
// but that is fixed (the rect is enlarged) in the WMS provider
identifyResult = dprovider->identify( point, format, r, 1, 1 );
}
else
{
// It would be nice to use the same extent and size which was used for drawing,
// so that WCS can use cache from last draw, unfortunately QgsRasterLayer::draw()
// is doing some tricks with extent and size to align raster to output which
// would be difficult to replicate here.
// Note: cutting the extent may result in slightly different x and y resolutions
// and thus shifted point calculated back in QGIS WMS (using average resolution)
//viewExtent = dprovider->extent().intersect( &viewExtent );
// Width and height are calculated from not projected extent and we hope that
// are similar to source width and height used to reproject layer for drawing.
// TODO: may be very dangerous, because it may result in different resolutions
// in source CRS, and WMS server (QGIS server) calcs wrong coor using average resolution.
int width = static_cast< int >( std::round( viewExtent.width() / mapUnitsPerPixel ) );
int height = static_cast< int >( std::round( viewExtent.height() / mapUnitsPerPixel ) );
QgsDebugMsg( QStringLiteral( "viewExtent.width = %1 viewExtent.height = %2" ).arg( viewExtent.width() ).arg( viewExtent.height() ) );
QgsDebugMsg( QStringLiteral( "width = %1 height = %2" ).arg( width ).arg( height ) );
QgsDebugMsg( QStringLiteral( "xRes = %1 yRes = %2 mapUnitsPerPixel = %3" ).arg( viewExtent.width() / width ).arg( viewExtent.height() / height ).arg( mapUnitsPerPixel ) );
identifyResult = dprovider->identify( point, format, viewExtent, width, height );
}
derivedAttributes.unite( derivedAttributesForPoint( QgsPoint( pointInCanvasCrs ) ) );
if ( identifyResult.isValid() )
{
QMap<int, QVariant> values = identifyResult.results();
QgsGeometry geometry;
if ( format == QgsRaster::IdentifyFormatValue )
{
for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
{
QString valueString;
if ( it.value().isNull() )
{
valueString = tr( "no data" );
}
else
{
QVariant value( it.value() );
// The cast is legit. Quoting QT doc :
// "Although this function is declared as returning QVariant::Type,
// the return value should be interpreted as QMetaType::Type"
if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::Float )
{
valueString = QgsRasterBlock::printValue( value.toFloat() );
}
else
{
valueString = QgsRasterBlock::printValue( value.toDouble() );
}
}
attributes.insert( dprovider->generateBandName( it.key() ), valueString );
}
QString label = layer->name();
results->append( IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), label, attributes, derivedAttributes ) );
}
else if ( format == QgsRaster::IdentifyFormatFeature )
{
for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
{
QVariant value = it.value();
if ( value.type() == QVariant::Bool && !value.toBool() )
{
// sublayer not visible or not queryable
continue;
}
if ( value.type() == QVariant::String )
{
// error
// TODO: better error reporting
QString label = layer->subLayers().value( it.key() );
attributes.clear();
attributes.insert( tr( "Error" ), value.toString() );
results->append( IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), label, attributes, derivedAttributes ) );
continue;
}
// list of feature stores for a single sublayer
const QgsFeatureStoreList featureStoreList = it.value().value<QgsFeatureStoreList>();
for ( const QgsFeatureStore &featureStore : featureStoreList )
{
const QgsFeatureList storeFeatures = featureStore.features();
for ( const QgsFeature &feature : storeFeatures )
{
attributes.clear();
// WMS sublayer and feature type, a sublayer may contain multiple feature types.
// Sublayer name may be the same as layer name and feature type name
// may be the same as sublayer. We try to avoid duplicities in label.
QString sublayer = featureStore.params().value( QStringLiteral( "sublayer" ) ).toString();
QString featureType = featureStore.params().value( QStringLiteral( "featureType" ) ).toString();
// Strip UMN MapServer '_feature'
featureType.remove( QStringLiteral( "_feature" ) );
QStringList labels;
if ( sublayer.compare( layer->name(), Qt::CaseInsensitive ) != 0 )
{
labels << sublayer;
}
if ( featureType.compare( sublayer, Qt::CaseInsensitive ) != 0 || labels.isEmpty() )
{
labels << featureType;
}
QMap< QString, QString > derAttributes = derivedAttributes;
derAttributes.unite( featureDerivedAttributes( feature, layer ) );
IdentifyResult identifyResult( qobject_cast<QgsMapLayer *>( layer ), labels.join( QStringLiteral( " / " ) ), featureStore.fields(), feature, derAttributes );
identifyResult.mParams.insert( QStringLiteral( "getFeatureInfoUrl" ), featureStore.params().value( QStringLiteral( "getFeatureInfoUrl" ) ) );
results->append( identifyResult );
}
}
}
}
else // text or html
{
QgsDebugMsg( QStringLiteral( "%1 HTML or text values" ).arg( values.size() ) );
for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
{
QString value = it.value().toString();