Skip to main content

Activating layer by its name in PyQGIS?

I'm writing a script that requires the attributes of different layers to work, and I would like to know how to call a layer that is not the currently active one and get its features. For

For example, I have three layers, named "Poles", "Lines" and "Loads", and I need to get the all the features of the layers with layer.getFeatures()layer.getFeatures() and save those features in a list for each layer. For only one layer we usually do this:

If my active layer is "Poles", I write:

    layer = iface.activeLayer()
    poles = layer.getFeatures()

And then I can do anything with "poles", but how can I do this with the other layers that are not active? If I wanted to do the same with the layer "Lines", I would have to activate it "manually" by clicking on it, and repeating that code.

I have tried with this method: Getting layer by name in PyQGIS

And this one: Getting a layer that is not active with PyQGIS

But I always get this error message, because I'm not activating the layer, only creating a list of layer names.

Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'getFeatures'

How can I activate a layer by its name to get the features in it?

Activating layer by its name in PyQGIS?

I'm writing a script that requires the attributes of different layers to work, and I would like to know how to call a layer that is not the currently active one and get its features. For example, I have three layers, named "Poles", "Lines" and "Loads", and I need to get the all the features of the layers with layer.getFeatures() and save those features in a list for each layer. For only one layer we usually do this:

If my active layer is "Poles", I write:

    layer = iface.activeLayer()
    poles = layer.getFeatures()

And then I can do anything with "poles", but how can I do this with the other layers that are not active? If I wanted to do the same with the layer "Lines", I would have to activate it "manually" by clicking on it, and repeating that code.

I have tried with this method: Getting layer by name in PyQGIS

And this one: Getting a layer that is not active with PyQGIS

But I always get this error message, because I'm not activating the layer, only creating a list of layer names.

Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'getFeatures'

How can I activate a layer by its name to get the features in it?

Activating layer by its name in PyQGIS

I'm writing a script that requires the attributes of different layers to work, and I would like to know how to call a layer that is not the currently active one and get its features.

For example, I have three layers, named "Poles", "Lines" and "Loads", and I need to get the all the features of the layers with layer.getFeatures() and save those features in a list for each layer. For only one layer we usually do this:

If my active layer is "Poles", I write:

layer = iface.activeLayer()
poles = layer.getFeatures()

And then I can do anything with "poles", but how can I do this with the other layers that are not active? If I wanted to do the same with the layer "Lines", I would have to activate it "manually" by clicking on it, and repeating that code.

I have tried with this method: Getting layer by name in PyQGIS

And this one: Getting a layer that is not active with PyQGIS

But I always get this error message, because I'm not activating the layer, only creating a list of layer names.

Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'getFeatures'

How can I activate a layer by its name to get the features in it?

replaced http://gis.stackexchange.com/ with https://gis.stackexchange.com/
Source Link

I'm writing a script that requires the attributes of different layers to work, and I would like to know how to call a layer that is not the currently active one and get its features. For example, I have three layers, named "Poles", "Lines" and "Loads", and I need to get the all the features of the layers with layer.getFeatures() and save those features in a list for each layer. For only one layer we usually do this:

If my active layer is "Poles", I write:

    layer = iface.activeLayer()
    poles = layer.getFeatures()

And then I can do anything with "poles", but how can I do this with the other layers that are not active? If I wanted to do the same with the layer "Lines", I would have to activate it "manually" by clicking on it, and repeating that code.

I have tried with this method: Getting layer by name in PyQGISGetting layer by name in PyQGIS

And this one: Getting a layer that is not active with PyQGISGetting a layer that is not active with PyQGIS

But I always get this error message, because I'm not activating the layer, only creating a list of layer names.

Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'getFeatures'

How can I activate a layer by its name to get the features in it?

I'm writing a script that requires the attributes of different layers to work, and I would like to know how to call a layer that is not the currently active one and get its features. For example, I have three layers, named "Poles", "Lines" and "Loads", and I need to get the all the features of the layers with layer.getFeatures() and save those features in a list for each layer. For only one layer we usually do this:

If my active layer is "Poles", I write:

    layer = iface.activeLayer()
    poles = layer.getFeatures()

