Skip to content

Commit

Permalink
Add method for exporting QgsFeatureList to GeoJSON featurecollection
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 9, 2016
1 parent 5c1a05c commit 3681e2c
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/core/qgsjsonutils.sip
Expand Up @@ -114,11 +114,20 @@ class QgsJSONExporter
* @param id optional ID to use as GeoJSON feature's ID instead of input feature's ID. If omitted, feature's
* ID is used.
* @returns GeoJSON string
* @see exportFeatures()
*/
QString exportFeature( const QgsFeature& feature,
const QVariantMap& extraProperties = QVariantMap(),
const QVariant& id = QVariant() ) const;


/** Returns a GeoJSON string representation of a list of features (feature collection).
* @param features features to convert
* @returns GeoJSON string
* @see exportFeature()
*/
QString exportFeatures( const QgsFeatureList& features ) const;

};


Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsjsonutils.cpp
Expand Up @@ -165,6 +165,17 @@ QString QgsJSONExporter::exportFeature( const QgsFeature& feature, const QVarian
return s;
}

QString QgsJSONExporter::exportFeatures( const QgsFeatureList& features ) const
{
QStringList featureJSON;
Q_FOREACH ( const QgsFeature& feature, features )
{
featureJSON << exportFeature( feature );
}

return QString( "{ \"type\": \"FeatureCollection\",\n \"features\":[\n%1\n]}" ).arg( featureJSON.join( ",\n" ) );
}



//
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsjsonutils.h
Expand Up @@ -134,12 +134,21 @@ class CORE_EXPORT QgsJSONExporter
* @param id optional ID to use as GeoJSON feature's ID instead of input feature's ID. If omitted, feature's
* ID is used.
* @returns GeoJSON string
* @see exportFeatures()
*/
QString exportFeature( const QgsFeature& feature,
const QVariantMap& extraProperties = QVariantMap(),
const QVariant& id = QVariant() ) const;


/** Returns a GeoJSON string representation of a list of features (feature collection).
* @param features features to convert
* @returns GeoJSON string
* @see exportFeature()
*/
QString exportFeatures( const QgsFeatureList& features ) const;


private:

//! Maximum number of decimal places for geometry coordinates
Expand Down
63 changes: 63 additions & 0 deletions tests/src/python/test_qgsjsonutils.py
Expand Up @@ -466,6 +466,69 @@ def testExportFeatureRelations(self):
}"""
self.assertEqual(exporter.exportFeature(pf2), expected)

def testExportFeatures(self):
""" Test exporting feature collections """

fields = QgsFields()
fields.append(QgsField("name", QVariant.String))
fields.append(QgsField("cost", QVariant.Double))
fields.append(QgsField("population", QVariant.Int))

feature = QgsFeature(fields, 5)
feature.setGeometry(QgsGeometry(QgsPointV2(5, 6)))
feature.setAttributes(['Valsier Peninsula', 6.8, 198])

exporter = QgsJSONExporter()

# single feature
expected = """{ "type": "FeatureCollection",
"features":[
{
"type":"Feature",
"id":5,
"geometry":
{"type": "Point", "coordinates": [5, 6]},
"properties":{
"name":"Valsier Peninsula",
"cost":6.8,
"population":198
}
}
]}"""
self.assertEqual(exporter.exportFeatures([feature]), expected)

# multiple features
feature2 = QgsFeature(fields, 6)
feature2.setGeometry(QgsGeometry(QgsPointV2(7, 8)))
feature2.setAttributes(['Henry Gale Island', 9.7, 38])

expected = """{ "type": "FeatureCollection",
"features":[
{
"type":"Feature",
"id":5,
"geometry":
{"type": "Point", "coordinates": [5, 6]},
"properties":{
"name":"Valsier Peninsula",
"cost":6.8,
"population":198
}
},
{
"type":"Feature",
"id":6,
"geometry":
{"type": "Point", "coordinates": [7, 8]},
"properties":{
"name":"Henry Gale Island",
"cost":9.7,
"population":38
}
}
]}"""
self.assertEqual(exporter.exportFeatures([feature, feature2]), expected)


if __name__ == "__main__":
unittest.main()

0 comments on commit 3681e2c

Please sign in to comment.