QgsMapLayerRegistry.mapLayers() not available in Python Bindings

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

QgsMapLayerRegistry.mapLayers() not available in Python Bindings

Düster Horst
Scalix message content
With Python I want to get all avalilable map layers from my current project. I tried to do this with

layerCollection = QgsMapLayerRegistry().instance().mapLayers()

It doesn't work. I took a look into qgsmaplayerregistry.sip and found out that the method mapLayers() is not wrapped until now. What is to do to wrap this method. Does exists a workareound to retrieve all project layers.

Best regards
Horst

------------------------------------------------

Dr. Horst Düster
GIS-Koordinator, Stv. Amtschef

Kanton Solothurn
Bau- und Justizdepartement
Amt für Geoinformation
SO!GIS Koordination
Rötistrasse 4
CH-4501 Solothurn

Telefon ++41(0)32 627 25 32
Telefax ++41(0)32 627 22 14

mailto:[hidden email]
http://www.agi.so.ch


_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: QgsMapLayerRegistry.mapLayers() not available in Python Bindings

Barry Rowlingson
Düster Horst wrote:
> With Python I want to get all avalilable map layers from my current
> project. I tried to do this with
>
> layerCollection = QgsMapLayerRegistry().instance().mapLayers()
>
> It doesn't work. I took a look into qgsmaplayerregistry.sip and found
> out that the method mapLayers() is not wrapped until now. What is to do
> to wrap this method. Does exists a workareound to retrieve all project
> layers.

You can get all the layers in the current map canvas from the console thus:

# get the map canvas
mc=iface.getMapCanvas()

# how many layers are there?
nLayers=mc.layerCount()

# loopage:

for l in range(nLayers):
   layer = mc.getZpos(l)
   doSomethingWith(layer)

  - is that good enough?

Barry

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: QgsMapLayerRegistry.mapLayers() not available in Python Bindings

Martin Dobias
In reply to this post by Düster Horst
On Jan 11, 2008 11:01 AM, Düster Horst <[hidden email]> wrote:

>
>
> With Python I want to get all avalilable map layers from my current project.
> I tried to do this with
>
> layerCollection = QgsMapLayerRegistry().instance().mapLayers()
>
> It doesn't work. I took a look into qgsmaplayerregistry.sip and found out
> that the method mapLayers() is not wrapped until now. What is to do to wrap
> this method. Does exists a workareound to retrieve all project layers.

Hi,

this function isn't wrapped so far because mapLayers() uses std::map
container which should be replaced with QMap instead. It's not a lot
of work, I'll do it probably soon.
Please note that theoretically QgsMapLayerRegistry can contain also
layers which are not part of the project (e.g. when using
georeferencer plugin it adds temporarily a layer to the registry).

In fact there is currently no way how to directly access all project's
layers correctly, since map canvas doesn't keep list of all layers
(only those which are visible) and it's not possible to access legend
from the plugin as it's not part of QGIS libraries :-/

One workaround would be to save the project and then use legend's
nodes to parse the list of layers, which is, well, quite inconvenient.
We should definitely do something with this. I guess for now wrapping
mapLayers() would do it...

Regards
Martin
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: QgsMapLayerRegistry.mapLayers() not available in Python Bindings

Düster Horst
In reply to this post by Düster Horst
Scalix message content
Martin,

Thank you very much for your response. I think for my purposes mapLayers() would be a good solution. So I'll wait for the wrapped method.

Horst

------------------------------------------------

Dr. Horst Düster
GIS-Koordinator, Stv. Amtschef

Kanton Solothurn
Bau- und Justizdepartement
Amt für Geoinformation
SO!GIS Koordination
Rötistrasse 4
CH-4501 Solothurn

Telefon ++41(0)32 627 25 32
Telefax ++41(0)32 627 22 14

mailto:[hidden email]
http://www.agi.so.ch

-----Ursprüngliche Nachricht-----
Von: Martin Dobias [mailto:[hidden email]]
Gesendet am: Freitag, 11. Januar 2008 11:57
An: Düster Horst
Cc: qgis-developer
Betreff: Re: [Qgis-developer] QgsMapLayerRegistry.mapLayers() not
available in Python Bindings

