Loading Projects
本章节讲述如导入已有工程
有时候你需要加载一个已经存在的工程,为了加载一个已有的QGIS应用,你需要QgsProject的实例instance()并且调用它的read()方法读取对应路径的工程。
# If you are not inside a QGIS console you first need to import # qgis and PyQt4 classes you will use in this script as shown below: from qgis.core import QgsProject from qgis.PyQt.QtCore import QFileInfo # Get the project instance project = QgsProject.instance() # Print the current project file name (might be empty in case no projects have been loaded) print(project.fileName()) u'/home/user/projects/my_qgis_project.qgs' # Load another project project.read(QFileInfo('/home/user/projects/my_other_qgis_project.qgs')) print(project.fileName()) u'/home/user/projects/my_other_qgis_project.qgs'
如果你需要对工程做一些修改,可以通过write()方法来读取工程实例:
# Save the project to the same project.write() # ... or to a new file project.write(QFileInfo('/home/user/projects/my_new_qgis_project.qgs'))
read()和write()都返回一个是否加载成功的布尔值。