Friday, December 22, 2017

Enable Legend window on QGIS Map Canvas

Hello there,
After styling your map analysis, you will want to add a legend or key to better communicate what is on the map to the map user/reader.

In QGIS, this is usually done through the "Print Composer". While this is excellent, the "Print Composer" dialog isn't an interactive mapping evironment. It is meant for final preparation of a map for printing or sharing in form of image/SVG/PDF.

In this post, we want an interactive legend layer separate from the "Layers Panel" on the map canvas so we can control map interactively to reveal some patterns with interfering with the main project "Layers Panel".

I will show you how to eanble the Legend dialog box using the python console with few lines of PyQGIS code.

Let get started...

Legend on QGIS Map Canvas
Start the "python console" enter the following codes...

from qgis.gui import *
root = QgsProject.instance().layerTreeRoot()
model = QgsLayerTreeModel(root)
view = QgsLayerTreeView()
view.setModel(model)
view.show()



This will start a GUI map Legend containing all the layer in the "Layers Panel". This especially good for interacting with the map canvas without obstructions from other setting on the "Layers Panel" (most especially if the map has large number of layers).

You can save the code as a .py file and run it anytime you want to start the Legend GUI window.

That is it!
Thanks for reading...



No comments:

Post a Comment