How to load a QGIS project in python
Today in a project we are working on we wanted to load a QGIS project. It takes surprisingly few lines of code to make a small standalone application that loads a project and then shows it as a map in a window like this:
Here is the code I wrote to produce this:
The main bit of magic is the QgsLayerTreeMapCanvasBridge class which will convert your project into a layer tree so that the layers appear in the canvas. If you ever need to make a standalone python application with a nice map in it, consider using the QGIS API to do it!
Share on Twitter Share on Facebook
Comments
surendrankn 9 years, 10 months ago
I got an error when it execute the following line
Link | Replycanvas = QgsMapCanvas(None) # will reparent it to widget via layout
"QGraphicsScene::addItem: item has already been added to this scene"
I could not see any solution for this. It displays a blank window
different way to build qgis python standalone appl 9 years, 5 months ago
[…] For example here and here. […]
Link | ReplyHow to write a QGIS-plugin to be used in IntraMaps 9 years, 4 months ago
[…] layer is still visible in the map (canvas). I have tried the “bridge” found here and here and different variations of iface.mapCanvas() without any luck so […]
Link | ReplyThomasG77 9 years, 2 months ago
Thanks for this snippet. When I add a WMS in the project layers, your sample is not working anymore. I've already posted samples on the qgis-users mailing list to illustrate the issue http://osdir.com/ml/qgis-user-gis/2015-10/msg00096.html . Any clue is welcome.
Link | ReplyNew Comment