[GIS] QGIS processing load memory layer with specific name

pyqgisqgis-pluginsqgis-processing

I have a simple QGIS plugin and I use processing algorithms from my models.

Now I want to load my export from processing in my QGIS project that I do easy with runandload but I can't to define name that show me an default name how to load memory layer with my name?

outputs_QGISFIELDCALCULATOR_4=processing.runandload('qgis:fieldcalculator', outputs_QGISFIELDCALCULATOR_3['OUTPUT_LAYER'],'newnew',2,50.0,0.0,True,'left(\"TOP_KOIN\", strpos(\"TOP_KOIN\" ,\' \')-1)',"memeory:layer")

And if I need that memory layer to continue my script for example to do something else how to take ?with memory name? or like this outputs_QGISFIELDCALCULATOR_4?

update

if result:
    selectedLayerIndex = self.dlg.comboBox.currentIndex()
    selectedLayer = layers[selectedLayerIndex]
    print selectedLayer
    #calc='C:/Users/saran/Desktop/qq/calc.shp'
    outputs_QGISFIELDCALCULATOR_1=processing.runalg('qgis:fieldcalculator', selectedLayer,'trid',1,10.0,0.0,True,'$id',None)
    load_it1 = QgsVectorLayer(outputs_QGISFIELDCALCULATOR_1['OUTPUT_LAYER'], 'tid', 'ogr')
    outputs_QGISFIELDCALCULATOR_2=processing.runalg('qgis:fieldcalculator', outputs_QGISFIELDCALCULATOR_1['OUTPUT_LAYER'],'area',0,10.0,3.0,True,'$area',None)
    load_it2 = QgsVectorLayer(outputs_QGISFIELDCALCULATOR_2['OUTPUT_LAYER'], 'area', 'ogr')
    outputs_QGISFIELDCALCULATOR_3=processing.runalg('qgis:fieldcalculator', outputs_QGISFIELDCALCULATOR_2['OUTPUT_LAYER'],'perimetre',0,10.0,3.0,True,'$perimeter',None)
    load_it3 = QgsVectorLayer(outputs_QGISFIELDCALCULATOR_3['OUTPUT_LAYER'], 'perimetre', 'ogr')
    outputs_QGISFIELDCALCULATOR_4=processing.runandload('qgis:fieldcalculator', outputs_QGISFIELDCALCULATOR_3['OUTPUT_LAYER'],'newnew',2,50.0,0.0,True,'left(\"TOP_KOIN\", strpos(\"TOP_KOIN\" ,\' \')-1)',None)

    load_it4 = QgsVectorLayer(outputs_QGISFIELDCALCULATOR_4['OUTPUT_LAYER'], 'new', 'ogr')
    QgsMapLayerRegistry.instance().addMapLayer(load_it1)
    QgsMapLayerRegistry.instance().addMapLayer(load_it2)
    QgsMapLayerRegistry.instance().addMapLayer(load_it3)
    QgsMapLayerRegistry.instance().addMapLayer(load_it4)
    my_output1 = processing.getObject(outputs_QGISFIELDCALCULATOR_1['OUTPUT_LAYER'])
    my_output2 = processing.getObject(outputs_QGISFIELDCALCULATOR_2['OUTPUT_LAYER'])
    my_output3 = processing.getObject(outputs_QGISFIELDCALCULATOR_3['OUTPUT_LAYER'])
    my_output4 = processing.getObject(outputs_QGISFIELDCALCULATOR_4['OUTPUT_LAYER'])

Best Answer

You may continue using the memory layer by loading it as an object. Before doing this, you need to know how the output from the algorithm is called, so typing these lines in the Python Console:

import processing
processing.alghelp('qgis:fieldcalculator')

you will see that the output's name is OUTPUT_LAYER:

ALGORITHM: Field calculator
    INPUT_LAYER <ParameterVector>
    FIELD_NAME <ParameterString>
    FIELD_TYPE <ParameterSelection>
    FIELD_LENGTH <ParameterNumber>
    FIELD_PRECISION <ParameterNumber>
    NEW_FIELD <ParameterBoolean>
    FORMULA <ParameterString>
    OUTPUT_LAYER <OutputVector>


FIELD_TYPE(Field type)
    0 - Float
    1 - Integer
    2 - String
    3 - Date

This means that you will be able to use it if you add a new variable and call it in this way:

# change 'my_output' name as you want
my_output = processing.getObject(outputs_QGISFIELDCALCULATOR_4['OUTPUT_LAYER'])

By now, you are ready to do something with it.

EDIT

From a more general point of view, I would use this code for accomplish both the tasks described in the question:

# see the using of 'runalg' instead of 'runandload' and the using of 'None'
outputs_QGISFIELDCALCULATOR_4=processing.runalg('qgis:fieldcalculator', outputs_QGISFIELDCALCULATOR_3['OUTPUT_LAYER'],'newnew',2,50.0,0.0,True,'left(\"TOP_KOIN\", strpos(\"TOP_KOIN\" ,\' \')-1)',None)

# the following for loading the previous output in the Layers Panel with a custom name
load_it = QgsVectorLayer(outputs_QGISFIELDCALCULATOR_4['OUTPUT_LAYER'], 'type here the name you want', 'ogr')
QgsMapLayerRegistry.instance().addMapLayer(load_it)

# change 'my_output' name as you want
my_output = processing.getObject(outputs_QGISFIELDCALCULATOR_4['OUTPUT_LAYER'])

# by now, 'my_output' will be the object to use for calling the layer returned by 'qgis:fieldcalculator' for further operations.