Monday 26 January 2015

pyqgis - How to load multiple vector layers into QGIS' map canvas using python scripting?


I am trying to write a standalone script in python to create a QGIS map canvas. The vector layers I am attempting to load are Shapefiles.


Currently, I am able to load each vector layer, add them to the map registry and show them in my map canvas application. The issue I am having is that I need all these vector layers to be shown in the map canvas at the same time. Currently the qgis map canvas just shows the last vector layer that was set to "canvas.setExtent(vector_layer.extent())". I've tried creating a list of all my layers and then putting that into canvas.setExtent(list_of_layers.extent()) but lists don't have the method "extent.()".


I've read the pyQGIS cookbook and I understand that the .extent() method is for a certain vector you would like to see... How can I show all the vector layers at one time (without using Python Console)? I've delved into other Q&As that had similar questions like this one; however, I'm not really finding any solutions that actually address my problem.


Here is my following code:


import modules
from qgis.core import *
from qgis.gui import *

from PyQt4.QtCore import *
from PyQt4 import QtGui
import os, sys
import qgis

def main():

# supply path to qgis isntall location
QgsApplication.setPrefixPath("/usr", True)


# create a reference to the QgsApplication, setting the
# second arguement to False disables the GUI
app = QgsApplication([], True)

# load providers
app.initQgis()


# create Qt widget
canvas = QgsMapCanvas()

canvas.setCanvasColor(Qt.white)

# enable this for smooth rendering
canvas.enableAntiAliasing(True)


# not updated US6SP10M files from ENC_ROOT
source_dir = "/home/cassandra/desktop/file_formats/Shapefiles"

# load vector layers

for files in os.listdir(source_dir):

# load only the shapefiles
if files.endswith(".shp"):
vlayer = QgsVectorLayer(source_dir + "/" + files, files, "ogr")

# add layer to the registry
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

# set extent to the extent of our layer

canvas.setExtent(vlayer.extent())

# set the map canvas layer set
canvas.setLayerSet([QgsMapCanvasLayer(vlayer)])

# refresh canvas and show it
canvas.refresh()
canvas.show()
app.exec_()
app.exitQgis()


main()

Answer



I figured it out! It was something very small. Basically, the layers you would like to see displayed on the canvas is provided through canvas.setLayerSet(list_of_layers).


# total list of layers actually displayed on map canvas
canvas_layers = []

# load vector layers
for files in os.listdir(source_dir):


# load only the shapefiles
if files.endswith(".shp"):

# create vector layer object
vlayer = QgsVectorLayer(source_dir + "/" + files, files, "ogr")
print(files)

# add the layer to the registry
QgsMapLayerRegistry.instance().addMapLayer(vlayer)


# combine extent of the current vector layer with the extent of the created "extent" rectangle object
extent.combineExtentWith(vlayer.extent())
canvas_layers.append(QgsMapCanvasLayer(vlayer))


# set extent to the extent of a larger rectangle so we can see all geometries
canvas.setExtent(extent)

# provide set of layers for display on the map canvas
canvas.setLayerSet(canvas_layers)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...