And then I can do anything with "poles", but how can I do this with the other layers that are not active? If I wanted to do the same with the layer "Lines", I would have to activate it "manually" by clicking on it, and repeating that code.

I have tried with this method: Getting layer by name in PyQGIS

And this one: Getting a layer that is not active with PyQGIS

But I always get this error message, because I'm not activating the layer, only creating a list of layer names.

Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'getFeatures'

How can I activate a layer by its name to get the features in it?

I'm writing a script that requires the attributes of different layers to work, and I would like to know how to call a layer that is not the currently active one and get its features. For example, I have three layers, named "Poles", "Lines" and "Loads", and I need to get the all the features of the layers with layer.getFeatures() and save those features in a list for each layer. For only one layer we usually do this:

If my active layer is "Poles", I write:

    layer = iface.activeLayer()
    poles = layer.getFeatures()

And then I can do anything with "poles", but how can I do this with the other layers that are not active? If I wanted to do the same with the layer "Lines", I would have to activate it "manually" by clicking on it, and repeating that code.

I have tried with this method: Getting layer by name in PyQGIS

And this one: Getting a layer that is not active with PyQGIS

But I always get this error message, because I'm not activating the layer, only creating a list of layer names.

Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'getFeatures'

How can I activate a layer by its name to get the features in it?

deleted 23 characters in body; edited title
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

How to activate a Activating layer by its name in PyQGIS?

I'm writing a script that requires the attributes of different layers to work, and I would like to know how to call a layer that is not the currently active one and get its features. For example, I have three layers, named "Poles", "Lines" and "Loads", and I need to get the all the features of the layers with layer.getFeatures() and save those features in a list for each layer. For only one layer we usually do this:

If my active layer is "Poles", I write:

    layer = iface.activeLayer()
    poles = layer.getFeatures()

And then I can do anything with "poles", but how can I do this with the other layers that are not active? If I wanted to do the same with the layer "Lines", I would have to activate it "manually" by clicking on it, and repeating that code.

I have tried with this method: Getting layer by name in PyQGIS

And this one: Getting a layer that is not active with PyQGIS

But I always get this error message, because I'm not activating the layer, only creating a list of layer names.

Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'getFeatures'

The question might be: How can I activate a layer by its name to get the features in it?

How to activate a layer by its name in PyQGIS?

I'm writing a script that requires the attributes of different layers to work, and I would like to know how to call a layer that is not the currently active one and get its features. For example, I have three layers, named "Poles", "Lines" and "Loads", and I need to get the all the features of the layers with layer.getFeatures() and save those features in a list for each layer. For only one layer we usually do this:

If my active layer is "Poles", I write:

    layer = iface.activeLayer()
    poles = layer.getFeatures()

And then I can do anything with "poles", but how can I do this with the other layers that are not active? If I wanted to do the same with the layer "Lines", I would have to activate it "manually" by clicking on it, and repeating that code.

I have tried with this method: Getting layer by name in PyQGIS

And this one: Getting a layer that is not active with PyQGIS

But I always get this error message, because I'm not activating the layer, only creating a list of layer names.

Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'getFeatures'

The question might be: How can I activate a layer by its name to get the features in it?

Activating layer by its name in PyQGIS?

I'm writing a script that requires the attributes of different layers to work, and I would like to know how to call a layer that is not the currently active one and get its features. For example, I have three layers, named "Poles", "Lines" and "Loads", and I need to get the all the features of the layers with layer.getFeatures() and save those features in a list for each layer. For only one layer we usually do this:

If my active layer is "Poles", I write:

    layer = iface.activeLayer()
    poles = layer.getFeatures()

And then I can do anything with "poles", but how can I do this with the other layers that are not active? If I wanted to do the same with the layer "Lines", I would have to activate it "manually" by clicking on it, and repeating that code.

I have tried with this method: Getting layer by name in PyQGIS

And this one: Getting a layer that is not active with PyQGIS

But I always get this error message, because I'm not activating the layer, only creating a list of layer names.

Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'getFeatures'

How can I activate a layer by its name to get the features in it?

Tweeted twitter.com/StackGIS/status/651477824730202113
deleted 42 characters in body
Source Link
PabloQG
  • 309
  • 1
  • 3
  • 10
Loading
Source Link
PabloQG
  • 309
  • 1
  • 3
  • 10
Loading