Skip to content
Permalink
Browse files

allow adding polygons to multipolygons. fixes split part tool (refs #…

  • Loading branch information
jef-n committed Jun 12, 2015
1 parent b113d2a commit 5b2b3a45a7c07d15b5c0d68c97f55520f8b61ddf
Showing with 52 additions and 9 deletions.
  1. +39 −9 src/core/geometry/qgsgeometryeditutils.cpp
  2. +13 −0 tests/src/python/test_qgsgeometry.py
@@ -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
{
@@ -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
# | | | | | |

0 comments on commit 5b2b3a4

Please sign in to comment.
You can’t perform that action at this time.