Skip to content

Commit

Permalink
Merge pull request #3946 from mhugent/fix_compoundcurve_area
Browse files Browse the repository at this point in the history
Fix compoundcurve area
  • Loading branch information
mhugent authored Jan 5, 2017
2 parents 0c49391 + 64b0b10 commit f4c0cf8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
3 changes: 1 addition & 2 deletions python/core/geometry/qgscurvev2.sip
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class QgsCurveV2: public QgsAbstractGeometryV2
*/
virtual int numPoints() const = 0;

/** Calculates the area of the curve. Derived classes should override this
* to return the correct area of the curve.
/** Sums up the area of the curve by iterating over the vertices (shoelace formula).
*/
virtual void sumUpArea( double& sum ) const = 0;

Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgscurvepolygonv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ double QgsCurvePolygonV2::area() const

double totalArea = 0.0;

if ( mExteriorRing->isClosed() )
if ( mExteriorRing->isRing() )
{
double area = 0.0;
mExteriorRing->sumUpArea( area );
Expand All @@ -376,7 +376,7 @@ double QgsCurvePolygonV2::area() const
for ( ; ringIt != mInteriorRings.constEnd(); ++ringIt )
{
double area = 0.0;
if (( *ringIt )->isClosed() )
if (( *ringIt )->isRing() )
{
( *ringIt )->sumUpArea( area );
totalArea -= qAbs( area );
Expand Down
3 changes: 1 addition & 2 deletions src/core/geometry/qgscurvev2.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ class CORE_EXPORT QgsCurveV2: public QgsAbstractGeometryV2
*/
virtual int numPoints() const = 0;

/** Calculates the area of the curve. Derived classes should override this
* to return the correct area of the curve.
/** Sums up the area of the curve by iterating over the vertices (shoelace formula).
*/
virtual void sumUpArea( double& sum ) const = 0;

Expand Down
3 changes: 0 additions & 3 deletions src/core/geometry/qgslinestringv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,9 +837,6 @@ QgsPointV2 QgsLineStringV2::centroid() const
void QgsLineStringV2::sumUpArea( double& sum ) const
{
int maxIndex = numPoints() - 1;
if ( maxIndex == 1 )
return; //no area, just a single line

for ( int i = 0; i < maxIndex; ++i )
{
sum += 0.5 * ( mX.at( i ) * mY.at( i + 1 ) - mY.at( i ) * mX.at( i + 1 ) );
Expand Down
33 changes: 30 additions & 3 deletions tests/src/core/testqgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

//qgis includes...
#include <qgsapplication.h>
#include "qgscompoundcurvev2.h"
#include <qgsgeometry.h>
#include "qgsgeometryutils.h"
#include <qgspoint.h>
Expand Down Expand Up @@ -60,6 +61,7 @@ class TestQgsGeometry : public QObject
void isEmpty();
void pointV2(); //test QgsPointV2
void lineStringV2(); //test QgsLineStringV2
void compoundCurveV2(); //test QgsCompoundCurveV2
void polygonV2(); //test QgsPolygonV2

void fromQgsPoint();
Expand Down Expand Up @@ -2049,13 +2051,13 @@ void TestQgsGeometry::lineStringV2()
QCOMPARE( area, 1.0 );
l36.setPoints( QgsPointSequenceV2() << QgsPointV2( 5, 10 ) << QgsPointV2( 10, 10 ) );
l36.sumUpArea( area );
QCOMPARE( area, 1.0 );
QVERIFY( qgsDoubleNear( area, -24 ) );
l36.setPoints( QgsPointSequenceV2() << QgsPointV2( 0, 0 ) << QgsPointV2( 2, 0 ) << QgsPointV2( 2, 2 ) );
l36.sumUpArea( area );
QVERIFY( qgsDoubleNear( area, 3.0 ) );
QVERIFY( qgsDoubleNear( area, -22 ) );
l36.setPoints( QgsPointSequenceV2() << QgsPointV2( 0, 0 ) << QgsPointV2( 2, 0 ) << QgsPointV2( 2, 2 ) << QgsPointV2( 0, 2 ) );
l36.sumUpArea( area );
QVERIFY( qgsDoubleNear( area, 7.0 ) );
QVERIFY( qgsDoubleNear( area, -18 ) );

//boundingBox - test that bounding box is updated after every modification to the line string
QgsLineStringV2 l37;
Expand Down Expand Up @@ -2140,6 +2142,31 @@ void TestQgsGeometry::lineStringV2()
QVERIFY( l39.numPoints() == 0 );
}

void TestQgsGeometry::compoundCurveV2()
{
//test that area of a compound curve ring is equal to a closed linestring with the same vertices
QgsCompoundCurveV2 cc;
QgsLineStringV2* l1 = new QgsLineStringV2();
l1->setPoints( QgsPointSequenceV2() << QgsPointV2( 1, 1 ) << QgsPointV2( 0, 2 ) );
cc.addCurve( l1 );
QgsLineStringV2* l2 = new QgsLineStringV2();
l2->setPoints( QgsPointSequenceV2() << QgsPointV2( 0, 2 ) << QgsPointV2( -1, 0 ) << QgsPointV2( 0, -1 ) );
cc.addCurve( l2 );
QgsLineStringV2* l3 = new QgsLineStringV2();
l3->setPoints( QgsPointSequenceV2() << QgsPointV2( 0, -1 ) << QgsPointV2( 1, 1 ) );
cc.addCurve( l3 );

double ccArea = 0.0;
cc.sumUpArea( ccArea );

QgsLineStringV2 ls;
ls.setPoints( QgsPointSequenceV2() << QgsPointV2( 1, 1 ) << QgsPointV2( 0, 2 ) << QgsPointV2( -1, 0 ) << QgsPointV2( 0, -1 )
<< QgsPointV2( 1, 1 ) );
double lsArea = 0.0;
ls.sumUpArea( lsArea );
QVERIFY( qgsDoubleNear( ccArea, lsArea ) );
}

void TestQgsGeometry::polygonV2()
{
//test constructor
Expand Down

0 comments on commit f4c0cf8

Please sign in to comment.