7

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()
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 16, 2016 at 17:58
4
  • gis.stackexchange.com/questions/137575/… Commented Jun 16, 2016 at 18:18
  • Unfortunately, I've tried the method posted. For some reason it just results a segmentation fault 'core' dumped error and no map appears. It only happens when I add that bit of code. Commented Jun 16, 2016 at 18:47
  • Actually I'll try to play around with it a bit and see what happens. Commented Jun 16, 2016 at 18:53
  • So I've definitely played with the code from the post for about an hour and nothing really happens. With the adjustments, my canvas map doesn't pop up anymore. All I see is that when I execute my code it just says the code exited with code 1. Commented Jun 16, 2016 at 19:58

1 Answer 1

7

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)
answered Jun 17, 2016 at 14:24
1
  • [TypeError: index 0 has type 'str' but 'QgsMapLayer' is expected] getting this error for "canvas.setLayerSet(canvas_layers)" this line. kindly suggest!!! Commented Oct 14, 2022 at 5:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.