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()