撰寫 Python 腳本並在處理框架下執行(QGIS3)

獨立的 Pyqgis 程式碼可以從 QGIS 中的 Python 主控台執行,不過只要經過些微調整,它也可以在處理框架中執行。這麼做的好處有兩個,首先因為處理框架提供了標準化的介面,所以指定輸出入檔變容易了;再來就是你可以指定你的腳本成為處理建模中的一部份,這樣一來就可以使用批次執行一次處理許多輸入檔。本教學將示範如何撰寫可以當成 QGIS 中處理框架的一部份的 Python 程式碼。

註解

The Processing API was completely overhauled in QGIS3. Please refer to this guide for best practices and tips.

內容說明

本腳本會依照使用者挑選的屬性,進行融合(Dissolve)操作;除此之外,還會把所有被融合的圖徵的另一個屬性值進行加總。在本例中,我們要依照全球範圍的 shapefile 的 CONTINENT 屬性進行融合,然後依照 POP_EST 屬性加總估計融合後的新區域的總人口。

取得資料

我們要使用 Natural Earth 提供的 Admin 0 - 國界 資料集。

下載 Admin 0 - 國界(Countries)shapefile

資料來源 [NATURALEARTH]

為了方便起見,你也可以直接用下面的連結下載包含此圖層的地理套件:

ne_global.gpkg

操作流程

  1. In the QGIS Browser Panel, locate the directory where you saved your downloaded data. Expand the zip or the gpkg entry and select the ne_10m_admin_0_countries layer. Drag the layer to the canvas.
../../_images/1100.png
  1. Go to Processing ‣ Toolbox. Click the Scripts button in the toolbar and select Create New Script from Template.
../../_images/240.png
  1. The template contains all the boilerplate code that is required for the Processing Framework to recognize it as a Processing script, and manage inputs/outputs. Let’s start customizing the example template to our needs. First change the class name from ExampleProcessingAlgorithm to DissolveProcessingAlgorithm. This name also needs to be updated in the createInstance method. Add a docstring to the class that explains what the algorithm does.
../../_images/332.png
  1. As you scroll down, you will see methods that assign name, group, description etc. to the script. Change the return values for name method to be dissolve_with_sum, displayName method to Dissolve with Sum, group method and groupId method to scripts. Change the return value of shortHelpString method to a description that will appear to the user. Click the Save button.
../../_images/417.png
  1. Name the script dissolve_with_sum and save it at the default location under profiles ‣ default ‣ processing ‣ scripts folder.
../../_images/517.png
  1. Now we will define the inputs for the script. The template already contains a definition of an INPUT Vector Layer and a OUTPUT layer. We will add 2 new inputs which allows the user to select a DISSOLVE_FIELD and a SUM_FIELD. Add a new import at the top and the following code in the initAlgorithm method. Click the Run button to preview the changes.
from qgis.core import QgsProcessingParameterField

self.addParameter(
        QgsProcessingParameterField(
                self.DISSOLVE_FIELD,
                'Choose Dissolve Field',
                '',
                self.INPUT))

self.addParameter(
        QgsProcessingParameterField(
                self.SUM_FIELD,
                'Choose Sum Field',
                '',
                self.INPUT))
