重要
翻訳は あなたが参加できる コミュニティの取り組みです。このページは現在 81.25% 翻訳されています。
17. プロセシングプラグインを書く
開発しようとしているプラグインの種類によっては、プロセシングアルゴリズム(またはそれらのセット)として機能を追加する方が良い場合もあるでしょう。そうすれば、QGIS内でのより良い統合がなされ(これは、モデラーやバッチ処理インタフェースといった、「プロセシング」のコンポーネントの中で実行できるためです)、また開発時間の短縮も期待できます(「プロセシング」が作業の大部分を肩代わりしてくれるからです)。
開発したアルゴリズムを配布するためには、アルゴリズムをプロセシングツールボックスに追加するためのプラグインを新しく作る必要があります。このプラグインにはアルゴリズムプロバイダを含ませるとともに、プラグインの初期化の際にアルゴリズムがツールボックスに登録されるようにする必要があります。
17.1. プラグインをアップデートする
すでに作成済みのプラグインをプロセッシングに追加したい場合は、さらにコードを追加する必要があります。
metadata.txtファイルに以下の変数を追加する必要があります。hasProcessingProvider=yes
initGuiメソッドによってプラグインのセットアップを担うPythonファイルでは、幾つかのコードを直す必要があります。1from qgis.core import QgsApplication 2from .processing_provider.provider import Provider 3 4class YourPluginName: 5 6 def __init__(self): 7 self.provider = None 8 9 def initProcessing(self): 10 self.provider = Provider() 11 QgsApplication.processingRegistry().addProvider(self.provider) 12 13 def initGui(self): 14 self.initProcessing() 15 16 def unload(self): 17 QgsApplication.processingRegistry().removeProvider(self.provider)
processing_providerフォルダを作ってそこに次の3つのファイルを納めることもできます。白紙の
__init__.pyファイル。このファイルは妥当なPythonパッケージを作るために必要です。provider.pyファイルはプロセッシングプロバイダを生成しあなたのアルゴリズムを外部から使えるようにします。1from qgis.core import QgsProcessingProvider 2from qgis.PyQt.QtGui import QIcon 3 4from .example_processing_algorithm import ExampleProcessingAlgorithm 5 6class Provider(QgsProcessingProvider): 7 8 """ The provider of our plugin. """ 9 10 def loadAlgorithms(self): 11 """ Load each algorithm into the current provider. """ 12 self.addAlgorithm(ExampleProcessingAlgorithm()) 13 # add additional algorithms here 14 # self.addAlgorithm(MyOtherAlgorithm()) 15 16 def id(self) -> str: 17 """The ID of your plugin, used for identifying the provider. 18 19 This string should be a unique, short, character only string, 20 eg "qgis" or "gdal". This string should not be localised. 21 """ 22 return 'yourplugin' 23 24 def name(self) -> str: 25 """The human friendly name of your plugin in Processing. 26 27 This string should be as short as possible (e.g. "Lastools", not 28 "Lastools version 1.0.1 64-bit") and localised. 29 """ 30 return self.tr('Your plugin') 31 32 def icon(self) -> QIcon: 33 """Should return a QIcon which is used for your provider inside 34 the Processing toolbox. 35 """ 36 return QgsProcessingProvider.icon(self)
example_processing_algorithm.pyファイルはサンプルアルゴリズムを含みます。 script template file の内容をコピー&ペーストして、自分の必要に合わせて修正してください。
You should have a tree similar to this:
1└── your_plugin_root_folder 2 ├── __init__.py 3 ├── LICENSE 4 ├── metadata.txt 5 └── processing_provider 6 ├── example_processing_algorithm.py 7 ├── __init__.py 8 └── provider.py
ここまできたらQGISでプラグインをリロードすれば、プロセッシングツールボックスとモデラーの中にあなたのスクリプトを見つけることができるはずです。
17.1.1. Creating a custom algorithm
Here's a simple example of a custom buffer algorithm:
1from qgis.core import (
2 QgsProcessingAlgorithm,
3 QgsProcessingParameterFeatureSource,
4 QgsProcessingParameterNumber,
5 QgsProcessingParameterFeatureSink,
6 QgsFeatureSink,
7)
8
9class BufferAlgorithm(QgsProcessingAlgorithm):
10
11 INPUT = 'INPUT'
12 DISTANCE = 'DISTANCE'
13 OUTPUT = 'OUTPUT'
14
15 def initAlgorithm(self, config=None):
16 self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, 'Input layer'))
17 self.addParameter(QgsProcessingParameterNumber(self.DISTANCE, 'Buffer distance', defaultValue=100.0))
18 self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, 'Output layer'))
19
20 def processAlgorithm(self, parameters, context, feedback):
21 source = self.parameterAsSource(parameters, self.INPUT, context)
22 distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
23 (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
24 source.fields(), source.wkbType(), source.sourceCrs())
25
26 for f in source.getFeatures():
27 f.setGeometry(f.geometry().buffer(distance, 5))
28 sink.addFeature(f, QgsFeatureSink.FastInsert)
29
30 return {self.OUTPUT: dest_id}
31
32 def name(self):
33 return 'buffer'
34
35 def displayName(self):
36 return 'Buffer Features'
37
38 def group(self):
39 return 'Examples'
40
41 def groupId(self):
42 return 'examples'
43
44 def createInstance(self):
45 return BufferAlgorithm()