Skip to content

Commit 8d31372

Browse files
committedNov 20, 2015
[BUGFIX] Multipoint asJSON
Fixes #13855 Multipoint asJSON made MultiPoint as Multilinestring with only 1 point by linestring. This bugfix adds tests. Conflicts: src/core/geometry/qgsmultipointv2.cpp tests/src/core/testqgsgeometry.cpp
1 parent 4f4fe59 commit 8d31372

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed
 

‎src/core/geometry/qgsmultipointv2.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,19 @@ QDomElement QgsMultiPointV2::asGML3( QDomDocument& doc, int precision, const QSt
6363

6464
QString QgsMultiPointV2::asJSON( int precision ) const
6565
{
66-
QString json = "{\"type\": \"MultiPoint\", \"coordinates\": [";
66+
QString json = "{\"type\": \"MultiPoint\", \"coordinates\": ";
67+
68+
QList<QgsPointV2> pts;
6769
foreach ( const QgsAbstractGeometryV2 *geom, mGeometries )
6870
{
6971
if ( dynamic_cast<const QgsPointV2*>( geom ) )
7072
{
7173
const QgsPointV2* point = static_cast<const QgsPointV2*>( geom );
72-
json += QgsGeometryUtils::pointsToJSON( QList<QgsPointV2>() << *point, precision ) + ", ";
74+
pts << *point;
7375
}
7476
}
75-
if ( json.endsWith( ", " ) )
76-
{
77-
json.chop( 2 ); // Remove last ", "
78-
}
79-
json += "] }";
77+
json += QgsGeometryUtils::pointsToJSON( pts, precision );
78+
json += " }";
8079
return json;
8180
}
8281

‎tests/src/core/testqgsgeometry.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class TestQgsGeometry : public QObject
8080
void bufferCheck();
8181
void smoothCheck();
8282

83+
void exportToGeoJSON();
84+
8385
private:
8486
/** A helper method to do a render check to see if the geometry op is as expected */
8587
bool renderCheck( QString theTestName, QString theComment = "", int mismatchCount = 0 );
@@ -706,6 +708,51 @@ void TestQgsGeometry::smoothCheck()
706708
QVERIFY( QgsGeometry::compare( multipoly, expectedMultiPoly ) );
707709
}
708710

711+
void TestQgsGeometry::exportToGeoJSON()
712+
{
713+
//Point
714+
QString wkt = "Point (40 50)";
715+
QScopedPointer<QgsGeometry> geom( QgsGeometry::fromWkt( wkt ) );
716+
QString obtained = geom->exportToGeoJSON();
717+
QString geojson = "{\"type\": \"Point\", \"coordinates\": [40, 50]}";
718+
QCOMPARE( obtained, geojson );
719+
720+
//MultiPoint
721+
wkt = "MultiPoint (0 0, 10 0, 10 10, 20 10)";
722+
geom.reset( QgsGeometry::fromWkt( wkt ) );
723+
obtained = geom->exportToGeoJSON();
724+
geojson = "{\"type\": \"MultiPoint\", \"coordinates\": [ [0, 0], [10, 0], [10, 10], [20, 10]] }";
725+
QCOMPARE( obtained, geojson );
726+
727+
//Linestring
728+
wkt = "LineString(0 0, 10 0, 10 10, 20 10)";
729+
geom.reset( QgsGeometry::fromWkt( wkt ) );
730+
obtained = geom->exportToGeoJSON();
731+
geojson = "{\"type\": \"LineString\", \"coordinates\": [ [0, 0], [10, 0], [10, 10], [20, 10]]}";
732+
QCOMPARE( obtained, geojson );
733+
734+
//MultiLineString
735+
wkt = "MultiLineString ((0 0, 10 0, 10 10, 20 10),(30 30, 40 30, 40 40, 50 40))";
736+
geom.reset( QgsGeometry::fromWkt( wkt ) );
737+
obtained = geom->exportToGeoJSON();
738+
geojson = "{\"type\": \"MultiLineString\", \"coordinates\": [[ [0, 0], [10, 0], [10, 10], [20, 10]], [ [30, 30], [40, 30], [40, 40], [50, 40]]] }";
739+
QCOMPARE( obtained, geojson );
740+
741+
//Polygon
742+
wkt = "Polygon ((0 0, 10 0, 10 10, 0 10, 0 0 ),(2 2, 4 2, 4 4, 2 4, 2 2))";
743+
geom.reset( QgsGeometry::fromWkt( wkt ) );
744+
obtained = geom->exportToGeoJSON();
745+
geojson = "{\"type\": \"Polygon\", \"coordinates\": [[ [0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], [ [2, 2], [4, 2], [4, 4], [2, 4], [2, 2]]] }";
746+
QCOMPARE( obtained, geojson );
747+
748+
//MultiPolygon
749+
wkt = "MultiPolygon (((0 0, 10 0, 10 10, 0 10, 0 0 )),((2 2, 4 2, 4 4, 2 4, 2 2)))";
750+
geom.reset( QgsGeometry::fromWkt( wkt ) );
751+
obtained = geom->exportToGeoJSON();
752+
geojson = "{\"type\": \"MultiPolygon\", \"coordinates\": [[[ [0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]], [[ [2, 2], [4, 2], [4, 4], [2, 4], [2, 2]]]] }";
753+
QCOMPARE( obtained, geojson );
754+
}
755+
709756
bool TestQgsGeometry::renderCheck( QString theTestName, QString theComment, int mismatchCount )
710757
{
711758
mReport += "<h2>" + theTestName + "</h2>\n";

0 commit comments

Comments
 (0)
Failed to load comments.