Skip to content

Commit 1f0d24a

Browse files
committed
[FEATURE] Show z/m values in derived info for identify tool
For lines/polygons, identify tool will show vertex number and x/y/z/m for nearest vertex to identify point. Also add number of parts and part number to results for collections.
1 parent 57c22e5 commit 1f0d24a

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

src/gui/qgsmaptoolidentify.cpp

+66-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include "qgsproject.h"
3636
#include "qgsmaplayerregistry.h"
3737
#include "qgsrendererv2.h"
38+
#include "qgsgeometryutils.h"
39+
#include "qgsgeometrycollectionv2.h"
3840

3941
#include <QSettings>
4042
#include <QMouseEvent>
@@ -265,7 +267,7 @@ bool QgsMapToolIdentify::identifyVectorLayer( QList<IdentifyResult> *results, Qg
265267

266268
featureCount++;
267269

268-
derivedAttributes.unite( featureDerivedAttributes( &( *f_it ), layer ) );
270+
derivedAttributes.unite( featureDerivedAttributes( &( *f_it ), layer, toLayerCoordinates( layer, point ) ) );
269271

270272
derivedAttributes.insert( tr( "feature id" ), fid < 0 ? tr( "new feature" ) : FID_TO_STRING( fid ) );
271273

@@ -283,7 +285,32 @@ bool QgsMapToolIdentify::identifyVectorLayer( QList<IdentifyResult> *results, Qg
283285
return featureCount > 0;
284286
}
285287

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 )
287314
{
288315
// Calculate derived attributes and insert:
289316
// measure distance or area depending on geometry type
@@ -296,13 +323,25 @@ QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( QgsFeatur
296323
calc.setEllipsoid( ellipsoid );
297324
calc.setSourceCrs( layer->crs().srsid() );
298325

299-
QGis::WkbType wkbType = QGis::WKBNoGeometry;
326+
QgsWKBTypes::Type wkbType = QgsWKBTypes::NoGeometry;
300327
QGis::GeometryType geometryType = QGis::NoGeometry;
301328

329+
QgsVertexId vId;
330+
QgsPointV2 closestPoint;
302331
if ( feature->constGeometry() )
303332
{
304333
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 );
306345
}
307346

308347
if ( geometryType == QGis::Line )
@@ -315,7 +354,11 @@ QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( QgsFeatur
315354
derivedAttributes.insert( tr( "Length" ), str );
316355
str = QLocale::system().toString( pline.size() );
317356
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 )
319362
{
320363
// Add the start and end points in as derived attributes
321364
QgsPoint pnt = mCanvas->mapSettings().layerToMapCoordinates( layer, pline.first() );
@@ -341,16 +384,33 @@ QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( QgsFeatur
341384
convertMeasurement( calc, perimeter, myDisplayUnits, false ); // perimeter and myDisplayUnits are out params
342385
str = calc.textUnit( perimeter, 3, myDisplayUnits, false );
343386
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 );
344393
}
345394
else if ( geometryType == QGis::Point &&
346-
( wkbType == QGis::WKBPoint || wkbType == QGis::WKBPoint25D ) )
395+
QgsWKBTypes::flatType( wkbType ) == QgsWKBTypes::Point )
347396
{
348397
// Include the x and y coordinates of the point as a derived attribute
349398
QgsPoint pnt = mCanvas->mapSettings().layerToMapCoordinates( layer, feature->constGeometry()->asPoint() );
350399
QString str = QLocale::system().toString( pnt.x(), 'g', 10 );
351400
derivedAttributes.insert( "X", str );
352401
str = QLocale::system().toString( pnt.y(), 'g', 10 );
353402
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+
}
354414
}
355415

356416
return derivedAttributes;

src/gui/qgsmaptoolidentify.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
163163
/** Transforms the measurements of derived attributes in the desired units*/
164164
virtual QGis::UnitType displayUnits();
165165

166-
QMap< QString, QString > featureDerivedAttributes( QgsFeature *feature, QgsMapLayer *layer );
166+
QMap< QString, QString > featureDerivedAttributes( QgsFeature *feature, QgsMapLayer *layer, const QgsPoint& layerPoint = QgsPoint() );
167+
168+
/** Adds details of the closest vertex to derived attributes
169+
*/
170+
void closestVertexAttributes( const QgsAbstractGeometryV2& geometry, QgsVertexId vId, QgsMapLayer *layer, QMap< QString, QString >& derivedAttributes );
167171

168172
// Last point in canvas CRS
169173
QgsPoint mLastPoint;

0 commit comments

Comments
 (0)