这一节中所有的代码,如果不在QGIS命令行中执行,都需要导入如下包库。
1from qgis.core import (
2 QgsProject,
3 QgsPathResolver
4)
5
6from qgis.gui import (
7 QgsLayerTreeMapCanvasBridge,
8)
2. 加载工程
有的时候你在开发一个插件或者开发一个独立的QGIS Python应用程序(see: Python Applications).(通常这种更多),你需要加载一个已有工程。
To load a project into the current QGIS application you need to create
an instance of the QgsProject
class.
This is a singleton class, so you must use its instance()
method to do it.
You can call its read()
method, passing the path of the project to be loaded:
1# If you are not inside a QGIS console you first need to import
2# qgis and PyQt classes you will use in this script as shown below:
3from qgis.core import QgsProject
4# Get the project instance
5project = QgsProject.instance()
6# Print the current project file name (might be empty in case no projects have been loaded)
7# print(project.fileName())
8
9# Load another project
10project.read('testdata/01_project.qgs')
11print(project.fileName())
testdata/01_project.qgs
If you need to make modifications to the project (for example to add or remove some layers)
and save your changes, call the write()
method of your project instance.
The write()
method also accepts an optional
path for saving the project to a new location:
# Save the project to the same
project.write()
# ... or to a new file
project.write('testdata/my_new_qgis_project.qgs')
Both read()
and
write()
functions
return a boolean value that you can use to check if the operation was successful.
备注
If you are writing a QGIS standalone application, in order to synchronise the loaded project with
the canvas you need to instantiate a QgsLayerTreeMapCanvasBridge
as in the example below:
bridge = QgsLayerTreeMapCanvasBridge( \
QgsProject.instance().layerTreeRoot(), canvas)
# Now you can safely load your project and see it in the canvas
project.read('testdata/my_new_qgis_project.qgs')
2.1. Resolving bad paths
It can happen that layers loaded in the project are moved to another location. When the project is loaded again all the layer paths are broken.
The QgsPathResolver
class with the
setPathPreprocessor()
allows setting a custom path pre-processor function, which allows for
manipulation of paths and data sources prior to resolving them to file references
or layer sources.
The processor function must accept a single string argument (representing the original file path or data source) and return a processed version of this path.
The path pre-processor function is called before any bad layer handler.
Some use cases:
replace an outdated path:
def my_processor(path): return path.replace('c:/Users/ClintBarton/Documents/Projects', 'x:/Projects/') QgsPathResolver.setPathPreprocessor(my_processor)
replace a database host address with a new one:
def my_processor(path): return path.replace('host=10.1.1.115', 'host=10.1.1.116') QgsPathResolver.setPathPreprocessor(my_processor)
replace stored database credentials with new ones:
1def my_processor(path): 2 path= path.replace("user='gis_team'", "user='team_awesome'") 3 path = path.replace("password='cats'", "password='g7as!m*'") 4 return path 5 6QgsPathResolver.setPathPreprocessor(my_processor)