Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
do not convert multiline to multipolygon if one of the part failed, a…
…lso consider case of 3 vertices line with first and last vertex identical
  • Loading branch information
3nids committed Mar 3, 2014
1 parent f538c8b commit 1d8faf2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
35 changes: 19 additions & 16 deletions src/core/qgsgeometry.cpp
Expand Up @@ -6227,9 +6227,10 @@ QgsGeometry* QgsGeometry::convertToPolygon( bool destMultipart )
for ( QgsMultiPolyline::iterator multiLineIt = multiLine.begin(); multiLineIt != multiLine.end(); ++multiLineIt )
{
// do not create polygon for a 1 segment line
// this does not consider the special case where line has 2 segments with first and last node identical
if (( *multiLineIt ).count() < 3 )
continue;
return 0;
if (( *multiLineIt ).count() == 3 && ( *multiLineIt ).first() == ( *multiLineIt ).last() )
return 0;

// add closing node
if (( *multiLineIt ).first() != ( *multiLineIt ).last() )
Expand All @@ -6256,21 +6257,23 @@ QgsGeometry* QgsGeometry::convertToPolygon( bool destMultipart )
QgsPolyline line = asPolyline();

// do not create polygon for a 1 segment line
if ( line.count() >= 3 )
{
// add closing node
if ( line.first() != line.last() )
line << line.first();
if ( line.count() < 3 )
return 0;
if ( line.count() == 3 && line.first() == line.last() )
return 0;

// destination is multipart
if ( destMultipart )
{
return fromMultiPolygon( QgsMultiPolygon() << ( QgsPolygon() << line ) );
}
else
{
return fromPolygon( QgsPolygon() << line );
}
// add closing node
if ( line.first() != line.last() )
line << line.first();

// destination is multipart
if ( destMultipart )
{
return fromMultiPolygon( QgsMultiPolygon() << ( QgsPolygon() << line ) );
}
else
{
return fromPolygon( QgsPolygon() << line );
}
}
return 0;
Expand Down
8 changes: 7 additions & 1 deletion tests/src/python/test_qgsgeometry.py
Expand Up @@ -1194,7 +1194,7 @@ def testConvertToType(self):
######## TO LINE ########
# POINT TO LINE
point = QgsGeometry.fromPoint(QgsPoint(1,1))
assert point.convertToType(QGis.Line, False) !=0 , "convertToType with a point should return a null geometry"
assert point.convertToType(QGis.Line, False) is None , "convertToType with a point should return a null geometry"
# MULTIPOINT TO LINE
multipoint = QgsGeometry.fromMultiPoint(points[0][0])
wkt = multipoint.convertToType(QGis.Line, False).exportToWkt()
Expand Down Expand Up @@ -1258,6 +1258,12 @@ def testConvertToType(self):
wkt = line.convertToType(QGis.Polygon, False).exportToWkt()
expWkt = "POLYGON((0 0,1 0,1 1,2 1,2 2,0 2,0 0))"
assert compareWkt( expWkt, wkt ), "convertToType failed: from line to polygon. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
# LINE ( 3 vertices, with first = last ) TO POLYGON
line = QgsGeometry.fromPolyline([QgsPoint(1,1),QgsPoint(0,0),QgsPoint(1,1)])
assert line.convertToType(QGis.Polygon, False) is None , "convertToType to polygon of a 3 vertices lines with first and last vertex identical should return a null geometry"
# MULTILINE ( with a part of 3 vertices, with first = last ) TO MULTIPOLYGON
multiline = QgsGeometry.fromMultiPolyline([points[0][0],[QgsPoint(1,1),QgsPoint(0,0),QgsPoint(1,1)]])
assert multiline.convertToType(QGis.Polygon, True) is None , "convertToType to polygon of a 3 vertices lines with first and last vertex identical should return a null geometry"
# LINE TO MULTIPOLYGON
line = QgsGeometry.fromPolyline(points[0][0])
wkt = line.convertToType(QGis.Polygon, True).exportToWkt()
Expand Down

0 comments on commit 1d8faf2

Please sign in to comment.