Last active
September 18, 2019 14:29
-
-
Save gacarrillor/e4ab41d9784d52fc7f9fb862e74fd492 to your computer and use it in GitHub Desktop.
Script algorithm that modifies the input layer and returns it as its output.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE model> | |
<Option type="Map"> | |
<Option name="children" type="Map"> | |
<Option name="qgis:basicstatisticsforfields_1" type="Map"> | |
<Option name="active" type="bool" value="true"/> | |
<Option name="alg_config"/> | |
<Option name="alg_id" type="QString" value="qgis:basicstatisticsforfields"/> | |
<Option name="component_description" type="QString" value="Basic statistics for fields"/> | |
<Option name="component_pos_x" type="double" value="741"/> | |
<Option name="component_pos_y" type="double" value="494"/> | |
<Option name="dependencies"/> | |
<Option name="id" type="QString" value="qgis:basicstatisticsforfields_1"/> | |
<Option name="outputs" type="Map"> | |
<Option name="results" type="Map"> | |
<Option name="child_id" type="QString" value="qgis:basicstatisticsforfields_1"/> | |
<Option name="component_description" type="QString" value="results"/> | |
<Option name="component_pos_x" type="double" value="745"/> | |
<Option name="component_pos_y" type="double" value="602"/> | |
<Option name="default_value" type="invalid"/> | |
<Option name="mandatory" type="bool" value="false"/> | |
<Option name="name" type="QString" value="results"/> | |
<Option name="output_name" type="QString" value="OUTPUT_HTML_FILE"/> | |
</Option> | |
</Option> | |
<Option name="outputs_collapsed" type="bool" value="true"/> | |
<Option name="parameters_collapsed" type="bool" value="true"/> | |
<Option name="params" type="Map"> | |
<Option name="FIELD_NAME" type="List"> | |
<Option type="Map"> | |
<Option name="parameter_name" type="QString" value="newfieldname"/> | |
<Option name="source" type="int" value="0"/> | |
</Option> | |
</Option> | |
<Option name="INPUT_LAYER" type="List"> | |
<Option type="Map"> | |
<Option name="child_id" type="QString" value="script:rename_field_1"/> | |
<Option name="output_name" type="QString" value="OUTPUT"/> | |
<Option name="source" type="int" value="1"/> | |
</Option> | |
</Option> | |
</Option> | |
</Option> | |
<Option name="script:rename_field_1" type="Map"> | |
<Option name="active" type="bool" value="true"/> | |
<Option name="alg_config"/> | |
<Option name="alg_id" type="QString" value="script:rename_field"/> | |
<Option name="component_description" type="QString" value="Rename Field"/> | |
<Option name="component_pos_x" type="double" value="441"/> | |
<Option name="component_pos_y" type="double" value="408"/> | |
<Option name="dependencies"/> | |
<Option name="id" type="QString" value="script:rename_field_1"/> | |
<Option name="outputs"/> | |
<Option name="outputs_collapsed" type="bool" value="true"/> | |
<Option name="parameters_collapsed" type="bool" value="true"/> | |
<Option name="params" type="Map"> | |
<Option name="INPUT" type="List"> | |
<Option type="Map"> | |
<Option name="parameter_name" type="QString" value="inputlayer"/> | |
<Option name="source" type="int" value="0"/> | |
</Option> | |
</Option> | |
<Option name="INPUT_FIELD" type="List"> | |
<Option type="Map"> | |
<Option name="parameter_name" type="QString" value="inputfield"/> | |
<Option name="source" type="int" value="0"/> | |
</Option> | |
</Option> | |
<Option name="NEW_FIELD_NAME" type="List"> | |
<Option type="Map"> | |
<Option name="parameter_name" type="QString" value="newfieldname"/> | |
<Option name="source" type="int" value="0"/> | |
</Option> | |
</Option> | |
</Option> | |
</Option> | |
</Option> | |
<Option name="help"/> | |
<Option name="modelVariables"/> | |
<Option name="model_group" type="QString" value=""/> | |
<Option name="model_name" type="QString" value="model_with_rename_field_script"/> | |
<Option name="parameterDefinitions" type="Map"> | |
<Option name="inputfield" type="Map"> | |
<Option name="allow_multiple" type="bool" value="false"/> | |
<Option name="data_type" type="int" value="-1"/> | |
<Option name="default" type="invalid"/> | |
<Option name="description" type="QString" value="input_field"/> | |
<Option name="flags" type="int" value="0"/> | |
<Option name="metadata"/> | |
<Option name="name" type="QString" value="inputfield"/> | |
<Option name="parameter_type" type="QString" value="field"/> | |
<Option name="parent_layer" type="QString" value="inputlayer"/> | |
</Option> | |
<Option name="inputlayer" type="Map"> | |
<Option name="data_types" type="List"> | |
<Option type="int" value="5"/> | |
</Option> | |
<Option name="default" type="invalid"/> | |
<Option name="description" type="QString" value="input_layer"/> | |
<Option name="flags" type="int" value="0"/> | |
<Option name="metadata"/> | |
<Option name="name" type="QString" value="inputlayer"/> | |
<Option name="parameter_type" type="QString" value="vector"/> | |
</Option> | |
<Option name="newfieldname" type="Map"> | |
<Option name="default" type="QString" value=""/> | |
<Option name="description" type="QString" value="new_field_name"/> | |
<Option name="flags" type="int" value="0"/> | |
<Option name="metadata"/> | |
<Option name="multiline" type="bool" value="false"/> | |
<Option name="name" type="QString" value="newfieldname"/> | |
<Option name="parameter_type" type="QString" value="string"/> | |
</Option> | |
<Option name="qgis:basicstatisticsforfields_1:results" type="Map"> | |
<Option name="create_by_default" type="bool" value="true"/> | |
<Option name="default" type="invalid"/> | |
<Option name="description" type="QString" value="results"/> | |
<Option name="file_filter" type="QString" value="HTML files (*.html)"/> | |
<Option name="flags" type="int" value="8"/> | |
<Option name="metadata"/> | |
<Option name="name" type="QString" value="qgis:basicstatisticsforfields_1:results"/> | |
<Option name="parameter_type" type="QString" value="fileDestination"/> | |
<Option name="supports_non_file_outputs" type="bool" value="true"/> | |
</Option> | |
</Option> | |
<Option name="parameters" type="Map"> | |
<Option name="inputfield" type="Map"> | |
<Option name="component_description" type="QString" value="inputfield"/> | |
<Option name="component_pos_x" type="double" value="172"/> | |
<Option name="component_pos_y" type="double" value="261"/> | |
<Option name="name" type="QString" value="inputfield"/> | |
</Option> | |
<Option name="inputlayer" type="Map"> | |
<Option name="component_description" type="QString" value="inputlayer"/> | |
<Option name="component_pos_x" type="double" value="143"/> | |
<Option name="component_pos_y" type="double" value="380"/> | |
<Option name="name" type="QString" value="inputlayer"/> | |
</Option> | |
<Option name="newfieldname" type="Map"> | |
<Option name="component_description" type="QString" value="newfieldname"/> | |
<Option name="component_pos_x" type="double" value="606"/> | |
<Option name="component_pos_y" type="double" value="302"/> | |
<Option name="name" type="QString" value="newfieldname"/> | |
</Option> | |
</Option> | |
</Option> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 qgis.PyQt.QtCore import QCoreApplication | |
from qgis.core import (QgsProcessing, | |
QgsProcessingAlgorithm, | |
QgsProcessingParameterVectorLayer, | |
QgsProcessingParameterString, | |
QgsProcessingOutputVectorLayer) | |
from qgis import processing | |
class ExampleProcessingAlgorithm(QgsProcessingAlgorithm): | |
INPUT = 'INPUT' | |
INPUT_FIELD = 'INPUT_FIELD' | |
NEW_FIELD_NAME = 'NEW_FIELD_NAME' | |
OUTPUT = 'OUTPUT' | |
def tr(self, string): | |
""" | |
Returns a translatable string with the self.tr() function. | |
""" | |
return QCoreApplication.translate('Processing', string) | |
def createInstance(self): | |
return ExampleProcessingAlgorithm() | |
def name(self): | |
return 'rename_field' | |
def displayName(self): | |
return self.tr('Rename Field') | |
def group(self): | |
return self.tr('Example scripts') | |
def groupId(self): | |
return 'examplescripts' | |
def shortHelpString(self): | |
return self.tr("Rename Field") | |
def initAlgorithm(self, config=None): | |
""" | |
Here we define the inputs and output of the algorithm, along | |
with some other properties. | |
""" | |
self.addParameter( | |
QgsProcessingParameterVectorLayer( | |
self.INPUT, | |
self.tr('Input layer'), | |
[QgsProcessing.TypeVector] | |
) | |
) | |
self.addParameter( | |
QgsProcessingParameterString( | |
self.INPUT_FIELD, | |
self.tr('Input field name') | |
) | |
) | |
self.addParameter( | |
QgsProcessingParameterString( | |
self.NEW_FIELD_NAME, | |
self.tr('New field name') | |
) | |
) | |
self.addOutput( | |
QgsProcessingOutputVectorLayer( | |
self.OUTPUT, | |
self.tr('Output layer') | |
) | |
) | |
def processAlgorithm(self, parameters, context, feedback): | |
source = self.parameterAsVectorLayer( | |
parameters, | |
self.INPUT, | |
context | |
) | |
input_field = self.parameterAsString( | |
parameters, | |
self.INPUT_FIELD, | |
context) | |
new_name = self.parameterAsString( | |
parameters, | |
self.NEW_FIELD_NAME, | |
context) | |
# Rename the field | |
source.startEditing() | |
source.renameAttribute(source.fields().indexFromName(input_field), new_name) | |
source.commitChanges() | |
return {self.OUTPUT: source} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment