Skip to content

Commit 477cd91

Browse files
committed
[BUGFIX] QgsCurvePolygonV2::asGML, add interior ring only if one exists
1 parent 3130f80 commit 477cd91

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/core/geometry/qgscurvepolygonv2.cpp

+22-14
Original file line numberDiff line numberDiff line change
@@ -300,16 +300,20 @@ QDomElement QgsCurvePolygonV2::asGML2( QDomDocument& doc, int precision, const Q
300300
elemOuterBoundaryIs.appendChild( outerRing );
301301
delete exteriorLineString;
302302
elemPolygon.appendChild( elemOuterBoundaryIs );
303-
QDomElement elemInnerBoundaryIs = doc.createElementNS( ns, "innerBoundaryIs" );
304-
for ( int i = 0, n = numInteriorRings(); i < n; ++i )
303+
int n = numInteriorRings();
304+
if ( n > 0 )
305305
{
306-
QgsLineStringV2* interiorLineString = interiorRing( i )->curveToLine();
307-
QDomElement innerRing = interiorLineString->asGML2( doc, precision, ns );
308-
innerRing.toElement().setTagName( "LinearRing" );
309-
elemInnerBoundaryIs.appendChild( innerRing );
310-
delete interiorLineString;
306+
QDomElement elemInnerBoundaryIs = doc.createElementNS( ns, "innerBoundaryIs" );
307+
for ( int i = 0; i < n; ++i )
308+
{
309+
QgsLineStringV2* interiorLineString = interiorRing( i )->curveToLine();
310+
QDomElement innerRing = interiorLineString->asGML2( doc, precision, ns );
311+
innerRing.toElement().setTagName( "LinearRing" );
312+
elemInnerBoundaryIs.appendChild( innerRing );
313+
delete interiorLineString;
314+
}
315+
elemPolygon.appendChild( elemInnerBoundaryIs );
311316
}
312-
elemPolygon.appendChild( elemInnerBoundaryIs );
313317
return elemPolygon;
314318
}
315319

@@ -321,14 +325,18 @@ QDomElement QgsCurvePolygonV2::asGML3( QDomDocument& doc, int precision, const Q
321325
outerRing.toElement().setTagName( "LinearRing" );
322326
elemExterior.appendChild( outerRing );
323327
elemCurvePolygon.appendChild( elemExterior );
324-
QDomElement elemInterior = doc.createElementNS( ns, "interior" );
325-
for ( int i = 0, n = numInteriorRings(); i < n; ++i )
328+
int n = numInteriorRings();
329+
if ( n > 0 )
326330
{
327-
QDomElement innerRing = interiorRing( i )->asGML2( doc, precision, ns );
328-
innerRing.toElement().setTagName( "LinearRing" );
329-
elemInterior.appendChild( innerRing );
331+
QDomElement elemInterior = doc.createElementNS( ns, "interior" );
332+
for ( int i = 0; i < n; ++i )
333+
{
334+
QDomElement innerRing = interiorRing( i )->asGML2( doc, precision, ns );
335+
innerRing.toElement().setTagName( "LinearRing" );
336+
elemInterior.appendChild( innerRing );
337+
}
338+
elemCurvePolygon.appendChild( elemInterior );
330339
}
331-
elemCurvePolygon.appendChild( elemInterior );
332340
return elemCurvePolygon;
333341
}
334342

