Skip to content
Permalink
Browse files

[processing] use QgsProcessingException in algorithms

  • Loading branch information
alexbruy committed Jul 13, 2017
1 parent 69dab42 commit 97a5a3dcbd3e05770456fd28baf8526c667206e2
@@ -37,7 +37,6 @@
from qgis.PyQt.QtGui import QIcon

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

@@ -31,14 +31,14 @@
QgsWkbTypes,
QgsFeatureSink,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)


from qgis.PyQt.QtGui import QIcon

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

@@ -84,7 +84,7 @@ def processAlgorithm(self, parameters, context, feedback):
if input_geometry:
output_geometry = QgsGeometry.fromRect(input_geometry.boundingBox())
if not output_geometry:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Error calculating bounding box'))

output_feature.setGeometry(output_geometry)
@@ -26,18 +26,19 @@

__revision__ = '$Format:%H$'

from math import sqrt

from qgis.core import (QgsFeature,
QgsFeatureSink,
QgsWkbTypes,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterFeatureSink)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
import processing
from math import sqrt
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm


class ConcaveHull(QgisAlgorithm):
@@ -89,7 +90,7 @@ def processAlgorithm(self, parameters, context, feedback):
features = delaunay_layer.getFeatures()
count = delaunay_layer.featureCount()
if count == 0:
raise GeoAlgorithmExecutionException(self.tr('No Delaunay triangles created.'))
raise QgsProcessingException(self.tr('No Delaunay triangles created.'))

counter = 50. / count
lengths = []
@@ -40,11 +40,11 @@
QgsWkbTypes,
QgsProcessing,
QgsFields,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

from . import voronoi

@@ -112,7 +112,7 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.setProgress(int(current * total))

if len(pts) < 3:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Input file should contain at least 3 points. Choose '
'another file and try again.'))

@@ -37,6 +37,7 @@
QgsPointXY,
QgsWkbTypes,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterEnum,
QgsProcessingParameterExtent,
QgsProcessingParameterNumber,
@@ -45,7 +46,6 @@
QgsFields)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

@@ -122,19 +122,19 @@ def processAlgorithm(self, parameters, context, feedback):
originY = bbox.yMaximum()

if hSpacing <= 0 or vSpacing <= 0:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Invalid grid spacing: {0}/{1}').format(hSpacing, vSpacing))

if width < hSpacing:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Horizontal spacing is too small for the covered area'))

if hSpacing <= hOverlay or vSpacing <= vOverlay:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Invalid overlay: {0}/{1}').format(hOverlay, vOverlay))

if height < vSpacing:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Vertical spacing is too small for the covered area'))

fields = QgsFields()
@@ -264,7 +264,7 @@ def _hexagonGrid(self, sink, width, height, originX, originY,

hOverlay = hSpacing - hOverlay
if hOverlay < 0:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('To preserve symmetry, hspacing is fixed relative to vspacing\n \
hspacing is fixed at: {0} and hoverlay is fixed at: {1}\n \
hoverlay cannot be negative. Increase hoverlay.').format(hSpacing, hOverlay)
@@ -28,14 +28,14 @@
from qgis.core import (QgsVectorLayerExporter,
QgsSettings,
QgsFeatureSink,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterString,
QgsProcessingParameterField,
QgsProcessingParameterBoolean,
QgsWkbTypes)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.tools import postgis


@@ -167,7 +167,7 @@ def processAlgorithm(self, parameters, context, feedback):
source.wkbType(), source.sourceCrs(), overwrite, options)

if exporter.errorCode() != QgsVectorLayerExporter.NoError:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Error importing to PostGIS\n{0}').format(exporter.errorMessage()))

features = source.getFeatures()
@@ -183,7 +183,7 @@ def processAlgorithm(self, parameters, context, feedback):

exporter.flushBuffer()
if exporter.errorCode() != QgsVectorLayerExporter.NoError:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Error importing to PostGIS\n{0}').format(exporter.errorMessage()))

if geomColumn and createIndex:
@@ -28,6 +28,7 @@
from qgis.core import (QgsDataSourceUri,
QgsFeatureSink,
QgsVectorLayerExporter,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterField,
@@ -36,7 +37,6 @@
QgsWkbTypes)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.tools import spatialite


@@ -137,7 +137,7 @@ def processAlgorithm(self, parameters, context, feedback):
source.wkbType(), source.sourceCrs(), overwrite, options)

if exporter.errorCode() != QgsVectorLayerExporter.NoError:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Error importing to Spatialite\n{0}').format(exporter.errorMessage()))

features = source.getFeatures()
@@ -153,7 +153,7 @@ def processAlgorithm(self, parameters, context, feedback):

exporter.flushBuffer()
if exporter.errorCode() != QgsVectorLayerExporter.NoError:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Error importing to Spatialite\n{0}').format(exporter.errorMessage()))

