Skip to content

Commit

Permalink
Fix area calculation when OTF active and no ellipsoid (fix #13601)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 15, 2015
1 parent 5ed3d1b commit 5654eec
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
12 changes: 12 additions & 0 deletions python/core/geometry/qgsgeometry.sip
Expand Up @@ -701,6 +701,18 @@ class QgsGeometry

/** Creates and returns a new geometry engine*/
static QgsGeometryEngine* createGeometryEngine( const QgsAbstractGeometryV2* geometry ) /Factory/;

/** Upgrades a point list from QgsPoint to QgsPointV2
* @param input list of QgsPoint objects to be upgraded
* @param output destination for list of points converted to QgsPointV2
*/
static void convertPointList( const QList<QgsPoint>& input, QList<QgsPointV2>& output );

/** Downgrades a point list from QgsPointV2 to QgsPoint
* @param input list of QgsPointV2 objects to be downgraded
* @param output destination for list of points converted to QgsPoint
*/
static void convertPointList( const QList<QgsPointV2>& input, QList<QgsPoint>& output );

}; // class QgsGeometry

10 changes: 9 additions & 1 deletion src/core/geometry/qgsgeometry.h
Expand Up @@ -746,8 +746,16 @@ class CORE_EXPORT QgsGeometry
*/
static QgsGeometryEngine* createGeometryEngine( const QgsAbstractGeometryV2* geometry );

//convert point list from v1 to v2
/** Upgrades a point list from QgsPoint to QgsPointV2
* @param input list of QgsPoint objects to be upgraded
* @param output destination for list of points converted to QgsPointV2
*/
static void convertPointList( const QList<QgsPoint>& input, QList<QgsPointV2>& output );

/** Downgrades a point list from QgsPointV2 to QgsPoint
* @param input list of QgsPointV2 objects to be downgraded
* @param output destination for list of points converted to QgsPoint
*/
static void convertPointList( const QList<QgsPointV2>& input, QList<QgsPoint>& output );

private:
Expand Down
15 changes: 6 additions & 9 deletions src/core/qgsdistancearea.cpp
Expand Up @@ -276,8 +276,9 @@ double QgsDistanceArea::measure( const QgsAbstractGeometryV2* geomV2 ) const
return 0.0;
}

if ( !mEllipsoidalMode )
if ( !mEllipsoidalMode || mEllipsoid == GEO_NONE )
{
//no transform required
if ( geomDimension == 1 )
{
return geomV2->length();
Expand All @@ -296,6 +297,8 @@ double QgsDistanceArea::measure( const QgsAbstractGeometryV2* geomV2 ) const
double sum = 0;
for ( int i = 0; i < collection->numGeometries(); ++i )
{
//hmm... this is a bit broken. What if a collection consists of both lines and polygons?
//the sum will be a mash of both areas and lengths
sum += measure( collection->geometryN( i ) );
}
return sum;
Expand Down Expand Up @@ -596,15 +599,9 @@ double QgsDistanceArea::measurePolygon( const QgsCurveV2* curve ) const

QList<QgsPointV2> linePointsV2;
curve->points( linePointsV2 );

QList<QgsPoint> linePoints;
QList<QgsPointV2>::const_iterator ptIt = linePointsV2.constBegin();
for ( ; ptIt != linePointsV2.constEnd(); ++ptIt )
{
linePoints.append( mCoordTransform->transform( QPoint( ptIt->x(), ptIt->y() ) ) );
}

return computePolygonArea( linePoints );
QgsGeometry::convertPointList( linePointsV2, linePoints );
return measurePolygon( linePoints );
}


Expand Down
16 changes: 15 additions & 1 deletion tests/src/core/testqgsdistancearea.cpp
Expand Up @@ -23,6 +23,8 @@
#include <qgsdistancearea.h>
#include <qgspoint.h>
#include "qgslogger.h"
#include "qgsgeometryfactory.h"
#include "qgsgeometry.h"

class TestQgsDistanceArea: public QObject
{
Expand All @@ -34,6 +36,7 @@ class TestQgsDistanceArea: public QObject
void basic();
void test_distances();
void unit_conversions();
void regression13601();
};

void TestQgsDistanceArea::initTestCase()
Expand Down Expand Up @@ -163,7 +166,18 @@ void TestQgsDistanceArea::unit_conversions()
QString myTxt = QgsDistanceArea::textUnit( inputValue, 7, inputUnit, true, false );
QString expectedTxt = QLocale::system().toString( 2.4710538146717, 'g', 1 + 7 );
QVERIFY( myTxt.startsWith( expectedTxt ) ); // Ignore units for now.
};
}

void TestQgsDistanceArea::regression13601()
{
//test regression #13601
QgsDistanceArea calc;
calc.setEllipsoidalMode( true );
calc.setEllipsoid( "NONE" );
calc.setSourceCrs( 1108L );
QgsGeometry geom( QgsGeometryFactory::geomFromWkt("Polygon ((252000 1389000, 265000 1389000, 265000 1385000, 252000 1385000, 252000 1389000))") );
QVERIFY( qgsDoubleNear( calc.measure( &geom ), 52000000, 0.0001 ) );
}

QTEST_MAIN( TestQgsDistanceArea )
#include "testqgsdistancearea.moc"
Expand Down

0 comments on commit 5654eec

Please sign in to comment.