tests/src/core/testqgsgeometry.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -2840,12 +2840,29 @@ void TestQgsGeometry::polygonV2()
28402840
<< QgsPointV2( QgsWKBTypes::Point, 0, 10 ) << QgsPointV2( QgsWKBTypes::Point, 10, 10 )
28412841
<< QgsPointV2( QgsWKBTypes::Point, 10, 0 ) << QgsPointV2( QgsWKBTypes::Point, 0, 0 ) );
28422842
exportPolygon.setExteriorRing( ext );
2843+
2844+
// GML document for compare
2845+
QDomDocument doc( "gml" );
2846+
2847+
// as GML2
2848+
QString expectedSimpleGML2( "<Polygon xmlns=\"gml\"><outerBoundaryIs xmlns=\"gml\"><LinearRing xmlns=\"gml\"><coordinates xmlns=\"gml\">0,0 0,10 10,10 10,0 0,0</coordinates></LinearRing></outerBoundaryIs></Polygon>" );
2849+
QCOMPARE( elemToString( exportPolygon.asGML2( doc ) ), expectedSimpleGML2 );
2850+
2851+
//as GML3
2852+
QString expectedSimpleGML3( "<Polygon xmlns=\"gml\"><exterior xmlns=\"gml\"><LinearRing xmlns=\"gml\"><coordinates xmlns=\"gml\">0,0 0,10 10,10 10,0 0,0</coordinates></LinearRing></exterior></Polygon>" );
2853+
QCOMPARE( elemToString( exportPolygon.asGML3( doc ) ), expectedSimpleGML3 );
2854+
2855+
// as JSON
2856+
QString expectedSimpleJson( "{\"type\": \"Polygon\", \"coordinates\": [[ [0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]] }" );
2857+
QCOMPARE( exportPolygon.asJSON(), expectedSimpleJson );
2858+
28432859
ring = new QgsLineStringV2();
28442860
ring->setPoints( QgsPointSequenceV2() << QgsPointV2( QgsWKBTypes::Point, 1, 1 )
28452861
<< QgsPointV2( QgsWKBTypes::Point, 1, 9 ) << QgsPointV2( QgsWKBTypes::Point, 9, 9 )
28462862
<< QgsPointV2( QgsWKBTypes::Point, 9, 1 ) << QgsPointV2( QgsWKBTypes::Point, 1, 1 ) );
28472863
exportPolygon.addInteriorRing( ring );
28482864

2865+
// as JSON
28492866
QString expectedJson( "{\"type\": \"Polygon\", \"coordinates\": [[ [0, 0], [0, 10], [10, 10], [10, 0], [0, 0]], [ [1, 1], [1, 9], [9, 9], [9, 1], [1, 1]]] }" );
28502867
QCOMPARE( exportPolygon.asJSON(), expectedJson );
28512868

@@ -2861,11 +2878,11 @@ void TestQgsGeometry::polygonV2()
28612878
<< QgsPointV2( QgsWKBTypes::Point, 4 / 3.0, 2 / 3.0 ) << QgsPointV2( QgsWKBTypes::Point, 2 / 3.0, 2 / 3.0 ) );
28622879
exportPolygonFloat.addInteriorRing( ring );
28632880

2881+
// as JSON
28642882
QString expectedJsonPrec3( "{\"type\": \"Polygon\", \"coordinates\": [[ [1.111, 1.111], [1.111, 11.111], [11.111, 11.111], [11.111, 1.111], [1.111, 1.111]], [ [0.667, 0.667], [0.667, 1.333], [1.333, 1.333], [1.333, 0.667], [0.667, 0.667]]] }" );
28652883
QCOMPARE( exportPolygonFloat.asJSON( 3 ), expectedJsonPrec3 );
28662884

28672885
// as GML2
2868-
QDomDocument doc( "gml" );
28692886
QString expectedGML2( "<Polygon xmlns=\"gml\"><outerBoundaryIs xmlns=\"gml\"><LinearRing xmlns=\"gml\"><coordinates xmlns=\"gml\">0,0 0,10 10,10 10,0 0,0</coordinates></LinearRing></outerBoundaryIs>" );
28702887
expectedGML2 += QString( "<innerBoundaryIs xmlns=\"gml\"><LinearRing xmlns=\"gml\"><coordinates xmlns=\"gml\">1,1 1,9 9,9 9,1 1,1</coordinates></LinearRing></innerBoundaryIs></Polygon>" );
28712888
QCOMPARE( elemToString( exportPolygon.asGML2( doc ) ), expectedGML2 );

0 commit comments

Comments
 (0)