if geomColumn and createIndex:
@@ -34,12 +34,12 @@
QgsFeatureSink,
QgsGeometry,
QgsWkbTypes,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsSpatialIndex,
QgsProcessingUtils)

from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import vector

@@ -132,7 +132,7 @@ def processAlgorithm(self, parameters, context, feedback):
int_sym = geom.symDifference(tmpGeom)
int_geom = QgsGeometry(int_com.difference(int_sym))
if int_geom.isEmpty() or not int_geom.isGeosValid():
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('GEOS geoprocessing error: One or '
'more input features have invalid '
'geometry.'))
@@ -145,7 +145,7 @@ def processAlgorithm(self, parameters, context, feedback):
outFeat.setAttributes(attrs)
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
except:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Feature geometry error: One or more '
'output features ignored due to invalid '
'geometry.'))
@@ -34,12 +34,12 @@
QgsFeatureRequest,
QgsFeatureSink,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterFeatureSink,
QgsMapLayer)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

@@ -78,12 +78,12 @@ def processAlgorithm(self, parameters, context, feedback):
totalFeatureCount = 0
for layer in input_layers:
if layer.type() != QgsMapLayer.VectorLayer:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('All layers must be vector layers!'))

if (len(layers) > 0):
if (layer.wkbType() != layers[0].wkbType()):
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('All layers must have same geometry type!'))

layers.append(layer)
@@ -95,7 +95,7 @@ def processAlgorithm(self, parameters, context, feedback):
if (dfield.name().upper() == sfield.name().upper()):
found = dfield
if (dfield.type() != sfield.type()):
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('{} field in layer {} has different '
'data type than in other layers.'.format(sfield.name(), layerSource)))

@@ -142,7 +142,7 @@ def processAlgorithm(self, parameters, context, feedback):
for sindex, sfield in enumerate(layer.fields()):
if (sfield.name().upper() == dfield.name().upper()):
if (sfield.type() != dfield.type()):
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Attribute type mismatch'))
dattribute = sattributes[sindex]
break
@@ -26,9 +26,8 @@

__revision__ = '$Format:%H$'

from qgis.core import (QgsProcessingParameterString)
from qgis.core import (QgsProcessingException, QgsProcessingParameterString)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.tools import postgis


@@ -67,6 +66,6 @@ def processAlgorithm(self, parameters, context, feedback):
try:
db._exec_sql_and_commit(str(sql))
except postgis.DbError as e:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Error executing SQL:\n{0}').format(str(e)))
return {}
@@ -29,12 +29,12 @@
import random

from qgis.core import (QgsFeatureSink,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterEnum,
QgsProcessingParameterNumber,
QgsProcessingParameterFeatureSink)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException


class RandomExtract(QgisAlgorithm):
@@ -82,12 +82,12 @@ def processAlgorithm(self, parameters, context, feedback):

if method == 0:
if value > featureCount:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Selected number is greater than feature count. '
'Choose a lower value and try again.'))
else:
if value > 100:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr("Percentage can't be greater than 100. Set a "
"different value and try again."))
value = int(round(value / 100.0000, 4) * featureCount)
@@ -29,14 +29,14 @@
import random

from qgis.core import (QgsFeatureSink,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterEnum,
QgsProcessingParameterField,
QgsProcessingParameterNumber,
QgsProcessingParameterFeatureSink)
from collections import defaultdict
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException


class RandomExtractWithinSubsets(QgisAlgorithm):
@@ -92,12 +92,12 @@ def processAlgorithm(self, parameters, context, feedback):
value = self.parameterAsInt(parameters, self.NUMBER, context)
if method == 0:
if value > featureCount:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Selected number is greater that feature count. '
'Choose lesser value and try again.'))
else:
if value > 100:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr("Percentage can't be greater than 100. Set "
"correct value and try again."))
value = value / 100.0
@@ -27,13 +27,13 @@

from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsExpression,
QgsProcessingException,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterField,
QgsProcessingParameterEnum,
QgsProcessingParameterString,
QgsProcessingOutputVectorLayer)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException


class SelectByAttribute(QgisAlgorithm):
@@ -112,7 +112,7 @@ def processAlgorithm(self, parameters, context, feedback):

if fieldType != QVariant.String and operator in self.STRING_OPERATORS:
op = ''.join(['"%s", ' % o for o in self.STRING_OPERATORS])
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Operators {0} can be used only with string fields.').format(op))

field_ref = QgsExpression.quotedColumnRef(fieldName)
@@ -132,7 +132,7 @@ def processAlgorithm(self, parameters, context, feedback):

expression = QgsExpression(expression_string)
if expression.hasParserError():
raise GeoAlgorithmExecutionException(expression.parserErrorString())
raise QgsProcessingException(expression.parserErrorString())

layer.selectByExpression(expression_string)
return {self.OUTPUT: parameters[self.INPUT]}

0 comments on commit 97a5a3d

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