[processing] use QgsProcessingException in algorithms

This commit is contained in:
Alexander Bruy 2017-07-13 08:55:17 +03:00
parent 69dab42785
commit 97a5a3dcbd
17 changed files with 49 additions and 50 deletions

View File

@ -37,7 +37,6 @@ from qgis.core import (QgsGeometry,
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]

View File

@ -31,6 +31,7 @@ from qgis.core import (QgsGeometry,
QgsWkbTypes,
QgsFeatureSink,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)
@ -38,7 +39,6 @@ from qgis.core import (QgsGeometry,
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 @@ class BoundingBox(QgisAlgorithm):
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)

View File

@ -26,18 +26,19 @@ __copyright__ = '(C) 2014, Piotr Pociask'
__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 @@ class ConcaveHull(QgisAlgorithm):
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 = []

View File

@ -40,11 +40,11 @@ from qgis.core import (QgsField,
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 @@ class Delaunay(QgisAlgorithm):
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.'))

View File

@ -37,6 +37,7 @@ from qgis.core import (QgsField,
QgsPointXY,
QgsWkbTypes,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterEnum,
QgsProcessingParameterExtent,
QgsProcessingParameterNumber,
@ -45,7 +46,6 @@ from qgis.core import (QgsField,
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 @@ class GridPolygon(QgisAlgorithm):
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 @@ class GridPolygon(QgisAlgorithm):
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)

View File

@ -28,6 +28,7 @@ __revision__ = '$Format:%H$'
from qgis.core import (QgsVectorLayerExporter,
QgsSettings,
QgsFeatureSink,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterString,
QgsProcessingParameterField,
@ -35,7 +36,6 @@ from qgis.core import (QgsVectorLayerExporter,
QgsWkbTypes)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.tools import postgis
@ -167,7 +167,7 @@ class ImportIntoPostGIS(QgisAlgorithm):
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 @@ class ImportIntoPostGIS(QgisAlgorithm):
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:

View File

@ -28,6 +28,7 @@ __revision__ = '$Format:%H$'
from qgis.core import (QgsDataSourceUri,
QgsFeatureSink,
QgsVectorLayerExporter,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterField,
@ -36,7 +37,6 @@ from qgis.core import (QgsDataSourceUri,
QgsWkbTypes)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.tools import spatialite
@ -137,7 +137,7 @@ class ImportIntoSpatialite(QgisAlgorithm):
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 @@ class ImportIntoSpatialite(QgisAlgorithm):
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:

View File

@ -34,12 +34,12 @@ from qgis.core import (QgsFeatureRequest,
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 @@ class Intersection(QgisAlgorithm):
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 @@ class Intersection(QgisAlgorithm):
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.'))

View File

@ -34,12 +34,12 @@ from qgis.core import (QgsFields,
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 @@ class Merge(QgisAlgorithm):
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 @@ class Merge(QgisAlgorithm):
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 @@ class Merge(QgisAlgorithm):
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

View File

@ -26,9 +26,8 @@ __copyright__ = '(C) 2012, Victor Olaya, Carterix Geomatics'
__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 @@ class PostGISExecuteSQL(QgisAlgorithm):
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 {}

View File

@ -29,12 +29,12 @@ __revision__ = '$Format:%H$'
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 @@ class RandomExtract(QgisAlgorithm):
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)

View File

@ -29,6 +29,7 @@ __revision__ = '$Format:%H$'
import random
from qgis.core import (QgsFeatureSink,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterEnum,
QgsProcessingParameterField,
@ -36,7 +37,6 @@ from qgis.core import (QgsFeatureSink,
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 @@ class RandomExtractWithinSubsets(QgisAlgorithm):
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

View File

@ -27,13 +27,13 @@ __revision__ = '$Format:%H$'
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 @@ class SelectByAttribute(QgisAlgorithm):
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 @@ class SelectByAttribute(QgisAlgorithm):
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]}

View File

@ -26,11 +26,11 @@ __revision__ = '$Format:%H$'
from qgis.core import (QgsExpression,
QgsVectorLayer,
QgsProcessingException,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterExpression,
QgsProcessingParameterEnum,
QgsProcessingOutputVectorLayer)
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
@ -85,7 +85,7 @@ class SelectByExpression(QgisAlgorithm):
expression = self.parameterAsString(parameters, self.EXPRESSION, context)
qExp = QgsExpression(expression)
if qExp.hasParserError():
raise GeoAlgorithmExecutionException(qExp.parserErrorString())
raise QgsProcessingException(qExp.parserErrorString())
layer.selectByExpression(expression, behavior)
return {self.OUTPUT: parameters[self.INPUT]}

View File

@ -27,12 +27,12 @@ __revision__ = '$Format:%H$'
from qgis.core import (QgsFeatureSink,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterNumber)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
class Smooth(QgisAlgorithm):
@ -89,7 +89,7 @@ class Smooth(QgisAlgorithm):
if input_feature.geometry():
output_geometry = input_feature.geometry().smooth(iterations, offset, -1, max_angle)
if not output_geometry:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Error smoothing geometry'))
output_feature.setGeometry(output_geometry)

View File

@ -27,11 +27,11 @@ __copyright__ = '(C) 2016, Mathieu Pellerin'
__revision__ = '$Format:%H$'
from qgis.core import (QgsDataSourceUri,
QgsProcessingException,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterString)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.tools import spatialite
@ -69,7 +69,7 @@ class SpatialiteExecuteSQL(QgisAlgorithm):
try:
db._exec_sql_and_commit(str(sql))
except spatialite.DbError as e:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Error executing SQL:\n{0}').format(str(e)))
return {}

View File

@ -37,12 +37,12 @@ from qgis.core import (QgsFeatureRequest,
QgsPointXY,
QgsWkbTypes,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterNumber)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from . import voronoi
@ -109,7 +109,7 @@ class VoronoiPolygons(QgisAlgorithm):
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.'))
@ -122,7 +122,7 @@ class VoronoiPolygons(QgisAlgorithm):
current = 0
if len(c.polygons) == 0:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('There were no polygons created.'))
total = 100.0 / len(c.polygons)