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()
-
gis.stackexchange.com/questions/137575/…WKT– WKT2016年06月16日 18:18:21 +00:00Commented 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.GreyEyedPallas– GreyEyedPallas2016年06月16日 18:47:01 +00:00Commented Jun 16, 2016 at 18:47
-
Actually I'll try to play around with it a bit and see what happens.GreyEyedPallas– GreyEyedPallas2016年06月16日 18:53:02 +00:00Commented 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.GreyEyedPallas– GreyEyedPallas2016年06月16日 19:58:21 +00:00Commented Jun 16, 2016 at 19:58
1 Answer 1
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)
-
[TypeError: index 0 has type 'str' but 'QgsMapLayer' is expected] getting this error for "canvas.setLayerSet(canvas_layers)" this line. kindly suggest!!!Satya Dalei– Satya Dalei2022年10月14日 05:15:11 +00:00Commented Oct 14, 2022 at 5:15
Explore related questions
See similar questions with these tags.