Skip to content

Commit 1d8faf2

Browse files
committed
do not convert multiline to multipolygon if one of the part failed, also consider case of 3 vertices line with first and last vertex identical
1 parent f538c8b commit 1d8faf2

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

src/core/qgsgeometry.cpp

+19-16
Original file line numberDiff line numberDiff line change
@@ -6227,9 +6227,10 @@ QgsGeometry* QgsGeometry::convertToPolygon( bool destMultipart )
62276227
for ( QgsMultiPolyline::iterator multiLineIt = multiLine.begin(); multiLineIt != multiLine.end(); ++multiLineIt )
62286228
{
62296229
// do not create polygon for a 1 segment line
6230-
// this does not consider the special case where line has 2 segments with first and last node identical
62316230
if (( *multiLineIt ).count() < 3 )
6232-
continue;
6231+
return 0;
6232+
if (( *multiLineIt ).count() == 3 && ( *multiLineIt ).first() == ( *multiLineIt ).last() )
6233+
return 0;
62336234

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

62586259
// do not create polygon for a 1 segment line
6259-
if ( line.count() >= 3 )
6260-
{
6261-
// add closing node
6262-
if ( line.first() != line.last() )
6263-
line << line.first();
6260+
if ( line.count() < 3 )
6261+
return 0;
6262+
if ( line.count() == 3 && line.first() == line.last() )
6263+
return 0;
62646264

6265-
// destination is multipart
6266-
if ( destMultipart )
6267-
{
6268-
return fromMultiPolygon( QgsMultiPolygon() << ( QgsPolygon() << line ) );
6269-
}
6270-
else
6271-
{
6272-
return fromPolygon( QgsPolygon() << line );
6273-
}
6265+
// add closing node
6266+
if ( line.first() != line.last() )
6267+
line << line.first();
6268+
6269+
// destination is multipart
6270+
if ( destMultipart )
6271+
{
6272+
return fromMultiPolygon( QgsMultiPolygon() << ( QgsPolygon() << line ) );
6273+
}
6274+
else
6275+
{
6276+
return fromPolygon( QgsPolygon() << line );
62746277
}
62756278
}
62766279
return 0;

tests/src/python/test_qgsgeometry.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ def testConvertToType(self):
11941194
######## TO LINE ########
11951195
# POINT TO LINE
11961196
point = QgsGeometry.fromPoint(QgsPoint(1,1))
1197-
assert point.convertToType(QGis.Line, False) !=0 , "convertToType with a point should return a null geometry"
1197+
assert point.convertToType(QGis.Line, False) is None , "convertToType with a point should return a null geometry"
11981198
# MULTIPOINT TO LINE
11991199
multipoint = QgsGeometry.fromMultiPoint(points[0][0])
12001200
wkt = multipoint.convertToType(QGis.Line, False).exportToWkt()
@@ -1258,6 +1258,12 @@ def testConvertToType(self):
12581258
wkt = line.convertToType(QGis.Polygon, False).exportToWkt()
12591259
expWkt = "POLYGON((0 0,1 0,1 1,2 1,2 2,0 2,0 0))"
12601260
assert compareWkt( expWkt, wkt ), "convertToType failed: from line to polygon. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1261+
# LINE ( 3 vertices, with first = last ) TO POLYGON
1262+
line = QgsGeometry.fromPolyline([QgsPoint(1,1),QgsPoint(0,0),QgsPoint(1,1)])
1263+
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"
1264+
# MULTILINE ( with a part of 3 vertices, with first = last ) TO MULTIPOLYGON
1265+
multiline = QgsGeometry.fromMultiPolyline([points[0][0],[QgsPoint(1,1),QgsPoint(0,0),QgsPoint(1,1)]])
1266+
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"
12611267
# LINE TO MULTIPOLYGON
12621268
line = QgsGeometry.fromPolyline(points[0][0])
12631269
wkt = line.convertToType(QGis.Polygon, True).exportToWkt()

0 commit comments

Comments
 (0)