Skip to content
Permalink
Browse files

Merge pull request #6225 from alexbruy/processing-algs

[processing][needs-docs] use native syntax for Processing scripts
  • Loading branch information
alexbruy committed Feb 5, 2018
2 parents 6841c52 + 567ebb9 commit b6c2de48fa363d0cd9d39a1d54cdc5b9d5160f25
Showing with 679 additions and 1,606 deletions.
  1. +0 −1 images/images.qrc
  2. +0 −14 images/themes/default/providerR.svg
  3. +0 −39 python/plugins/processing/algs/examplescripts/ProcessingExampleScriptsPlugin.py
  4. +0 −31 python/plugins/processing/algs/examplescripts/__init__.py
  5. +0 −18 python/plugins/processing/algs/examplescripts/metadata.txt
  6. +0 −5 python/plugins/processing/algs/examplescripts/scripts/examplescript.py
  7. +0 −5 python/plugins/processing/algs/qgis/CMakeLists.txt
  8. +107 −0 python/plugins/processing/algs/qgis/KeepNBiggestParts.py
  9. +8 −6 python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py
  10. +0 −44 python/plugins/processing/algs/qgis/scripts/Keep_n_biggest_parts.py
  11. +1 −23 python/plugins/processing/core/Processing.py
  12. +1 −1 python/plugins/processing/gui/ContextAction.py
  13. +0 −56 python/plugins/processing/gui/CreateNewScriptAction.py
  14. +1 −1 python/plugins/processing/gui/ProcessingToolbox.py
  15. +0 −5 python/plugins/processing/gui/ToolboxAction.py
  16. +2 −2 python/plugins/processing/modeler/AddModelFromFileAction.py
  17. +2 −2 python/plugins/processing/modeler/CreateNewModelAction.py
  18. +8 −6 python/plugins/processing/modeler/ModelerAlgorithmProvider.py
  19. +4 −4 python/plugins/processing/modeler/ModelerDialog.py
  20. +25 −36 python/plugins/processing/script/AddScriptFromFileAction.py
  21. +17 −5 python/plugins/processing/script/{WrongScriptException.py → CreateNewScriptAction.py}
  22. +0 −104 python/plugins/processing/script/CreateScriptCollectionPluginAction.py
  23. +16 −15 python/plugins/processing/{gui → script}/DeleteScriptAction.py
  24. +20 −11 python/plugins/processing/{gui → script}/EditScriptAction.py
  25. +0 −313 python/plugins/processing/script/ScriptAlgorithm.py
  26. +37 −31 python/plugins/processing/script/ScriptAlgorithmProvider.py
  27. +47 −58 python/plugins/processing/{gui → script}/ScriptEdit.py
  28. +99 −141 python/plugins/processing/{gui → script}/ScriptEditorDialog.py
  29. +0 −117 python/plugins/processing/script/ScriptSelector.py
  30. +51 −45 python/plugins/processing/script/ScriptUtils.py
  31. +0 −14 python/plugins/processing/script/snippets.py
  32. +2 −8 python/plugins/processing/tests/AlgorithmsTestBase.py
  33. +0 −1 python/plugins/processing/tests/CMakeLists.txt
  34. +0 −57 python/plugins/processing/tests/ScriptAlgorithmsTest.py
  35. +45 −0 python/plugins/processing/tests/testdata/expected/biggest_parts.gml
  36. +43 −0 python/plugins/processing/tests/testdata/expected/biggest_parts.xsd
  37. +13 −23 python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
  38. +128 −185 python/plugins/processing/ui/DlgModeler.ui
  39. +2 −11 python/plugins/processing/ui/DlgScriptEditor.ui
  40. +0 −168 python/plugins/processing/ui/scriptselector.ui
