I found a problem with exporting the vector layer with: "Replace all selected raw field values by displayed values" in the PyQGIS script.
The source layer contains a value map for attribute "value_map"
:
PyQGIS script:
# -*- coding: utf-8 -*-
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
QgsFeatureSink,
QgsVectorFileWriter,
QgsProcessingException,
QgsProcessingAlgorithm,
QgsVectorFileWriter,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)
from qgis import processing
from qgis.utils import iface
class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
INPUT = 'INPUT'
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return ExampleProcessingAlgorithm()
def name(self):
return 'myscript'
def displayName(self):
return self.tr('My Script')
def group(self):
return self.tr('Example scripts')
def groupId(self):
return 'examplescripts'
def shortHelpString(self):
return self.tr("Example algorithm short description")
def initAlgorithm(self, config=None):
# We add the input vector features source. It can have any kind of
# geometry.
self.addParameter(
QgsProcessingParameterFeatureSource(
self.INPUT,
self.tr('Input layer'),
[QgsProcessing.TypeVectorAnyGeometry]
)
)
def processAlgorithm(self, parameters, context, feedback):
layer = iface.activeLayer()
feedback.pushInfo(str(layer))
QgsVectorFileWriter.writeAsVectorFormat(
layer=layer,
fileName='C://Users//001-PC//Desktop//output_layer.json',
fileEncoding="utf-8",
driverName="GEOJSON",
fieldValueConverter=QgsVectorFileWriter.FieldValueConverter()
)
return {}
How can I export layer values with a values map? My script does not work with:
fieldValueConverter = QgsVectorFileWriter.FieldValueConverter()
Exported JSON (script result):
{
"type": "FeatureCollection",
"name": "output_layer",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5514" } },
"features": [
{ "type": "Feature", "properties": { "fid": 1, "text": "aaa", "value_map": 1 }, "geometry": { "type": "Point", "coordinates": [ -553020.462518186424859, -1170227.009075149893761 ] } },
{ "type": "Feature", "properties": { "fid": 2, "text": "sadasd", "value_map": 2 }, "geometry": { "type": "Point", "coordinates": [ -553025.836183290113695, -1170234.612239605281502 ] } },
{ "type": "Feature", "properties": { "fid": 3, "text": "sdasdsad", "value_map": 3 }, "geometry": { "type": "Point", "coordinates": [ -553018.004352234653197, -1170230.038907602196559 ] } }
]
}
Source layer value map setting: