Skip to content
Permalink
Browse files

Add method for exporting QgsFeatureList to GeoJSON featurecollection

  • Loading branch information
nyalldawson committed May 7, 2016
1 parent 5c1a05c commit 3681e2ceebe9f82268d658c39606ed53e89423d5
Showing with 92 additions and 0 deletions.
  1. +9 −0 python/core/qgsjsonutils.sip
  2. +11 −0 src/core/qgsjsonutils.cpp
  3. +9 −0 src/core/qgsjsonutils.h
  4. +63 −0 tests/src/python/test_qgsjsonutils.py
@@ -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;

};


@@ -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" ) );
}



//
@@ -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
@@ -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.
You can’t perform that action at this time.