@@ -596,7 +596,6 @@
<file>themes/default/providerGdal.svg</file>
<file>themes/default/providerGrass.svg</file>
<file>themes/default/providerQgis.svg</file>
<file>themes/default/providerR.svg</file>
<file>themes/default/providerSaga.svg</file>
<file>themes/default/processingModel.svg</file>
<file>themes/default/processingScript.svg</file>

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -1,9 +1,4 @@
FILE(GLOB PY_FILES *.py)
FILE(GLOB HELP_FILES help/*.rst)
FILE(GLOB SCRIPT_FILES scripts/*.*)

ADD_SUBDIRECTORY(ui)

PLUGIN_INSTALL(processing algs/qgis ${PY_FILES})
PLUGIN_INSTALL(processing algs/qgis/help ${HELP_FILES})
PLUGIN_INSTALL(processing algs/qgis/scripts ${SCRIPT_FILES})
@@ -0,0 +1,107 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
KeepNBiggestParts.py
---------------------
Date : July 2014
Copyright : (C) 2014 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""

__author__ = 'Victor Olaya'
__date__ = 'July 2014'
__copyright__ = '(C) 2014, Victor Olaya'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from operator import itemgetter


from qgis.core import (QgsGeometry,
QgsFeatureSink,
QgsProcessing,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterNumber,
)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm


class KeepNBiggestParts(QgisAlgorithm):

POLYGONS = 'POLYGONS'
PARTS = 'PARTS'
OUTPUT = 'OUTPUT'

def group(self):
return self.tr('Vector geometry')

def groupId(self):
return 'vectorgeometry'

def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSource(self.POLYGONS,
self.tr('Polygons'), [QgsProcessing.TypeVectorPolygon]))
self.addParameter(QgsProcessingParameterNumber(self.PARTS,
self.tr('Parts to keep'),
QgsProcessingParameterNumber.Integer,
1, False, 1))
self.addParameter(
QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Parts'), QgsProcessing.TypeVectorPolygon))

def name(self):
return 'keepnbiggestparts'

def displayName(self):
return self.tr('Keep N biggest parts')

def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.POLYGONS, context)
parts = self.parameterAsInt(parameters, self.PARTS, context)

fields = source.fields()
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
source.fields(), source.wkbType(), source.sourceCrs())

features = source.getFeatures()
total = 100.0 / source.featureCount() if source.featureCount() else 0
for current, feat in enumerate(features):
if feedback.isCanceled():
break

geom = feat.geometry()
if geom.isMultipart():
out_feature = feat
geoms = geom.asGeometryCollection()
geom_area = [(i, geoms[i].area()) for i in range(len(geoms))]
geom_area.sort(key=itemgetter(1))
if parts == 1:
out_feature.setGeometry(geoms[geom_area[-1][0]])
elif parts > len(geoms):
out_feature.setGeometry(geom)
else:
out_feature.setGeometry(geom)
geomres = [geoms[i].asPolygon() for i, a in geom_area[-1 * parts:]]
out_feature.setGeometry(QgsGeometry.fromMultiPolygonXY(geomres))
sink.addFeature(out_feature, QgsFeatureSink.FastInsert)
else:
sink.addFeature(feat, QgsFeatureSink.FastInsert)

feedback.setProgress(int(current * total))

return {self.OUTPUT: dest_id}
@@ -36,7 +36,7 @@
from qgis.core import (QgsApplication,
QgsProcessingProvider)

from processing.script.ScriptUtils import ScriptUtils
from processing.script import ScriptUtils

from .QgisAlgorithm import QgisAlgorithm

@@ -80,6 +80,7 @@
from .ImportIntoPostGIS import ImportIntoPostGIS
from .ImportIntoSpatialite import ImportIntoSpatialite
from .Intersection import Intersection
from .KeepNBiggestParts import KeepNBiggestParts
from .LinesToPolygons import LinesToPolygons
from .MinimumBoundingGeometry import MinimumBoundingGeometry
from .NearestNeighbourAnalysis import NearestNeighbourAnalysis
@@ -200,6 +201,7 @@ def getAlgs(self):
ImportIntoPostGIS(),
ImportIntoSpatialite(),
Intersection(),
KeepNBiggestParts(),
LinesToPolygons(),
MinimumBoundingGeometry(),
NearestNeighbourAnalysis(),
@@ -288,11 +290,11 @@ def getAlgs(self):
VectorLayerScatterplot3D()])

# to store algs added by 3rd party plugins as scripts
folder = os.path.join(os.path.dirname(__file__), 'scripts')
scripts = ScriptUtils.loadFromFolder(folder)
for script in scripts:
script.allowEdit = False
algs.extend(scripts)
#folder = os.path.join(os.path.dirname(__file__), 'scripts')
#scripts = ScriptUtils.loadFromFolder(folder)
#for script in scripts:
# script.allowEdit = False
#algs.extend(scripts)

return algs

This file was deleted.

@@ -45,12 +45,12 @@
QgsProcessingOutputMapLayer)

import processing
from processing.script.ScriptUtils import ScriptUtils
from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.MessageBarProgress import MessageBarProgress
from processing.gui.RenderingStyles import RenderingStyles
from processing.gui.Postprocessing import handleAlgorithmResults
from processing.gui.AlgorithmExecutor import execute
from processing.script import ScriptUtils
from processing.tools import dataobjects

from processing.algs.qgis.QgisAlgorithmProvider import QgisAlgorithmProvider # NOQA
@@ -103,28 +103,6 @@ def deinitialize():

Processing.BASIC_PROVIDERS = []

@staticmethod
def addScripts(folder):
Processing.initialize()
provider = QgsApplication.processingRegistry().providerById("qgis")
scripts = ScriptUtils.loadFromFolder(folder)
# fix_print_with_import
print(scripts)
for script in scripts:
script.allowEdit = False
script._icon = provider.icon()
provider.externalAlgs.extend(scripts)
provider.refreshAlgorithms()

@staticmethod
def removeScripts(folder):
provider = QgsApplication.processingRegistry().providerById("qgis")
for alg in provider.externalAlgs[::-1]:
path = os.path.dirname(alg.descriptionFile)
if path == folder:
provider.externalAlgs.remove(alg)
provider.refreshAlgorithms()

@staticmethod
def runAlgorithm(algOrName, parameters, onFinish=None, feedback=None, context=None):
if isinstance(algOrName, QgsProcessingAlgorithm):
@@ -37,5 +37,5 @@ def setData(self, itemData, toolbox):

def tr(self, string, context=''):
if context == '':
context = 'ContextAction'
context = self.__class__.__name__
return QCoreApplication.translate(context, string)

0 comments on commit b6c2de4

Please sign in to comment.
You can’t perform that action at this time.