Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #4851 from alexbruy/processing-exception
[processing] use QgsProcessingException instead of old exception class
  • Loading branch information
alexbruy committed Jul 13, 2017
2 parents 50df255 + 5620854 commit a2b82bf
Show file tree
Hide file tree
Showing 25 changed files with 72 additions and 122 deletions.
1 change: 0 additions & 1 deletion python/plugins/processing/algs/qgis/Boundary.py
Expand Up @@ -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]

Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/BoundingBox.py
Expand Up @@ -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]

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions python/plugins/processing/algs/qgis/ConcaveHull.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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 = []
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/Delaunay.py
Expand Up @@ -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

Expand Down Expand Up @@ -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.'))

Expand Down
12 changes: 6 additions & 6 deletions python/plugins/processing/algs/qgis/GridPolygon.py
Expand Up @@ -37,6 +37,7 @@
QgsPointXY,
QgsWkbTypes,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterEnum,
QgsProcessingParameterExtent,
QgsProcessingParameterNumber,
Expand All @@ -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]

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/ImportIntoPostGIS.py
Expand Up @@ -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


Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/ImportIntoSpatialite.py
Expand Up @@ -28,6 +28,7 @@
from qgis.core import (QgsDataSourceUri,
QgsFeatureSink,
QgsVectorLayerExporter,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterField,
Expand All @@ -36,7 +37,6 @@
QgsWkbTypes)

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


Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/Intersection.py
Expand Up @@ -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

Expand Down Expand Up @@ -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.'))
Expand All @@ -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.'))
Expand Down
10 changes: 5 additions & 5 deletions python/plugins/processing/algs/qgis/Merge.py
Expand Up @@ -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]

Expand Down Expand Up @@ -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)
Expand All @@ -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)))

Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions python/plugins/processing/algs/qgis/PostGISExecuteSQL.py
Expand Up @@ -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


Expand Down Expand Up @@ -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 {}
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/RandomExtract.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/SelectByAttribute.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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 a2b82bf

Please sign in to comment.