Skip to content

Commit a6081ba

Browse files
author
homann
committed
Fixed measurements from identify tool:
* Added support for 25D (by ignoring Z value completely) * Made the tool convert to the preferred units set for measurment tool git-svn-id: http://svn.osgeo.org/qgis/trunk@11474 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent f346a86 commit a6081ba

File tree

4 files changed

+90
-11
lines changed

4 files changed

+90
-11
lines changed

src/app/qgsmaptoolidentify.cpp

+52-2
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,9 @@ void QgsMapToolIdentify::identifyVectorLayer( const QgsPoint& point )
346346
if ( layer->geometryType() == QGis::Line )
347347
{
348348
double dist = calc.measure( f_it->geometry() );
349-
QString str = calc.textUnit( dist, 3, mCanvas->mapUnits(), false );
349+
QGis::UnitType myDisplayUnits;
350+
convertMeasurement( calc, dist, myDisplayUnits, false );
351+
QString str = calc.textUnit( dist, 3, myDisplayUnits, false ); // dist and myDisplayUnits are out params
350352
mResults->addDerivedAttribute( featureNode, tr( "Length" ), str );
351353
if ( f_it->geometry()->wkbType() == QGis::WKBLineString )
352354
{
@@ -364,7 +366,9 @@ void QgsMapToolIdentify::identifyVectorLayer( const QgsPoint& point )
364366
else if ( layer->geometryType() == QGis::Polygon )
365367
{
366368
double area = calc.measure( f_it->geometry() );
367-
QString str = calc.textUnit( area, 3, mCanvas->mapUnits(), true );
369+
QGis::UnitType myDisplayUnits;
370+
convertMeasurement( calc, area, myDisplayUnits, true ); // area and myDisplayUnits are out params
371+
QString str = calc.textUnit( area, 3, myDisplayUnits, true );
368372
mResults->addDerivedAttribute( featureNode, tr( "Area" ), str );
369373
}
370374
else if ( layer->geometryType() == QGis::Point )
@@ -534,3 +538,49 @@ void QgsMapToolIdentify::removeLayer( QString layerID )
534538
}
535539
}
536540
}
541+
542+
void QgsMapToolIdentify::convertMeasurement( QgsDistanceArea &calc, double &measure, QGis::UnitType &u, bool isArea )
543+
{
544+
// Helper for converting between meters and feet
545+
// The parameter &u is out only...
546+
547+
QGis::UnitType myUnits = mCanvas->mapUnits();
548+
if (( myUnits == QGis::Degrees || myUnits == QGis::Feet ) &&
549+
calc.ellipsoid() != "NONE" &&
550+
calc.hasCrsTransformEnabled() )
551+
{
552+
// Measuring on an ellipsoid returns meters, and so does using projections???
553+
myUnits = QGis::Meters;
554+
QgsDebugMsg( "We're measuring on an ellipsoid or using projections, the system is returning meters" );
555+
}
556+
557+
// Get the units for display
558+
QSettings settings;
559+
QString myDisplayUnitsTxt = settings.value( "/qgis/measure/displayunits", "meters" ).toString();
560+
561+
// Only convert between meters and feet
562+
if ( myUnits == QGis::Meters && myDisplayUnitsTxt == "feet" )
563+
{
564+
QgsDebugMsg( QString( "Converting %1 meters" ).arg( QString::number( measure ) ) );
565+
measure /= 0.3048;
566+
if ( isArea )
567+
{
568+
measure /= 0.3048;
569+
}
570+
QgsDebugMsg( QString( "to %1 feet" ).arg( QString::number( measure ) ) );
571+
myUnits = QGis::Feet;
572+
}
573+
if ( myUnits == QGis::Feet && myDisplayUnitsTxt == "meters" )
574+
{
575+
QgsDebugMsg( QString( "Converting %1 feet" ).arg( QString::number( measure ) ) );
576+
measure *= 0.3048;
577+
if ( isArea )
578+
{
579+
measure *= 0.3048;
580+
}
581+
QgsDebugMsg( QString( "to %1 meters" ).arg( QString::number( measure ) ) );
582+
myUnits = QGis::Meters;
583+
}
584+
585+
u = myUnits;
586+
}

src/app/qgsmaptoolidentify.h

+5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
#ifndef QGSMAPTOOLIDENTIFY_H
1818
#define QGSMAPTOOLIDENTIFY_H
1919

20+
#include "qgis.h"
2021
#include "qgsmaptool.h"
2122
#include "qgspoint.h"
2223
#include "qgsfeature.h"
24+
#include "qgsdistancearea.h"
2325

2426
#include <QObject>
2527

@@ -108,6 +110,9 @@ class QgsMapToolIdentify : public QgsMapTool
108110
//! list of identified features
109111
QgsFeatureList mFeatureList;
110112

113+
//! Private helper
114+
void convertMeasurement( QgsDistanceArea &calc, double &measure, QGis::UnitType &u, bool isArea );
115+
111116
private slots:
112117
// Let us know when the QgsIdentifyResults dialog box has been closed
113118
void resultsDialogGone();

