Skip to content

Commit

Permalink
Merge pull request #6225 from alexbruy/processing-algs
Browse files Browse the repository at this point in the history
[processing][needs-docs] use native syntax for Processing scripts
  • Loading branch information
alexbruy committed Feb 5, 2018
2 parents 6841c52 + 567ebb9 commit b6c2de4
Show file tree
Hide file tree
Showing 40 changed files with 679 additions and 1,606 deletions.
1 change: 0 additions & 1 deletion images/images.qrc
Expand Up @@ -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>
Expand Down
14 changes: 0 additions & 14 deletions images/themes/default/providerR.svg

This file was deleted.

This file was deleted.

31 changes: 0 additions & 31 deletions python/plugins/processing/algs/examplescripts/__init__.py

This file was deleted.

18 changes: 0 additions & 18 deletions python/plugins/processing/algs/examplescripts/metadata.txt

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions python/plugins/processing/algs/qgis/CMakeLists.txt
@@ -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})
107 changes: 107 additions & 0 deletions python/plugins/processing/algs/qgis/KeepNBiggestParts.py
@@ -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}
14 changes: 8 additions & 6 deletions python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -200,6 +201,7 @@ def getAlgs(self):
ImportIntoPostGIS(),
ImportIntoSpatialite(),
Intersection(),
KeepNBiggestParts(),
LinesToPolygons(),
MinimumBoundingGeometry(),
NearestNeighbourAnalysis(),
Expand Down Expand Up @@ -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

Expand Down

This file was deleted.

24 changes: 1 addition & 23 deletions python/plugins/processing/core/Processing.py
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/gui/ContextAction.py
Expand Up @@ -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.