6

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()
  • 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. – GreyEyedPallas Jun 16 '16 at 18:47
  • Actually I'll try to play around with it a bit and see what happens. – GreyEyedPallas Jun 16 '16 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. – GreyEyedPallas Jun 16 '16 at 19:58
5

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.