src/core/qgsdistancearea.cpp

+31-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "qgsdistancearea.h"
3030
#include "qgsapplication.h"
3131
#include "qgslogger.h"
32-
#include "qgslogger.h"
3332

3433
// MSVC compiler doesn't have defined M_PI in math.h
3534
#ifndef M_PI
@@ -185,34 +184,48 @@ double QgsDistanceArea::measure( QgsGeometry* geometry )
185184
memcpy( &wkbType, ( wkb + 1 ), sizeof( wkbType ) );
186185

187186
// measure distance or area based on what is the type of geometry
187+
bool hasZptr = false;
188+
188189
switch ( wkbType )
189190
{
191+
case QGis::WKBLineString25D:
192+
hasZptr = true;
190193
case QGis::WKBLineString:
191-
measureLine( wkb, &res );
194+
measureLine( wkb, &res, hasZptr );
195+
QgsDebugMsg( "returning " + QString::number( res ) );
192196
return res;
193197

198+
case QGis::WKBMultiLineString25D:
199+
hasZptr = true;
194200
case QGis::WKBMultiLineString:
195201
count = *(( int* )( wkb + 5 ) );
196202
ptr = wkb + 9;
197203
for ( i = 0; i < count; i++ )
198204
{
199-
ptr = measureLine( ptr, &res );
205+
ptr = measureLine( ptr, &res, hasZptr );
200206
resTotal += res;
201207
}
208+
QgsDebugMsg( "returning " + QString::number( resTotal ) );
202209
return resTotal;
203210

211+
case QGis::WKBPolygon25D:
212+
hasZptr = true;
204213
case QGis::WKBPolygon:
205-
measurePolygon( wkb, &res );
214+
measurePolygon( wkb, &res, hasZptr );
215+
QgsDebugMsg( "returning " + QString::number( res ) );
206216
return res;
207217

218+
case QGis::WKBMultiPolygon25D:
219+
hasZptr = true;
208220
case QGis::WKBMultiPolygon:
209221
count = *(( int* )( wkb + 5 ) );
210222
ptr = wkb + 9;
211223
for ( i = 0; i < count; i++ )
212224
{
213-
ptr = measurePolygon( ptr, &res );
225+
ptr = measurePolygon( ptr, &res, hasZptr );
214226
resTotal += res;
215227
}
228+
QgsDebugMsg( "returning " + QString::number( resTotal ) );
216229
return resTotal;
217230

218231
default:
@@ -222,7 +235,7 @@ double QgsDistanceArea::measure( QgsGeometry* geometry )
222235
}
223236

224237

225-
unsigned char* QgsDistanceArea::measureLine( unsigned char* feature, double* area )
238+
unsigned char* QgsDistanceArea::measureLine( unsigned char* feature, double* area, bool hasZptr )
226239
{
227240
unsigned char *ptr = feature + 5;
228241
unsigned int nPoints = *(( int* )ptr );
@@ -239,6 +252,12 @@ unsigned char* QgsDistanceArea::measureLine( unsigned char* feature, double* are
239252
ptr += sizeof( double );
240253
y = *(( double * ) ptr );
241254
ptr += sizeof( double );
255+
if ( hasZptr )
256+
{
257+
// totally ignore Z value
258+
ptr += sizeof( double );
259+
}
260+
242261
points.append( QgsPoint( x, y ) );
243262
}
244263

@@ -313,7 +332,7 @@ double QgsDistanceArea::measureLine( const QgsPoint& p1, const QgsPoint& p2 )
313332
}
314333

315334

316-
unsigned char* QgsDistanceArea::measurePolygon( unsigned char* feature, double* area )
335+
unsigned char* QgsDistanceArea::measurePolygon( unsigned char* feature, double* area, bool hasZptr )
317336
{
318337
// get number of rings in the polygon
319338
unsigned int numRings = *(( int* )( feature + 1 + sizeof( int ) ) );
@@ -344,6 +363,11 @@ unsigned char* QgsDistanceArea::measurePolygon( unsigned char* feature, double*
344363
ptr += sizeof( double );
345364
y = *(( double * ) ptr );
346365
ptr += sizeof( double );
366+
if ( hasZptr )
367+
{
368+
// totally ignore Z value
369+
ptr += sizeof( double );
370+
}
347371

348372
pnt = QgsPoint( x, y );
349373

src/core/qgsdistancearea.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ class CORE_EXPORT QgsDistanceArea
9090
protected:
9191

9292
//! measures line distance, line points are extracted from WKB
93-
unsigned char* measureLine( unsigned char* feature, double* area );
93+
unsigned char* measureLine( unsigned char* feature, double* area, bool hasZptr = false );
9494
//! measures polygon area, vertices are extracted from WKB
95-
unsigned char* measurePolygon( unsigned char* feature, double* area );
95+
unsigned char* measurePolygon( unsigned char* feature, double* area, bool hasZptr = false );
9696

9797
/**
9898
calculates distance from two points on ellipsoid

0 commit comments

Comments
 (0)