Skip to content

Commit a860af5

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.
1 parent 69eb659 commit a860af5

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed
 

‎src/core/geometry/qgsmultipointv2.cpp

Lines changed: 8 additions & 5 deletions
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

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

8383
void dataStream();
84+
85+
void exportToGeoJSON();
8486

8587
private:
8688
/** A helper method to do a render check to see if the geometry op is as expected */
@@ -739,6 +741,51 @@ void TestQgsGeometry::dataStream()
739741
QVERIFY( resultGeometry.isEmpty() );
740742
}
741743

744+
void TestQgsGeometry::exportToGeoJSON()
745+
{
746+
//Point
747+
QString wkt = "Point (40 50)";
748+
QScopedPointer<QgsGeometry> geom( QgsGeometry::fromWkt( wkt ) );
749+
QString obtained = geom->exportToGeoJSON();
750+
QString geojson = "{\"type\": \"Point\", \"coordinates\": [40, 50]}";
751+
QCOMPARE( obtained, geojson );
752+
753+
//MultiPoint
754+
wkt = "MultiPoint (0 0, 10 0, 10 10, 20 10)";
755+
geom.reset( QgsGeometry::fromWkt( wkt ) );
756+
obtained = geom->exportToGeoJSON();
757+
geojson = "{\"type\": \"MultiPoint\", \"coordinates\": [ [0, 0], [10, 0], [10, 10], [20, 10]] }";
758+
QCOMPARE( obtained, geojson );
759+
760+
//Linestring
761+
wkt = "LineString(0 0, 10 0, 10 10, 20 10)";
762+
geom.reset( QgsGeometry::fromWkt( wkt ) );
763+
obtained = geom->exportToGeoJSON();
764+
geojson = "{\"type\": \"LineString\", \"coordinates\": [ [0, 0], [10, 0], [10, 10], [20, 10]]}";
765+
QCOMPARE( obtained, geojson );
766+
767+
//MultiLineString
768+
wkt = "MultiLineString ((0 0, 10 0, 10 10, 20 10),(30 30, 40 30, 40 40, 50 40))";
769+
geom.reset( QgsGeometry::fromWkt( wkt ) );
770+
obtained = geom->exportToGeoJSON();
771+
geojson = "{\"type\": \"MultiLineString\", \"coordinates\": [[ [0, 0], [10, 0], [10, 10], [20, 10]], [ [30, 30], [40, 30], [40, 40], [50, 40]]] }";
772+
QCOMPARE( obtained, geojson );
773+
774+
//Polygon
775+
wkt = "Polygon ((0 0, 10 0, 10 10, 0 10, 0 0 ),(2 2, 4 2, 4 4, 2 4, 2 2))";
776+
geom.reset( QgsGeometry::fromWkt( wkt ) );
777+
obtained = geom->exportToGeoJSON();
778+
geojson = "{\"type\": \"Polygon\", \"coordinates\": [[ [0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], [ [2, 2], [4, 2], [4, 4], [2, 4], [2, 2]]] }";
779+
QCOMPARE( obtained, geojson );
780+
781+
//MultiPolygon
782+
wkt = "MultiPolygon (((0 0, 10 0, 10 10, 0 10, 0 0 )),((2 2, 4 2, 4 4, 2 4, 2 2)))";
783+
geom.reset( QgsGeometry::fromWkt( wkt ) );
784+
obtained = geom->exportToGeoJSON();
785+
geojson = "{\"type\": \"MultiPolygon\", \"coordinates\": [[[ [0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]], [[ [2, 2], [4, 2], [4, 4], [2, 4], [2, 2]]]] }";
786+
QCOMPARE( obtained, geojson );
787+
}
788+
742789
bool TestQgsGeometry::renderCheck( const QString& theTestName, const QString& theComment, int mismatchCount )
743790
{
744791
mReport += "<h2>" + theTestName + "</h2>\n";

0 commit comments

Comments
 (0)
Please sign in to comment.