0

While coding to obtain individual layers' attributes (field) list, I am stymied by the code.

I can identify a specific layer:
MyLayer = QgsMapLayerRegistry.instance().mapLayersByName("TestPoint")
which returns on "print MyLayer":
qgis._core.QgsVectorLayer object at 0x000000000FCC6730
from which I infer I am correctly using MyLayer as a Layer object/pointer (or whatnot...)

However, when I run this:
fields = Mylayer.pendingAllAttributesList()
I force this error:
AttributeError: 'list' object has no attribute 'pendingAllAttributesList'

Following this statement, eventually I would run a "For field in fields...:" indent block, to iterate through the fields, but I can't even get past my error.

I get same/similar errors for other attribute functions within the API, using versions provided in code samples, and best I can tell I am duplicating relevant python code within the samples found. Somehow, I am missing something important about the way the Attributes work, or the coding, or the list or object.

I am using the recently downloaded QGIS 2.6.0 package for windows.

2 Answers 2

2

mapLayersByName returns a list-of-QgsMapLayers so you need to use [0] at the end to just get the single one.

The defintion for that function is:

QList<QgsMapLayer*> QgsMapLayerRegistry::mapLayersByName(QString layerName)

Which you can see returns a QList of QgsMapLayer, which is just a list of QgsMapLayers in Pythion.

Use QgsMapLayerRegistry.instance().mapLayersByName("layer")[0] to get a single layer.

Use something like this to hide the logic:

def layers_by_name(name, single=False):
    layers = QgsMapLayerRegistry.instance().mapLayersByName("layer")
    if single:
       return layers[0]
    else:
       return layers

layer = layers_by_name("mylayer", single=True)
6
  • thanks, @Nathan. The API says that -that- function returns a pointer to a layer, not returns a list. Is this a Python thing I missed? Is there alternate code to get the attribute/field columns, or is my stepping the correct path? MapLayerByName returns a one-element list, of one pointer? (How would I have known -that-! ??) Sometimes I see "instance()" in samples, and not quite sure why that sneaks in, either.
    – John
    Nov 17, 2014 at 22:15
  • Ah, @Nathan. So this function returns a list by definition -- the clue is the "QList<QgsMapLayer*>. I needed that translation for the fronting portion of that definition line. Thanks.
    – John
    Nov 17, 2014 at 22:51
  • But....@Nathan...if the mapLayersByName(Qstring) returns a list with a single item (a single pointer) in that QList, to the layer whose name is "Qstring", why does the python statement contain (or need to contain) the "instance()" -- what is "instance()" doing for the statement?
    – John
    Nov 17, 2014 at 22:55
  • 1
    QgsMapLayerRegistry is a singleton (en.wikipedia.org/wiki/Singleton_pattern) i.e there is only one map layer registry, ever, so you access it though instance(). That has nothing to do with what you are getting out of it as a result. instance() returns the instance of QgsMapLayerRegistry. It's a gross pattern but we are stuck with that for now.
    – Nathan W
    Nov 17, 2014 at 22:58
  • Another "Ahah", @Nathan. Other than hearing that, from you, where would I have learned that in the API qgis.org/api/classQgsMapLayerRegistry.html. I appreciate your yielding clues so I can answer my own questions, later.
    – John
    Nov 17, 2014 at 23:01
0

This, for future readers. I noted this clue that would be helpful

layerSS = QgsMapLayerRegistry.instance().mapLayersByName("TestPoint") print layerSS
returns:
[< qgis._core.QgsVectorLayer object at 0x000000000FCC6730 >]

while:
layerSS = QgsMapLayerRegistry.instance().mapLayersByName("TestPoint")[0] print layerSS
returns: < qgis._core.QgsVectorLayer object at 0x000000000FCC6730>

Note the square brackets ("[ ... ]", in the first case, indicating that the result was a List with one pointer pointer as an element (a pointer with the "< ... >"), and that my error spoke of lists.

Note the absence of the brackets, in the second case, indicating that the result was a pointer

With that portion of my error identified, my next question should/would have been, how do I get the first/only element of a list. The answer was @Nathans first clue: using the [0].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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