@@ -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+
10531100bool TestQgsGeometry::renderCheck ( const QString& theTestName, const QString& theComment, int mismatchCount )
10541101{
10551102 mReport += " <h2>" + theTestName + " </h2>\n " ;
0 commit comments