Skip to content

Instantly share code, notes, and snippets.

@timlinux
Last active April 3, 2019 14:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timlinux/22935c16da4eda92762f to your computer and use it in GitHub Desktop.
Save timlinux/22935c16da4eda92762f to your computer and use it in GitHub Desktop.
Load and show a QGIS project in python
# coding=utf-8
# A simple demonstration of how to load a QGIS project and then
# show it in a widget.
# This code is public domain, use if for any purpose you see fit.
# Tim Sutton 2015
import os
from qgis.core import QgsProject
from qgis.gui import QgsMapCanvas, QgsLayerTreeMapCanvasBridge
from qgis.core.contextmanagers import qgisapp
from PyQt4.QtCore import QFileInfo
with qgisapp():
project_path = os.path.dirname(__file__) + os.path.sep + 'test.qgs'
canvas = QgsMapCanvas(None) # will reparent it to widget via layout
# Load our project
bridge = QgsLayerTreeMapCanvasBridge(
QgsProject.instance().layerTreeRoot(), canvas)
QgsProject.instance().read(QFileInfo(project_path))
canvas.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment