Add method for exporting QgsFeatureList to GeoJSON featurecollection

This commit is contained in:
Nyall Dawson 2016-05-07 22:19:24 +10:00
parent 5c1a05c8a1
commit 3681e2ceeb
4 changed files with 92 additions and 0 deletions

View File

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

View File

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

View File

@ -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

View File

@ -466,6 +466,69 @@ class TestQgsJSONUtils(unittest.TestCase):
}"""
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()