@@ -81,6 +81,8 @@ class TestQgsGeometry : public QObject
81
81
void smoothCheck ();
82
82
83
83
void dataStream ();
84
+
85
+ void exportToGeoJSON ();
84
86
85
87
private:
86
88
/* * A helper method to do a render check to see if the geometry op is as expected */
@@ -739,6 +741,51 @@ void TestQgsGeometry::dataStream()
739
741
QVERIFY ( resultGeometry.isEmpty () );
740
742
}
741
743
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
+
742
789
bool TestQgsGeometry::renderCheck ( const QString& theTestName, const QString& theComment, int mismatchCount )
743
790
{
744
791
mReport += " <h2>" + theTestName + " </h2>\n " ;
0 commit comments