TransWikia.com

Changing name layer from geoprocessing output

Geographic Information Systems Asked by BERA on December 31, 2020

How can I change the name for geoprocessing output map layers, for example:

out_diss = 'ogr:dbname='C:/GIS/data/testdata/output.gpkg' table="ak_riks_dissolved" (geom)'

dissolved = processing.run("native:dissolve", 
    {'INPUT':'C:/GIS/data/testdata/ak_riks.shp','FIELD':[],
    'OUTPUT':out_diss})['OUTPUT']

dissolved.setTitle('dissolved') #No difference
QgsProject.instance().addMapLayer(dissolved)

Instead of the default very long name:

enter image description here

3 Answers

Try to use QgsVectorLayer method to change the layer name before adding it to the layer tree:

out_diss = 'ogr:dbname='C:/GIS/data/testdata/output.gpkg' table="ak_riks_dissolved" (geom)'

dissolved = processing.run("native:dissolve", 
    {'INPUT':'C:/GIS/data/testdata/ak_riks.shp','FIELD':[],
    'OUTPUT':out_diss})['OUTPUT']

vLayer = QgsVectorLayer(out_diss, "Dissolve", "ogr")
QgsProject.instance().addMapLayer(vLayer)

Correct answer by ahmadhanb on December 31, 2020

The following code works for me. I use the processing.runAndLoadResults method to automatically add the layer to the project. The name of my output layer is well "dissolved".

int_diss = 'C:/Users/v.bre/Desktop/test_dissolve.shp'
out_diss = 'ogr:dbname='C:/Users/v.bre/Desktop/zzz/name/dissolve.gpkg' table="dissolved" (geom)'

dissolved = processing.runAndLoadResults("native:dissolve", {'INPUT':int_diss,'FIELD':[],'OUTPUT':out_diss})

You can also try to use the setName method to rename a layer instead of setTitle.

Answered by Vincent Bré on December 31, 2020

An approach that I mentioned in comments, using setName() method:

out_diss = 'ogr:dbname='C:/Temp/output.gpkg' table="ak_riks_dissolved" (geom)'

layer_input = QgsProject.instance().mapLayersByName("LayerB")[0]

dissolved = processing.run("native:dissolve", 
    {'INPUT':layer_input,
    'FIELD':[],
    'OUTPUT':out_diss})['OUTPUT']
    
dissolved.setName('Renamed!')
QgsProject.instance().addMapLayer(dissolved)

References:

Answered by Taras on December 31, 2020

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