The code snippets on this page need the following imports if you're outside the pyqgis console:

1
2
3
4
5
6
7
8
from qgis.core import (
    QgsProject,
    QgsPathResolver
)

from qgis.gui import (
    QgsLayerTreeMapCanvasBridge,
)

2. プロジェクトをロードする

時々プラグインから、または(しばしば)スタンドアローンのQGIS Pythonアプリケーションを開発する時に既存のプロジェクトをロードする必要があります(参照: Python アプリケーション )。

プロジェクトを現在のQGISアプリケーションにロードするには、 QgsProject クラスのインスタンスを作成する必要があります。これはシングルトンクラスなので、それを行うには instance() メソッドを使わなければなりません。 read() メソッドを呼び出して、読み込むプロジェクトのパスを渡すことができます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# If you are not inside a QGIS console you first need to import
# qgis and PyQt classes you will use in this script as shown below:
from qgis.core import QgsProject
# 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())

# Load another project
import os
print(os.getcwd())
project.read('testdata/01_project.qgs')
print(project.fileName())
...
testdata/01_project.qgs

プロジェクトに変更(たとえばレイヤーの追加や削除)を加え、その変更を保存する必要がある場合は、プロジェクトインスタンスの write() メソッドを呼び出します。 write() メソッドにパスを指定すれば、プロジェクトを新しい場所に保存することもできます。

# Save the project to the same
project.write()
# ... or to a new file
project.write('testdata/my_new_qgis_project.qgs')

read()write() の両方の関数は操作が成功したかどうかをチェックするために使用できるブール値を返します。

注釈

QGISスタンドアロンアプリケーションを作成している場合は、ロードされたプロジェクトをキャンバスと同期させるために、 QgsLayerTreeMapCanvasBridge を以下の例のようにインスタンス化する必要があります:

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:

  1. replace an outdated path:

    def my_processor(path):
        return path.replace('c:/Users/ClintBarton/Documents/Projects', 'x:/Projects/')
    
    QgsPathResolver.setPathPreprocessor(my_processor)
    
  2. 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)
    
  3. replace stored database credentials with new ones:

    1
    2
    3
    4
    5
    6
    def my_processor(path):
        path= path.replace("user='gis_team'", "user='team_awesome'")
        path = path.replace("password='cats'", "password='g7as!m*'")
        return path
    
    QgsPathResolver.setPathPreprocessor(my_processor)