Anda di halaman 1dari 4

Source :

http://geotux.tuxfamily.org/index.php/en/component/k2/item/270-tabla-de-contenido-leyenda-paraaplicaciones-basadas-en-pyqgis/

QGIS provides several utilities for Python developers. However, if you


want to build your own PyQGIS application, you have to create some
widgets on your own. The layer list widget is one of them and it certainly
is a must for GIS applications. Here is mine.
I have created a widget for PyQGIS applications that is a (really) simplified version of the QGIS layer list widget. The
main difference is that it has no group layers. The widget is also based on OpenOceanMap's legend written by Aaron
Racicot but instead of working with QCheckBoxcontrols it uses QTreeWidgetItem for representing every map layer.

This is a screenshot of what you may get:

What does the widget allow?

Get an ordered list of map layers.


Move up or down a layer affecting its z-order in the map.
Make a layer visible or invisible in the map.
Visualize/change the layer symbology (actually, you have to develop your own layer symbology dialog, I just
give you a foo example).
Change some layer properties.
Load/Save QGIS layer style files.
Remove layers.
Integrate your map with other tools acting over loaded layers (e.g. Show/Hide all, Remove all).

What about the license?


It is GNU GPL v.2.

How to include it?


To include the widget on your PyQGIS application you should follow these steps:
1. Download this ZIP file and extract it to your application folder. It contains the legend.py file, which is actually the
widget, a number of images (icons) to represent layer types and menu options, and finally, a dialog that is used to set
layer properties.
You can get the icons from the QGIS project, I have not created them.
2. Import the Legend class.
In the main class of your application, which inherits from QMainWindow, import the Legend class from the legend.py
file, this way:

from legend import Legend


3. Add the CreateLegendWidget method.
Include the CreateLegendWidget method in the main class of the application to add the dock widget into the GUI and
associate the layer list widget to the canvas. The method is this:

def createLegendWidget( self ):


""" Create the map legend widget and associate to the canvas """
self.legend = Legend( self )
self.legend.setCanvas( self.canvas )
self.legend.setObjectName( "theMapLegend" )
self.LegendDock = QDockWidget( "Layers", self )
self.LegendDock.setObjectName( "legend" )
self.LegendDock.setAllowedAreas( Qt.LeftDockWidgetArea |
Qt.RightDockWidgetArea )
self.LegendDock.setWidget( self.legend )
self.LegendDock.setContentsMargins ( 9, 9, 9, 9 )
self.addDockWidget( Qt.LeftDockWidgetArea, self.LegendDock )
In the __init__ method of the application main class call the just added method:

self.createLegendWidget()

# Create the legend widget

4. Compile/Import a resource file to include legend widget icons.


The layer list widget include some icons you would like to see. To accomplish this, compile the provided
resources.qrc file with the following command:

$ pyrcc4 resources.qrc -o resources_rc.py


The legend.py file already contains a reference to the genereated file. Nevertheless, if you have your own resources
file, you may add all the images in the imgs folder into a new prefix, for instance, "legend". If you want to choose
another prefix, make sure to change the resource_prefix variable in the legend.py file as well.

resources_prefix = ":/legend/imgs/"

5. Let the legend to load and remove layers in order to avoid segmentation fault.
You must have a method to load layers into your map application, but now that you want to include a layer list widget
you should let it control all these matters. The layer list widget has a SLOT to manage the layer set at the time you
load, move or remove a layer. All you have to do is to call the appropriate methods (either addMapLayer or
removeMapLayer) from QgsMapLayerRegistry instance, but do not use the setLayerSet canvas method yourself, the
new widget will. So, comment or delete your setLayerSet calls.

QgsMapLayerRegistry.instance().addMapLayer( layer );
#self.canvas.setLayerSet( self.layers )
6. Load the properties dialog.
To allow the user set some layer properties you have to compile the dlgLayerProperties.ui file, which will provide a
basic dialog.

$ pyuic4 dlgLayerProperties.ui -o dlgLayerProperties_ui.py

7. (Optional) Enable/Disable tools


The layer list widget emits a SIGNAL that allows you to enable/disable tools in your application, according to the
active layer type, either raster or vector. The SIGNAL is called activeLayerChanged and if you want to use it, you are
suppose to write something like this in your application main class:

self.connect( self.legend, SIGNAL( "activeLayerChanged" ),


self.enableTools )
... # Probably more code
def enableTools( self ):
if not self.legend.activeLayer():
# Disable some general tools
else:
# Enable some general tools
layerType = self.legend.activeLayer().layer().type()
if layerType == 0: # Vector Layer

# Enable vector tools and disable others


elif layerType == 1: # Raster Layer
# Enable raster tools and disable others
8. (Optional) Use a few Legend class methods to expose functionality to other tools or plugins:

Iterate over the layers:

for layer in self.legend.layers:


...

Show (or hide) all the layers in the map:

def showAllLayers( self ):


self.legend.setStatusForAllLayers( True )

Get the active layer (for plugins):

return self.legend.activeLayer()

Refresh the layer symbology (for plugins):

self.legend.refreshLayerSymbology( layer )

Remove layers by Id (for plugins):

self.legend.removeLayers( layerIds )
That's it!

Anda mungkin juga menyukai