Fix #9951 (perimeter completely wrong with OTF on)

With OTF on and computation on an ellipsoid, the coordinates
were transformed twice(!) when measuring perimeter.
This commit is contained in:
Martin Dobias 2014-06-10 16:23:02 +07:00
parent 1d1eed448e
commit a19886d2f5
3 changed files with 54 additions and 2 deletions

View File

@ -99,6 +99,11 @@ class QgsDistanceArea
double computeDistanceBearing( const QgsPoint& p1, const QgsPoint& p2,
double* course1 = NULL, double* course2 = NULL );
//! uses flat / planimetric / Euclidean distance
double computeDistanceFlat( const QgsPoint& p1, const QgsPoint& p2 );
//! calculate distance with given coordinates (does not do a transform anymore)
double computeDistance( const QList<QgsPoint>& points );
/**
calculates area of polygon on ellipsoid

View File

@ -474,7 +474,7 @@ double QgsDistanceArea::measureLine( const QgsPoint &p1, const QgsPoint &p2 )
else
{
QgsDebugMsgLevel( "Cartesian calculation on canvas coordinates", 4 );
result = sqrt(( p2.x() - p1.x() ) * ( p2.x() - p1.x() ) + ( p2.y() - p1.y() ) * ( p2.y() - p1.y() ) );
result = computeDistanceFlat( p1, p2 );
}
}
catch ( QgsCsException &cse )
@ -565,7 +565,7 @@ const unsigned char *QgsDistanceArea::measurePolygon( const unsigned char* featu
if ( idx == 0 )
{
// exterior ring
*perimeter += measureLine( points );
*perimeter += computeDistance( points );
}
}
}
@ -712,6 +712,48 @@ double QgsDistanceArea::computeDistanceBearing(
return s;
}
double QgsDistanceArea::computeDistanceFlat( const QgsPoint& p1, const QgsPoint& p2 )
{
return sqrt( ( p2.x() - p1.x() ) * ( p2.x() - p1.x() ) + ( p2.y() - p1.y() ) * ( p2.y() - p1.y() ) );
}
double QgsDistanceArea::computeDistance( const QList<QgsPoint>& points )
{
if ( points.size() < 2 )
return 0;
double total = 0;
QgsPoint p1, p2;
try
{
p1 = points[0];
for ( QList<QgsPoint>::const_iterator i = points.begin(); i != points.end(); ++i )
{
p2 = *i;
if ( mEllipsoidalMode && ( mEllipsoid != GEO_NONE ) )
{
total += computeDistanceBearing( p1, p2 );
}
else
{
total += computeDistanceFlat( p1, p2 );
}
p1 = p2;
}
return total;
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse );
QgsMessageLog::logMessage( QObject::tr( "Caught a coordinate system exception while trying to transform a point. Unable to calculate line length." ) );
return 0.0;
}
}
///////////////////////////////////////////////////////////

View File

@ -130,6 +130,11 @@ class CORE_EXPORT QgsDistanceArea
double computeDistanceBearing( const QgsPoint& p1, const QgsPoint& p2,
double* course1 = NULL, double* course2 = NULL );
//! uses flat / planimetric / Euclidean distance
double computeDistanceFlat( const QgsPoint& p1, const QgsPoint& p2 );
//! calculate distance with given coordinates (does not do a transform anymore)
double computeDistance( const QList<QgsPoint>& points );
/**
calculates area of polygon on ellipsoid