Skip to content

Commit 00ea968

Browse files
committed
[BUGFIX] Multipoint asJSON
Fixes #13855 Multipoint asJSON made MultiPoint as Multilinestring with only 1 point by linestring. This bugfix adds tests.
1 parent 4203a7c commit 00ea968

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/core/geometry/qgsmultipointv2.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,23 @@ QDomElement QgsMultiPointV2::asGML3( QDomDocument& doc, int precision, const QSt
7373

7474
QString QgsMultiPointV2::asJSON( int precision ) const
7575
{
76-
QString json = "{\"type\": \"MultiPoint\", \"coordinates\": [";
76+
QString json = "{\"type\": \"MultiPoint\", \"coordinates\": ";
77+
78+
QList<QgsPointV2> pts;
7779
Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
7880
{
7981
if ( dynamic_cast<const QgsPointV2*>( geom ) )
8082
{
8183
const QgsPointV2* point = static_cast<const QgsPointV2*>( geom );
82-
json += QgsGeometryUtils::pointsToJSON( QList<QgsPointV2>() << *point, precision ) + ", ";
84+
pts << *point;
8385
}
8486
}
85-
if ( json.endsWith( ", " ) )
87+
json += QgsGeometryUtils::pointsToJSON( pts, precision );
88+
/*if ( json.endsWith( ", " ) )
8689
{
8790
json.chop( 2 ); // Remove last ", "
88-
}
89-
json += "] }";
91+
}*/
92+
json += " }";
9093
return json;
9194
}
9295

tests/src/core/testqgsgeometry.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class TestQgsGeometry : public QObject
8383
void smoothCheck();
8484

8585
void dataStream();
86+
87+
void exportToGeoJSON();
8688

8789
private:
8890
/** A helper method to do a render check to see if the geometry op is as expected */
@@ -1050,6 +1052,51 @@ void TestQgsGeometry::dataStream()
10501052
QVERIFY( resultGeometry.isEmpty() );
10511053
}
10521054

1055+
void TestQgsGeometry::exportToGeoJSON()
1056+
{
1057+
//Point
1058+
QString wkt = "Point (40 50)";
1059+
QScopedPointer<QgsGeometry> geom( QgsGeometry::fromWkt( wkt ) );
1060+
QString obtained = geom->exportToGeoJSON();
1061+
QString geojson = "{\"type\": \"Point\", \"coordinates\": [40, 50]}";
1062+
QCOMPARE( obtained, geojson );
1063+
1064+
//MultiPoint
1065+
wkt = "MultiPoint (0 0, 10 0, 10 10, 20 10)";
1066+
geom.reset( QgsGeometry::fromWkt( wkt ) );
1067+
obtained = geom->exportToGeoJSON();
1068+
geojson = "{\"type\": \"MultiPoint\", \"coordinates\": [ [0, 0], [10, 0], [10, 10], [20, 10]] }";
1069+
QCOMPARE( obtained, geojson );
1070+
1071+
//Linestring
1072+
wkt = "LineString(0 0, 10 0, 10 10, 20 10)";
1073+
geom.reset( QgsGeometry::fromWkt( wkt ) );
1074+
obtained = geom->exportToGeoJSON();
1075+
geojson = "{\"type\": \"LineString\", \"coordinates\": [ [0, 0], [10, 0], [10, 10], [20, 10]]}";
1076+
QCOMPARE( obtained, geojson );
1077+
1078+
//MultiLineString
1079+
wkt = "MultiLineString ((0 0, 10 0, 10 10, 20 10),(30 30, 40 30, 40 40, 50 40))";
1080+
geom.reset( QgsGeometry::fromWkt( wkt ) );
1081+
obtained = geom->exportToGeoJSON();
1082+
geojson = "{\"type\": \"MultiLineString\", \"coordinates\": [[ [0, 0], [10, 0], [10, 10], [20, 10]], [ [30, 30], [40, 30], [40, 40], [50, 40]]] }";
1083+
QCOMPARE( obtained, geojson );
1084+
1085+
//Polygon
1086+
wkt = "Polygon ((0 0, 10 0, 10 10, 0 10, 0 0 ),(2 2, 4 2, 4 4, 2 4, 2 2))";
1087+
geom.reset( QgsGeometry::fromWkt( wkt ) );
1088+
obtained = geom->exportToGeoJSON();
1089+
geojson = "{\"type\": \"Polygon\", \"coordinates\": [[ [0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], [ [2, 2], [4, 2], [4, 4], [2, 4], [2, 2]]] }";
1090+
QCOMPARE( obtained, geojson );
1091+
1092+
//MultiPolygon
1093+
wkt = "MultiPolygon (((0 0, 10 0, 10 10, 0 10, 0 0 )),((2 2, 4 2, 4 4, 2 4, 2 2)))";
1094+
geom.reset( QgsGeometry::fromWkt( wkt ) );
1095+
obtained = geom->exportToGeoJSON();
1096+
geojson = "{\"type\": \"MultiPolygon\", \"coordinates\": [[[ [0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]], [[ [2, 2], [4, 2], [4, 4], [2, 4], [2, 2]]]] }";
1097+
QCOMPARE( obtained, geojson );
1098+
}
1099+
10531100
bool TestQgsGeometry::renderCheck( const QString& theTestName, const QString& theComment, int mismatchCount )
10541101
{
10551102
mReport += "<h2>" + theTestName + "</h2>\n";

0 commit comments

Comments
 (0)