TransWikia.com

Layer fields refactoring does not work for CSV export file

Geographic Information Systems Asked on April 30, 2021

In my graphical model does not working refactoring layer fields. Entry layer has two fields, which first is string and second is integer.

My Refactoring functions change first column to int and second to string.

Problem is in result CSV file, where both columns are string.

The entry layer:

{
   "type":"FeatureCollection",
   "name":"input_layer",
   "crs":{
      "type":"name",
      "properties":{
         "name":"urn:ogc:def:crs:OGC:1.3:CRS84"
      }
   },
   "features":[
      {
         "type":"Feature",
         "properties":{
            "id":1,
            "string_col":"a",
            "int_col":1
         },
         "geometry":{
            "type":"Point",
            "coordinates":[
               17.188928015181641,
               50.339572602501271
            ]
         }
      },
      {
         "type":"Feature",
         "properties":{
            "id":2,
            "string_col":"b",
            "int_col":2
         },
         "geometry":{
            "type":"Point",
            "coordinates":[
               17.141655627366305,
               50.278921237002351
            ]
         }
      },
      {
         "type":"Feature",
         "properties":{
            "id":3,
            "string_col":"c",
            "int_col":3
         },
         "geometry":{
            "type":"Point",
            "coordinates":[
               17.20468547778675,
               50.234027334108539
            ]
         }
      }
   ]
}

Graphical script:

from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFeatureSink
import processing


class Model(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterVectorLayer('entrylayer', 'entry layer', types=[QgsProcessing.TypeVectorPoint], defaultValue=None))
        self.addParameter(QgsProcessingParameterFeatureSink('Result', 'result', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))

    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(1, model_feedback)
        results = {}
        outputs = {}

        # Refactor fields
        alg_params = {
            'FIELDS_MAPPING': [{'expression': '"string_col"', 'length': 0, 'name': 'string_col', 'precision': 0, 'type': 2}, {'expression': '"int_col"', 'length': 0, 'name': 'int_col', 'precision': 0, 'type': 10}],
            'INPUT': parameters['entrylayer'],
            'OUTPUT': parameters['Result']
        }
        outputs['RefactorFields'] = processing.run('qgis:refactorfields', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['Result'] = outputs['RefactorFields']['OUTPUT']
        return results

    def name(self):
        return 'model'

    def displayName(self):
        return 'model'

    def group(self):
        return ''

    def groupId(self):
        return ''

    def createInstance(self):
        return Model()

Result CSV layer contains both column marked as string:

string_col,int_col
"0","1"
"0","2"
"0","3"

Is it possible refactor fields and save to CSV file in correct data types?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP