@@ -80,6 +80,8 @@ class TestQgsGeometry : public QObject
80
80
void bufferCheck ();
81
81
void smoothCheck ();
82
82
83
+ void exportToGeoJSON ();
84
+
83
85
private:
84
86
/* * A helper method to do a render check to see if the geometry op is as expected */
85
87
bool renderCheck ( QString theTestName, QString theComment = " " , int mismatchCount = 0 );
@@ -706,6 +708,51 @@ void TestQgsGeometry::smoothCheck()
706
708
QVERIFY ( QgsGeometry::compare ( multipoly, expectedMultiPoly ) );
707
709
}
708
710
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
+
709
756
bool TestQgsGeometry::renderCheck ( QString theTestName, QString theComment, int mismatchCount )
710
757
{
711
758
mReport += " <h2>" + theTestName + " </h2>\n " ;
0 commit comments