Skip to content

Commit

Permalink
allow adding polygons to multipolygons. fixes split part tool (refs #…
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Jun 12, 2015
1 parent b113d2a commit 5b2b3a4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
48 changes: 39 additions & 9 deletions src/core/geometry/qgsgeometryeditutils.cpp
Expand Up @@ -110,22 +110,52 @@ int QgsGeometryEditUtils::addPart( QgsAbstractGeometryV2* geom, QgsAbstractGeome
if ( geom->geometryType() == "MultiSurface" || geom->geometryType() == "MultiPolygon" )
{
QgsCurveV2* curve = dynamic_cast<QgsCurveV2*>( part );
if ( !curve || !curve->isClosed() || curve->numPoints() < 4 )
if ( curve && curve->isClosed() && curve->numPoints() >= 4 )
{
delete part; return 2;
QgsCurvePolygonV2 *poly = 0;
if ( curve->geometryType() == "LineString" )
{
poly = new QgsPolygonV2();
}
else
{
poly = new QgsCurvePolygonV2();
}
poly->setExteriorRing( curve );
added = geomCollection->addGeometry( poly );
}

QgsCurvePolygonV2* poly = 0;
if ( curve->geometryType() == "LineString" )
else if ( part->geometryType() == "Polygon" )
{
poly = new QgsPolygonV2();
added = geomCollection->addGeometry( part );
}
else if ( part->geometryType() == "MultiPolygon" )
{
QgsGeometryCollectionV2 *parts = dynamic_cast<QgsGeometryCollectionV2*>( part );

int i;
int n = geomCollection->numGeometries();
for ( i = 0; i < parts->numGeometries() && geomCollection->addGeometry( parts->geometryN( i ) ); i++ )
;

added = i == parts->numGeometries();
if ( !added )
{
while ( geomCollection->numGeometries() > n )
geomCollection->removeGeometry( n );
delete part; return 2;
}

while ( parts->numGeometries() > 0 )
{
parts->removeGeometry( 0 );
}

delete part;
}
else
{
poly = new QgsCurvePolygonV2();
delete part; return 2;
}
poly->setExteriorRing( curve );
added = geomCollection->addGeometry( poly );
}
else
{
Expand Down
13 changes: 13 additions & 0 deletions tests/src/python/test_qgsgeometry.py
Expand Up @@ -1150,6 +1150,19 @@ def testAddPart(self):
wkt = polygon.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )

mp = QgsGeometry.fromMultiPolygon( points[:1] )
p = QgsGeometry.fromPolygon( points[1] )

assert mp.addPartGeometry( p ) == 0
wkt = mp.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )

mp = QgsGeometry.fromMultiPolygon( points[:1] )
mp2 = QgsGeometry.fromMultiPolygon( points[1:] )
assert mp.addPartGeometry( mp2 ) == 0
wkt = mp.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )

def testConvertToType(self):
# 5-+-4 0-+-9 13-+-+-12
# | | | | | |
Expand Down

0 comments on commit 5b2b3a4

Please sign in to comment.