On Jan 11, 2008 11:01 AM, Düster Horst <[hidden email]> wrote:
>
>
> With Python I want to get all avalilable map layers from my current project.
> I tried to do this with
>
> layerCollection = QgsMapLayerRegistry().instance().mapLayers()
>
> It doesn't work. I took a look into qgsmaplayerregistry.sip and found out
> that the method mapLayers() is not wrapped until now. What is to do to wrap
> this method. Does exists a workareound to retrieve all project layers.

Hi,

this function isn't wrapped so far because mapLayers() uses std::map
container which should be replaced with QMap instead. It's not a lot
of work, I'll do it probably soon.
Please note that theoretically QgsMapLayerRegistry can contain also
layers which are not part of the project (e.g. when using
georeferencer plugin it adds temporarily a layer to the registry).

In fact there is currently no way how to directly access all project's
layers correctly, since map canvas doesn't keep list of all layers
(only those which are visible) and it's not possible to access legend
from the plugin as it's not part of QGIS libraries :-/

One workaround would be to save the project and then use legend's
nodes to parse the list of layers, which is, well, quite inconvenient.
We should definitely do something with this. I guess for now wrapping
mapLayers() would do it...

Regards
Martin

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: QgsMapLayerRegistry.mapLayers() not available in Python Bindings

Martin Dobias
In reply to this post by Barry Rowlingson
On Jan 11, 2008 11:40 AM, Barry Rowlingson <[hidden email]> wrote:

>
> You can get all the layers in the current map canvas from the console thus:
>
> # get the map canvas
> mc=iface.getMapCanvas()
>
> # how many layers are there?
> nLayers=mc.layerCount()
>
> # loopage:
>
> for l in range(nLayers):
>    layer = mc.getZpos(l)
>    doSomethingWith(layer)
>
>   - is that good enough?

Well, it doesn't include hidden layers....

Martin
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: QgsMapLayerRegistry.mapLayers() not available in Python Bindings

Düster Horst
In reply to this post by Barry Rowlingson
Scalix message content
Barry,

Yep that's exactly the solution for my problem. Thank you very much.

Regards
Horst

------------------------------------------------

Dr. Horst Düster
GIS-Koordinator, Stv. Amtschef

Kanton Solothurn
Bau- und Justizdepartement
Amt für Geoinformation
SO!GIS Koordination
Rötistrasse 4
CH-4501 Solothurn

Telefon ++41(0)32 627 25 32
Telefax ++41(0)32 627 22 14

mailto:[hidden email]
http://www.agi.so.ch

-----Ursprüngliche Nachricht-----
Von: Barry Rowlingson [mailto:[hidden email]]
Gesendet am: Freitag, 11. Januar 2008 11:40
An: Düster Horst
Cc: qgis-developer
Betreff: Re: [Qgis-developer] QgsMapLayerRegistry.mapLayers() not
available in Python Bindings

Düster Horst wrote:
> With Python I want to get all avalilable map layers from my current
> project. I tried to do this with
>
> layerCollection = QgsMapLayerRegistry().instance().mapLayers()
>
> It doesn't work. I took a look into qgsmaplayerregistry.sip and found
> out that the method mapLayers() is not wrapped until now. What is to do
> to wrap this method. Does exists a workareound to retrieve all project
> layers.

You can get all the layers in the current map canvas from the console thus:

# get the map canvas
mc=iface.getMapCanvas()

# how many layers are there?
nLayers=mc.layerCount()

# loopage:

for l in range(nLayers):
layer = mc.getZpos(l)
doSomethingWith(layer)

- is that good enough?

Barry

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: QgsMapLayerRegistry.mapLayers() not available in Python Bindings

Martin Dobias
In reply to this post by Düster Horst
On Jan 11, 2008 12:01 PM, Düster Horst <[hidden email]> wrote:
>
>
> Martin,
>
> Thank you very much for your response. I think for my purposes mapLayers()
> would be a good solution. So I'll wait for the wrapped method.

I guess you're already using another solution, but starting from r7984
you can use mapLayers() also from Python.

Martin
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer