35
35
#include " qgsproject.h"
36
36
#include " qgsmaplayerregistry.h"
37
37
#include " qgsrendererv2.h"
38
+ #include " qgsgeometryutils.h"
39
+ #include " qgsgeometrycollectionv2.h"
38
40
39
41
#include < QSettings>
40
42
#include < QMouseEvent>
@@ -265,7 +267,7 @@ bool QgsMapToolIdentify::identifyVectorLayer( QList<IdentifyResult> *results, Qg
265
267
266
268
featureCount++;
267
269
268
- derivedAttributes.unite ( featureDerivedAttributes ( &( *f_it ), layer ) );
270
+ derivedAttributes.unite ( featureDerivedAttributes ( &( *f_it ), layer, toLayerCoordinates ( layer, point ) ) );
269
271
270
272
derivedAttributes.insert ( tr ( " feature id" ), fid < 0 ? tr ( " new feature" ) : FID_TO_STRING ( fid ) );
271
273
@@ -283,7 +285,32 @@ bool QgsMapToolIdentify::identifyVectorLayer( QList<IdentifyResult> *results, Qg
283
285
return featureCount > 0 ;
284
286
}
285
287
286
- QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes ( QgsFeature *feature, QgsMapLayer *layer )
288
+ void QgsMapToolIdentify::closestVertexAttributes ( const QgsAbstractGeometryV2& geometry, QgsVertexId vId, QgsMapLayer *layer, QMap< QString, QString >& derivedAttributes )
289
+ {
290
+ QString str = QLocale::system ().toString ( vId.vertex + 1 );
291
+ derivedAttributes.insert ( tr ( " Closest vertex number" ), str );
292
+
293
+ QgsPointV2 closestPoint = geometry.vertexAt ( vId );
294
+
295
+ QgsPoint closestPointMapCoords = mCanvas ->mapSettings ().layerToMapCoordinates ( layer, QgsPoint ( closestPoint.x (), closestPoint.y () ) );
296
+ str = QLocale::system ().toString ( closestPointMapCoords.x (), ' g' , 10 );
297
+ derivedAttributes.insert ( " Closest vertex X" , str );
298
+ str = QLocale::system ().toString ( closestPointMapCoords.y (), ' g' , 10 );
299
+ derivedAttributes.insert ( " Closest vertex Y" , str );
300
+
301
+ if ( closestPoint.is3D () )
302
+ {
303
+ str = QLocale::system ().toString ( closestPoint.z (), ' g' , 10 );
304
+ derivedAttributes.insert ( " Closest vertex Z" , str );
305
+ }
306
+ if ( closestPoint.isMeasure () )
307
+ {
308
+ str = QLocale::system ().toString ( closestPoint.m (), ' g' , 10 );
309
+ derivedAttributes.insert ( " Closest vertex M" , str );
310
+ }
311
+ }
312
+
313
+ QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes ( QgsFeature *feature, QgsMapLayer *layer, const QgsPoint& layerPoint )
287
314
{
288
315
// Calculate derived attributes and insert:
289
316
// measure distance or area depending on geometry type
@@ -296,13 +323,25 @@ QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( QgsFeatur
296
323
calc.setEllipsoid ( ellipsoid );
297
324
calc.setSourceCrs ( layer->crs ().srsid () );
298
325
299
- QGis::WkbType wkbType = QGis::WKBNoGeometry ;
326
+ QgsWKBTypes::Type wkbType = QgsWKBTypes::NoGeometry ;
300
327
QGis::GeometryType geometryType = QGis::NoGeometry;
301
328
329
+ QgsVertexId vId;
330
+ QgsPointV2 closestPoint;
302
331
if ( feature->constGeometry () )
303
332
{
304
333
geometryType = feature->constGeometry ()->type ();
305
- wkbType = feature->constGeometry ()->wkbType ();
334
+ wkbType = feature->constGeometry ()->geometry ()->wkbType ();
335
+ // find closest vertex to clicked point
336
+ closestPoint = QgsGeometryUtils::closestVertex ( *feature->constGeometry ()->geometry (), QgsPointV2 ( layerPoint.x (), layerPoint.y () ), vId );
337
+ }
338
+
339
+ if ( QgsWKBTypes::isMultiType ( wkbType ) )
340
+ {
341
+ QString str = QLocale::system ().toString ( static_cast <QgsGeometryCollectionV2*>( feature->constGeometry ()->geometry () )->numGeometries () );
342
+ derivedAttributes.insert ( tr ( " Parts" ), str );
343
+ str = QLocale::system ().toString ( vId.part + 1 );
344
+ derivedAttributes.insert ( tr ( " Part number" ), str );
306
345
}
307
346
308
347
if ( geometryType == QGis::Line )
@@ -315,7 +354,11 @@ QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( QgsFeatur
315
354
derivedAttributes.insert ( tr ( " Length" ), str );
316
355
str = QLocale::system ().toString ( pline.size () );
317
356
derivedAttributes.insert ( tr ( " Vertices" ), str );
318
- if ( wkbType == QGis::WKBLineString || wkbType == QGis::WKBLineString25D )
357
+
358
+ // add details of closest vertex to identify point
359
+ closestVertexAttributes ( *feature->constGeometry ()->geometry (), vId, layer, derivedAttributes );
360
+
361
+ if ( QgsWKBTypes::flatType ( wkbType ) == QgsWKBTypes::LineString )
319
362
{
320
363
// Add the start and end points in as derived attributes
321
364
QgsPoint pnt = mCanvas ->mapSettings ().layerToMapCoordinates ( layer, pline.first () );
@@ -341,16 +384,33 @@ QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( QgsFeatur
341
384
convertMeasurement ( calc, perimeter, myDisplayUnits, false ); // perimeter and myDisplayUnits are out params
342
385
str = calc.textUnit ( perimeter, 3 , myDisplayUnits, false );
343
386
derivedAttributes.insert ( tr ( " Perimeter" ), str );
387
+
388
+ str = QLocale::system ().toString ( feature->constGeometry ()->geometry ()->nCoordinates () );
389
+ derivedAttributes.insert ( tr ( " Vertices" ), str );
390
+
391
+ // add details of closest vertex to identify point
392
+ closestVertexAttributes ( *feature->constGeometry ()->geometry (), vId, layer, derivedAttributes );
344
393
}
345
394
else if ( geometryType == QGis::Point &&
346
- ( wkbType == QGis::WKBPoint || wkbType == QGis::WKBPoint25D ) )
395
+ QgsWKBTypes::flatType ( wkbType ) == QgsWKBTypes:: Point )
347
396
{
348
397
// Include the x and y coordinates of the point as a derived attribute
349
398
QgsPoint pnt = mCanvas ->mapSettings ().layerToMapCoordinates ( layer, feature->constGeometry ()->asPoint () );
350
399
QString str = QLocale::system ().toString ( pnt.x (), ' g' , 10 );
351
400
derivedAttributes.insert ( " X" , str );
352
401
str = QLocale::system ().toString ( pnt.y (), ' g' , 10 );
353
402
derivedAttributes.insert ( " Y" , str );
403
+
404
+ if ( QgsWKBTypes::hasZ ( wkbType ) )
405
+ {
406
+ str = QLocale::system ().toString ( static_cast <QgsPointV2*>( feature->constGeometry ()->geometry () )->z (), ' g' , 10 );
407
+ derivedAttributes.insert ( " Z" , str );
408
+ }
409
+ if ( QgsWKBTypes::hasM ( wkbType ) )
410
+ {
411
+ str = QLocale::system ().toString ( static_cast <QgsPointV2*>( feature->constGeometry ()->geometry () )->m (), ' g' , 10 );
412
+ derivedAttributes.insert ( " M" , str );
413
+ }
354
414
}
355
415
356
416
return derivedAttributes;
0 commit comments