Merge pull request #8150 from elpaso/bugfix-20053-json-exporter-values

Fixes #20053 decimal separator in csv files
This commit is contained in:
Alessandro Pasotti 2018-10-10 09:56:52 +02:00 committed by GitHub
commit b80c95c71a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 2 deletions

View File

@ -70,6 +70,7 @@ QgsCoordinateReferenceSystem QgsJsonExporter::sourceCrs() const
QString QgsJsonExporter::exportFeature( const QgsFeature &feature, const QVariantMap &extraProperties,
const QVariant &id ) const
{
QString s = QStringLiteral( "{\n \"type\":\"Feature\",\n" );
// ID
@ -119,6 +120,12 @@ QString QgsJsonExporter::exportFeature( const QgsFeature &feature, const QVarian
if ( mIncludeAttributes )
{
QgsFields fields = mLayer ? mLayer->fields() : feature.fields();
// List of formatters through we want to pass the values
QStringList formattersWhiteList;
formattersWhiteList << QStringLiteral( "KeyValue" )
<< QStringLiteral( "List" )
<< QStringLiteral( "ValueRelation" )
<< QStringLiteral( "ValueMap" );
for ( int i = 0; i < fields.count(); ++i )
{
@ -133,7 +140,7 @@ QString QgsJsonExporter::exportFeature( const QgsFeature &feature, const QVarian
{
QgsEditorWidgetSetup setup = fields.at( i ).editorWidgetSetup();
QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
if ( fieldFormatter != QgsApplication::fieldFormatterRegistry()->fallbackFieldFormatter() )
if ( formattersWhiteList.contains( fieldFormatter->id() ) )
val = fieldFormatter->representValue( mLayer.data(), i, setup.config(), QVariant(), val );
}

View File

@ -31,7 +31,7 @@ from qgis.core import (QgsJsonUtils,
QgsRelation,
QgsEditorWidgetSetup
)
from qgis.PyQt.QtCore import QVariant, QTextCodec
from qgis.PyQt.QtCore import QVariant, QTextCodec, QLocale
start_app()
codec = QTextCodec.codecForName("System")
@ -676,6 +676,56 @@ class TestQgsJsonUtils(unittest.TestCase):
]}"""
self.assertEqual(exporter.exportFeatures([feature, feature2]), expected)
def testExportFeaturesWithLocale_regression20053(self):
""" Test exporting feature export with range widgets and locale different than C
Regression: https://issues.qgis.org/issues/20053 - decimal separator in csv files
"""
source = QgsVectorLayer("Point?field=name:string&field=cost:double&field=population:int&field=date:date",
"parent", "memory")
self.assertTrue(source.isValid())
fields = source.fields()
feature = QgsFeature(fields, 5)
feature.setGeometry(QgsGeometry(QgsPoint(5, 6)))
feature.setAttributes(['Valsier Peninsula', 6.8, 198000, '2018-09-10'])
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":198000,
"date":"2018-09-10"
}
}
]}"""
self.assertEqual(exporter.exportFeatures([feature]), expected)
setup = QgsEditorWidgetSetup('Range', {
'AllowNull': True,
'Max': 2147483647,
'Min': -2147483648,
'Precision': 4,
'Step': 1,
'Style': 'SpinBox'
}
)
source.setEditorWidgetSetup(1, setup)
source.setEditorWidgetSetup(2, setup)
QLocale.setDefault(QLocale('it'))
exporter.setVectorLayer(source)
self.assertEqual(exporter.exportFeatures([feature]), expected)
if __name__ == "__main__":
unittest.main()