../../_images/6a.png ../../_images/6b.png
  1. You will see a Dissolve with Sum dialog with our newly defined inputs. Select the ne_10m_admin_0_countries layer as Input layer`. As both Dissolve Field and Sum Fields are filtered based on the input layer, they will be pre-populated with existing fields from the input layer. Click the Close button.
../../_images/716.png
  1. Now we define our custom logic for processing data in the processAlgorithm method. This method gets passed a dictionary called parameters. It contains the inputs that the user has selected. There are helper methods that allow you to take these inputs and create appropriate objects. We first get our inputs using parameterAsSource and parameterAsString methods. Next we want to create a feature sink where we will write the output. QGIS3 has a new class called QgsFeatureSink which is the preferred way to create objects that can accept new features. The output needs only 2 fields - one for the value of dissolved field and another for the sum of the selected field.
from PyQt5.QtCore import QVariant
from qgis.core import QgsField, QgsFields

source = self.parameterAsSource(
        parameters,
        self.INPUT,
        context)
dissolve_field = self.parameterAsString(
        parameters,
        self.DISSOLVE_FIELD,
        context)
sum_field = self.parameterAsString(
        parameters,
        self.SUM_FIELD,
        context)

fields = QgsFields()
fields.append(QgsField(dissolve_field, QVariant.String))
fields.append(QgsField('SUM_' + sum_field, QVariant.Double))

(sink, dest_id) = self.parameterAsSink(
        parameters,
        self.OUTPUT,
        context, fields, source.wkbType(), source.sourceCrs())
../../_images/8a.png ../../_images/8b.png
  1. Now we will ready the input features and create a dictionary to hold the unique values from the dissolve_field and the sum of the values from the sum_field. Note the use of feedback.pushInfo() method to communicate the status with the user.
feedback.pushInfo('Extracting unique values from dissolve_field and computing sum')

features = source.getFeatures()
unique_values = set(f[dissolve_field] for f in features)

# Get Indices of dissolve field and sum field
dissolveIdx = source.fields().indexFromName(dissolve_field)
sumIdx = source.fields().indexFromName(sum_field)

# Find all unique values for the given dissolve_field and
# sum the corresponding values from the sum_field
sum_unique_values = {}
attrs = [{dissolve_field: f[dissolveIdx], sum_field: f[sumIdx]} for f in source.getFeatures()]
for unique_value in unique_values:
        val_list = [ f_attr[sum_field] for f_attr in attrs if f_attr[dissolve_field] == unique_value]
        sum_unique_values[unique_value] = sum(val_list)
../../_images/916.png
  1. Next, we will call the built-in processing algorithm native:dissolve on the input layer to generate the dissolved geometries. Once we have the dissolved geometries, we iterate through the output of the dissolve algorithm and create new features to be added to the output. At the end we return the dest_id FeatureSink as the output. Now the script is ready. Click the Run button.

註解

Notice the use of parameters[self.INPUT] to fetch the input layer from the parameters dictionary directly without defining it as a source. As we are passing the input object to the algorithm without doing anything with it, it’s not necessary to define it as a source.

from qgis.core import QgsFeature

# Running the processing dissolve algorithm
feedback.pushInfo('Dissolving features')
dissolved_layer = processing.run("native:dissolve", {
        'INPUT': parameters[self.INPUT],
        'FIELD': dissolve_field,
        'OUTPUT': 'memory:'
        }, context=context, feedback=feedback)['OUTPUT']

# Read the dissolved layer and create output features
for f in dissolved_layer.getFeatures():
        new_feature =  QgsFeature()
        # Set geometry to dissolved geometry
        new_feature.setGeometry(f.geometry())
        # Set attributes from sum_unique_values dictionary that we had computed
        new_feature.setAttributes([f[dissolve_field], sum_unique_values[f[dissolve_field]]])
        sink.addFeature(new_feature, QgsFeatureSink.FastInsert)

return {self.OUTPUT: dest_id}
../../_images/10a.png ../../_images/10b.png
  1. In the Dissolve with Sum, dialog, select ne_10m_admin_0_countries as the Input layer, CONTINENT as the Dissolve field and POP_EST as the Sum field. Click Run.
../../_images/1120.png
  1. Once the processing is finished, click the Close button and switch to the main QGIS window.
../../_images/1216.png
  1. You will see the dissolved output layer with one feature for every continent and the total population summed from the individual countries belonging to that continent.
../../_images/1316.png
  1. One another advantage of writing processing script is that the methods within the Processing Framework are aware of layer selection and automatically filter your inputs to use only the selected features. This happens because we are defining our input as a QgsProcessingParameterFeatureSource. A feature source allows use of ANY object which contains vector features, not just a vector layer, so when there are selected features in your layer and ask Processing to use selected features, the input is passed on to your script as a QgsProcessingFeatureSource object containing selected features and not the full vector layer. Here’s a quick demonstration of this functionality. Let’s say we want to dissolve only certain continents. Let’s create a selection using Select feature by Expression tool.
../../_images/1416.png
  1. 輸入以下的表達式,按下 選取 之後,就可把南北美洲選取起來,

"CONTINENT" = 'North America' OR "CONTINENT" = 'South America'
../../_images/1514.png
  1. You will see the selected features highlighted in yellow. Locate the dissolve_with_sum script and double-click it to run it.
../../_images/1612.png
  1. In the Dissolve with Sum dialog, select the ne_10m_admin_0_countries as the Input layer. This time, make sure you check the Selected features only box. Choose SUBREGION as the Dissolve field and POP_EST as the Sum field.
../../_images/1713.png
  1. Once the processing finishes, click Close and switch back to the main QGIS window. You will notice a new layer with only the selected features dissolved. Click Identify button and click on a feature to inspect and verify that the script worked correctly.
../../_images/1813.png

以下放上完整的腳本供各位參考,你也可以依照個人需求編輯調整此檔案。

# -*- coding: utf-8 -*-

"""
***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************
"""

from PyQt5.QtCore import QCoreApplication, QVariant
from qgis.core import (QgsProcessing,
                       QgsFeatureSink,
                       QgsFeature,
                       QgsField,
                       QgsFields,
                       QgsProcessingException,
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterFeatureSink,
                       QgsProcessingParameterField,
                       )
import processing


class DissolveProcessingAlgorithm(QgsProcessingAlgorithm):
    """
    Dissolve algorithm that dissolves features based on selected
    attribute and summarizes the selected field by cumputing the
    sum of dissolved features.
    """
    INPUT = 'INPUT'
    OUTPUT = 'OUTPUT'
    DISSOLVE_FIELD = 'dissolve_field'
    SUM_FIELD = 'sum_field'

    def tr(self, string):
        """
        Returns a translatable string with the self.tr() function.
        """
        return QCoreApplication.translate('Processing', string)

    def createInstance(self):
        return DissolveProcessingAlgorithm()

    def name(self):
        """
        Returns the algorithm name, used for identifying the algorithm. This
        string should be fixed for the algorithm, and must not be localised.
        The name should be unique within each provider. Names should contain
        lowercase alphanumeric characters only and no spaces or other
        formatting characters.
        """
        return 'dissolve_with_sum'

    def displayName(self):
        """
        Returns the translated algorithm name, which should be used for any
        user-visible display of the algorithm name.
        """
        return self.tr('Dissolve with Sum')

    def group(self):
        """
        Returns the name of the group this algorithm belongs to. This string
        should be localised.
        """
        return self.tr('scripts')

    def groupId(self):
        """
        Returns the unique ID of the group this algorithm belongs to. This
        string should be fixed for the algorithm, and must not be localised.
        The group id should be unique within each provider. Group id should
        contain lowercase alphanumeric characters only and no spaces or other
        formatting characters.
        """
        return 'scripts'

    def shortHelpString(self):
        """
        Returns a localised short helper string for the algorithm. This string
        should provide a basic description about what the algorithm does and the
        parameters and outputs associated with it..
        """
        return self.tr("Dissolves selected features and creates and sums values of features that were dissolved")

    def initAlgorithm(self, config=None):
        """
        Here we define the inputs and output of the algorithm, along
        with some other properties.
        """
        # We add the input vector features source. It can have any kind of
        # geometry.
        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.INPUT,
                self.tr('Input layer'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )
        self.addParameter(
            QgsProcessingParameterField(
                self.DISSOLVE_FIELD,
                'Choose Dissolve Field',
                '',
                self.INPUT))
        self.addParameter(
            QgsProcessingParameterField(
                self.SUM_FIELD,
                'Choose Sum Field',
                '',
                self.INPUT))
        # We add a feature sink in which to store our processed features (this
        # usually takes the form of a newly created vector layer when the
        # algorithm is run in QGIS).
        self.addParameter(
            QgsProcessingParameterFeatureSink(
                self.OUTPUT,
                self.tr('Output layer')
            )
        )

    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """
        source = self.parameterAsSource(
            parameters,
            self.INPUT,
            context
        )
        dissolve_field = self.parameterAsString(
            parameters,
            self.DISSOLVE_FIELD,
            context)
        sum_field = self.parameterAsString(
            parameters,
            self.SUM_FIELD,
            context)
        
        fields = QgsFields()
        fields.append(QgsField(dissolve_field, QVariant.String))
        fields.append(QgsField('SUM_' + sum_field, QVariant.Double))
        
        (sink, dest_id) = self.parameterAsSink(
            parameters,
            self.OUTPUT,
            context, fields, source.wkbType(), source.sourceCrs())
        
        # Create a dictionary to hold the unique values from the 
        # dissolve_field and the sum of the values from the sum_field
        feedback.pushInfo('Extracting unique values from dissolve_field and computing sum')
        features = source.getFeatures()
        unique_values = set(f[dissolve_field] for f in features)
        # Get Indices of dissolve field and sum field
        dissolveIdx = source.fields().indexFromName(dissolve_field)
        sumIdx = source.fields().indexFromName(sum_field)
        
        # Find all unique values for the given dissolve_field and
        # sum the corresponding values from the sum_field
        sum_unique_values = {}
        attrs = [{dissolve_field: f[dissolveIdx], sum_field: f[sumIdx]}
                for f in source.getFeatures()]
        for unique_value in unique_values:
            val_list = [ f_attr[sum_field] 
                for f_attr in attrs if f_attr[dissolve_field] == unique_value]
            sum_unique_values[unique_value] = sum(val_list)
        
        # Running the processing dissolve algorithm
        feedback.pushInfo('Dissolving features')
        dissolved_layer = processing.run("native:dissolve", {
            'INPUT': parameters[self.INPUT],
            'FIELD': dissolve_field,
            'OUTPUT': 'memory:'
        }, context=context, feedback=feedback)['OUTPUT']
        
        # Read the dissolved layer and create output features
        for f in dissolved_layer.getFeatures():
            new_feature =  QgsFeature()
            # Set geometry to dissolved geometry
            new_feature.setGeometry(f.geometry())
            # Set attributes from sum_unique_values dictionary that we had computed
            new_feature.setAttributes([f[dissolve_field], sum_unique_values[f[dissolve_field]]])
            sink.addFeature(new_feature, QgsFeatureSink.FastInsert)
        
        return {self.OUTPUT: dest_id}
comments powered by Disqus

This work is licensed under a Creative Commons Attribution 4.0 International License