Skip to content

Commit

Permalink
[Tests] GeoJSON stringToFields with a feature collection
Browse files Browse the repository at this point in the history
The methode `QgsJsonUtils::stringToFields` is a wrapper to `QgsOgrUtils::stringToFields`

The `QgsOgrUtils::stringToFields` is described as
> Attempts to retrieve the fields from a string representing a collection of features using OGR.

The GeoJSON feature collection string was not covered by tests.
  • Loading branch information
rldhont authored and nyalldawson committed Mar 17, 2020
1 parent 2f99dbd commit c67d258
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/src/core/testqgsogrutils.cpp
Expand Up @@ -479,6 +479,14 @@ void TestQgsOgrUtils::stringToFields()
QCOMPARE( fields.at( 0 ).type(), QVariant::String );
QCOMPARE( fields.at( 1 ).name(), QString( "height" ) );
QCOMPARE( fields.at( 1 ).type(), QVariant::Double );

// geojson string with 2 features
fields = QgsOgrUtils::stringToFields( QStringLiteral( "{ \"type\": \"FeatureCollection\",\"features\":[{\n\"type\": \"Feature\",\"geometry\": {\"type\": \"Point\",\"coordinates\": [125, 10]},\"properties\": {\"name\": \"Dinagat Islands\",\"height\":5.5}}, {\n\"type\": \"Feature\",\"geometry\": {\"type\": \"Point\",\"coordinates\": [110, 20]},\"properties\": {\"name\": \"Henry Gale Island\",\"height\":6.5}}]}" ), QTextCodec::codecForName( "System" ) );
QCOMPARE( fields.count(), 2 );
QCOMPARE( fields.at( 0 ).name(), QString( "name" ) );
QCOMPARE( fields.at( 0 ).type(), QVariant::String );
QCOMPARE( fields.at( 1 ).name(), QString( "height" ) );
QCOMPARE( fields.at( 1 ).type(), QVariant::Double );
}


Expand Down
8 changes: 8 additions & 0 deletions tests/src/python/test_qgsjsonutils.py
Expand Up @@ -96,6 +96,14 @@ def testStringToFields(self):
self.assertEqual(fields[1].name(), "height")
self.assertEqual(fields[1].type(), QVariant.Double)

# geojson string with 2 features
fields = QgsJsonUtils.stringToFields('{ "type": "FeatureCollection","features":[{\n"type": "Feature","geometry": {"type": "Point","coordinates": [125, 10]},"properties": {"name": "Dinagat Islands","height":5.5}}, {\n"type": "Feature","geometry": {"type": "Point","coordinates": [110, 20]},"properties": {"name": "Henry Gale Island","height":6.5}}]}', codec)
self.assertEqual(fields.count(), 2)
self.assertEqual(fields[0].name(), "name")
self.assertEqual(fields[0].type(), QVariant.String)
self.assertEqual(fields[1].name(), "height")
self.assertEqual(fields[1].type(), QVariant.Double)

def testEncodeValue(self):
""" test encoding various values for use in GeoJSON strings """
self.assertEqual(QgsJsonUtils.encodeValue(NULL), 'null')
Expand Down

0 comments on commit c67d258

Please sign in to comment.