Skip to content

Commit 856be9b

Browse files
committedDec 22, 2015
Ensure that rings are closed when adding to polygons
1 parent 5838819 commit 856be9b

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed
 

‎src/core/geometry/qgspolygonv2.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ void QgsPolygonV2::addInteriorRing( QgsCurveV2* ring )
182182
ring = segmented;
183183
}
184184

185+
QgsLineStringV2* lineString = dynamic_cast< QgsLineStringV2*>( ring );
186+
if ( lineString && !lineString->isClosed() )
187+
{
188+
lineString->close();
189+
}
190+
185191
if ( mWkbType == QgsWKBTypes::Polygon25D )
186192
{
187193
ring->convertTo( QgsWKBTypes::LineString25D );
@@ -209,6 +215,12 @@ void QgsPolygonV2::setExteriorRing( QgsCurveV2* ring )
209215
ring = line;
210216
}
211217

218+
QgsLineStringV2* lineString = dynamic_cast< QgsLineStringV2*>( ring );
219+
if ( lineString && !lineString->isClosed() )
220+
{
221+
lineString->close();
222+
}
223+
212224
mExteriorRing = ring;
213225

214226
//set proper wkb type

‎tests/src/core/testqgsgeometry.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,6 +2152,16 @@ void TestQgsGeometry::polygonV2()
21522152
//retrieve exterior ring and check
21532153
QCOMPARE( *( static_cast< QgsLineStringV2* >( p1.exteriorRing() ) ), *ext );
21542154

2155+
//test that a non closed exterior ring will be automatically closed
2156+
ext = new QgsLineStringV2();
2157+
ext->setPoints( QList< QgsPointV2 >() << QgsPointV2( 0, 0 ) << QgsPointV2( 0, 10 ) << QgsPointV2( 10, 10 )
2158+
<< QgsPointV2( 10, 0 ) );
2159+
QVERIFY( !ext->isClosed() );
2160+
p1.setExteriorRing( ext );
2161+
QVERIFY( !p1.isEmpty() );
2162+
QVERIFY( p1.exteriorRing()->isClosed() );
2163+
QCOMPARE( p1.nCoordinates(), 5 );
2164+
21552165
//initial setting of exterior ring should set z/m type
21562166
QgsPolygonV2 p2;
21572167
ext = new QgsLineStringV2();
@@ -2228,35 +2238,44 @@ void TestQgsGeometry::polygonV2()
22282238
QCOMPARE( p6.interiorRing( 0 ), ring );
22292239
QVERIFY( !p6.interiorRing( 1 ) );
22302240

2241+
//add non-closed interior ring, should be closed automatically
2242+
ring = new QgsLineStringV2();
2243+
ring->setPoints( QList< QgsPointV2 >() << QgsPointV2( 0.1, 0.1 ) << QgsPointV2( 0.1, 0.9 ) << QgsPointV2( 0.9, 0.9 )
2244+
<< QgsPointV2( 0.9, 0.1 ) );
2245+
QVERIFY( !ring->isClosed() );
2246+
p6.addInteriorRing( ring );
2247+
QCOMPARE( p6.numInteriorRings(), 2 );
2248+
QVERIFY( p6.interiorRing( 1 )->isClosed() );
2249+
22312250
//try adding an interior ring with z to a 2d polygon, z should be dropped
22322251
ring = new QgsLineStringV2();
22332252
ring->setPoints( QList< QgsPointV2 >() << QgsPointV2( QgsWKBTypes::PointZ, 0.1, 0.1, 1 )
22342253
<< QgsPointV2( QgsWKBTypes::PointZ, 0.1, 0.2, 2 ) << QgsPointV2( QgsWKBTypes::PointZ, 0.2, 0.2, 3 )
22352254
<< QgsPointV2( QgsWKBTypes::PointZ, 0.2, 0.1, 4 ) << QgsPointV2( QgsWKBTypes::PointZ, 0.1, 0.1, 1 ) );
22362255
p6.addInteriorRing( ring );
2237-
QCOMPARE( p6.numInteriorRings(), 2 );
2256+
QCOMPARE( p6.numInteriorRings(), 3 );
22382257
QVERIFY( !p6.is3D() );
22392258
QVERIFY( !p6.isMeasure() );
22402259
QCOMPARE( p6.wkbType(), QgsWKBTypes::Polygon );
2241-
QVERIFY( p6.interiorRing( 1 ) );
2242-
QVERIFY( !p6.interiorRing( 1 )->is3D() );
2243-
QVERIFY( !p6.interiorRing( 1 )->isMeasure() );
2244-
QCOMPARE( p6.interiorRing( 1 )->wkbType(), QgsWKBTypes::LineString );
2260+
QVERIFY( p6.interiorRing( 2 ) );
2261+
QVERIFY( !p6.interiorRing( 2 )->is3D() );
2262+
QVERIFY( !p6.interiorRing( 2 )->isMeasure() );
2263+
QCOMPARE( p6.interiorRing( 2 )->wkbType(), QgsWKBTypes::LineString );
22452264

22462265
//try adding an interior ring with m to a 2d polygon, m should be dropped
22472266
ring = new QgsLineStringV2();
22482267
ring->setPoints( QList< QgsPointV2 >() << QgsPointV2( QgsWKBTypes::PointM, 0.1, 0.1, 0, 1 )
22492268
<< QgsPointV2( QgsWKBTypes::PointM, 0.1, 0.2, 0, 2 ) << QgsPointV2( QgsWKBTypes::PointM, 0.2, 0.2, 0, 3 )
22502269
<< QgsPointV2( QgsWKBTypes::PointM, 0.2, 0.1, 0, 4 ) << QgsPointV2( QgsWKBTypes::PointM, 0.1, 0.1, 0, 1 ) );
22512270
p6.addInteriorRing( ring );
2252-
QCOMPARE( p6.numInteriorRings(), 3 );
2271+
QCOMPARE( p6.numInteriorRings(), 4 );
22532272
QVERIFY( !p6.is3D() );
22542273
QVERIFY( !p6.isMeasure() );
22552274
QCOMPARE( p6.wkbType(), QgsWKBTypes::Polygon );
2256-
QVERIFY( p6.interiorRing( 2 ) );
2257-
QVERIFY( !p6.interiorRing( 2 )->is3D() );
2258-
QVERIFY( !p6.interiorRing( 2 )->isMeasure() );
2259-
QCOMPARE( p6.interiorRing( 2 )->wkbType(), QgsWKBTypes::LineString );
2275+
QVERIFY( p6.interiorRing( 3 ) );
2276+
QVERIFY( !p6.interiorRing( 3 )->is3D() );
2277+
QVERIFY( !p6.interiorRing( 3 )->isMeasure() );
2278+
QCOMPARE( p6.interiorRing( 3 )->wkbType(), QgsWKBTypes::LineString );
22602279

22612280
//addInteriorRing without z/m to PolygonZM
22622281
QgsPolygonV2 p6b;

0 commit comments

Comments
 (0)
Please sign in to comment.