1

I have seen this type of question posed a few times on this site. The solutions that I have gleamed from these questions has helped me. But I have not been able to get my script to produce a result that I would like.

I have a layer in my QGIS 3.10 map called "warn", the actual file path and name is "C:\temp\warn1.shp". I am writing a Toolbox to perform a series of processes on this "warn"-file, with the intention that this file will be called something different in the future.

Some Examples that I have used:

I set up the parameters for the Toolbox as described in the QGIS Template:

self.addParameter(
            QgsProcessingParameterFeatureSource(
                'INPUT',
                self.tr('Area'),
                [QgsProcessing.TypeVectorPolygon]
            )
        )
    

I call this input in the main script:

Polygon = parameters['INPUT']

When I try and confirm the name of the input using a print statement, a value is returned, but the name contains what appears to be an identifier:

print (Polygon)

Returns:

warn1_940166d7_22bc_47bf_b76c_d75462802047

When I use the Polygon variable in the definition of the layer:

layer = QgsProject.instance().mapLayersByName(Polygon)[0]

I get an "index out of range" error. Which is expected, as the name does not exist in the map.

How can I return the name of the layer, as it appears in Layer View (Table of Contents)?

In this case I would like "warn" returned.

2
  • 1
    layer_name = Polygon.split("_")[0] ? Sep 28 '20 at 8:02
  • @J.Monticolo thanks for the suggestion. Unfortunately in some of the tests I have done, the name might be two words or one. I know I could force it one way or the other, but that would be my last option. I would still prefer the ability to get the name as it appears in the ToC. Sep 28 '20 at 8:59
3

I've achieved this by a little workaround :

from qgis.utils import iface  # add this in the list of imports
#
#
class My_model(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        # ...

    def processAlgorithm(self, parameters, context, model_feedback):
        # ...
        lyr_list = iface.layerTreeView().layerTreeModel().rootGroup().layerOrder()
        lyr_dict = {lyr.id(): (lyr.name(), lyr) for lyr in lyr_list}
        if parameters['INPUT'] in lyr_dict:
                print(lyr_dict[parameters['INPUT']][0])
                polygon = lyr_dict[parameters['INPUT']][0]
                layer = lyr_dict[parameters['INPUT']][1]
2
  • 1
    I used your code and included: if parameters['INPUT'] in lyr_dict: print(lyr_dict[parameters['INPUT']]) layer = QgsProject.instance().mapLayersByName(lyr_dict[parameters['INPUT']])[0] Sep 29 '20 at 0:33
  • @KeaganAllan: I improved the answer, from the lyr_list, you can access directly to the layers, so I construct a tuple with it. Sep 29 '20 at 7:05
1

@Keagan Allan answered to his own question

Using the accepted answer I added the following to my code:

lyr_list = iface.layerTreeView().layerTreeModel().rootGroup().layerOrder()
lyr_dict = {lyr.id(): lyr.name() for lyr in lyr_list}
if parameters['INPUT'] in lyr_dict:
      print(lyr_dict[parameters['INPUT']])
      polygon = lyr_dict[parameters['INPUT']]
      layer = QgsProject.instance().mapLayersByName(polygon)[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.