如何使用PyQGIS将项目加载到QGIS中?

时间:2017-04-23 16:33:47

标签: python qgis

我正在使用PyQGIS Cookbook学习PyQGIS,并购买了PyQGIS Programmers Guide。我已经能够使用小脚本加载图层,但没有运气将项目加载到QGIS中。我一直得到一个' SyntaxError:无效的语法'对于第13行。我在QGIS中使用Python控制台并输入' import _loadproject'启动脚本。

有人可以指出我哪里出错吗?

"""Load Project: This is to test loading a project."""

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis.utils import iface

class ProjectScript:

def __init__(self, iface):
    self.iface = iface

def load_project(self)
    project = QgsProject.instance() \
    project.read(QFileInfo('S:/SOCAL/Section_28/SOCAL_CK47/Control_Data/SOCAL_CK47_project.qgs'))
    print project.fileName 'S:/SOCAL/Section_28/SOCAL_CK47/Control_Data/SOCAL_CK47_project.qgs'

def run_script(iface):
"""Run the script by loading and calling LoadProject methods."""
fs = ProjectScript(iface)
fs.load_project()

1 个答案:

答案 0 :(得分:1)

检查此示例:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis.utils import iface

class ProjectScript:

    def __init__(self, iface):
        self.iface = iface

    def load_project(self):
        project = QgsProject.instance() 
        project.read(QFileInfo('S:\\SOCAL\\Section_28\\SOCAL_CK47\\Control_Data\\SOCAL_CK47_project.qgs'))
        print project.fileName()

    def run_script(iface):
        """Run the script by loading and calling LoadProject methods."""
        fs = ProjectScript(iface)
        fs.load_project()

使用python控制台保存此脚本,例如 ProjectScript.py ,然后执行。下一张图片显示结果。enter image description here