Skip to content
Permalink
Browse files

Merge pull request #5177 from alexbruy/fix-zero-division

[processing] prevent float division by zero in QGIS algorithms
  • Loading branch information
alexbruy committed Sep 12, 2017
2 parents 592151f + 8c46e4b commit 70cd0e4d45970b67a101517ab21aabc189412137
Showing with 100 additions and 100 deletions.
  1. +1 −1 python/plugins/processing/algs/qgis/AddTableField.py
  2. +1 −1 python/plugins/processing/algs/qgis/AutoincrementalField.py
  3. +1 −1 python/plugins/processing/algs/qgis/BasicStatisticsNumbers.py
  4. +1 −1 python/plugins/processing/algs/qgis/BasicStatisticsStrings.py
  5. +1 −1 python/plugins/processing/algs/qgis/Boundary.py
  6. +1 −1 python/plugins/processing/algs/qgis/BoundingBox.py
  7. +1 −1 python/plugins/processing/algs/qgis/Buffer.py
  8. +1 −1 python/plugins/processing/algs/qgis/Centroids.py
  9. +1 −1 python/plugins/processing/algs/qgis/CheckValidity.py
  10. +1 −1 python/plugins/processing/algs/qgis/Clip.py
  11. +2 −2 python/plugins/processing/algs/qgis/ConcaveHull.py
  12. +2 −2 python/plugins/processing/algs/qgis/ConvexHull.py
  13. +1 −1 python/plugins/processing/algs/qgis/Datasources2Vrt.py
  14. +2 −2 python/plugins/processing/algs/qgis/Delaunay.py
  15. +1 −1 python/plugins/processing/algs/qgis/DeleteColumn.py
  16. +2 −2 python/plugins/processing/algs/qgis/DeleteDuplicateGeometries.py
  17. +1 −1 python/plugins/processing/algs/qgis/DeleteHoles.py
  18. +1 −1 python/plugins/processing/algs/qgis/DensifyGeometries.py
  19. +1 −1 python/plugins/processing/algs/qgis/DensifyGeometriesInterval.py
  20. +1 −1 python/plugins/processing/algs/qgis/Difference.py
  21. +2 −2 python/plugins/processing/algs/qgis/Dissolve.py
  22. +1 −1 python/plugins/processing/algs/qgis/EquivalentNumField.py
  23. +1 −1 python/plugins/processing/algs/qgis/ExecuteSQL.py
  24. +1 −1 python/plugins/processing/algs/qgis/Explode.py
  25. +1 −1 python/plugins/processing/algs/qgis/ExportGeometryInfo.py
  26. +1 −1 python/plugins/processing/algs/qgis/ExtentFromLayer.py
  27. +1 −1 python/plugins/processing/algs/qgis/ExtractByLocation.py
  28. +1 −1 python/plugins/processing/algs/qgis/ExtractNodes.py
  29. +1 −1 python/plugins/processing/algs/qgis/FieldPyculator.py
  30. +1 −1 python/plugins/processing/algs/qgis/FieldsCalculator.py
  31. +1 −1 python/plugins/processing/algs/qgis/FieldsMapper.py
  32. +1 −1 python/plugins/processing/algs/qgis/GeometryConvert.py
  33. +1 −1 python/plugins/processing/algs/qgis/Gridify.py
  34. +1 −1 python/plugins/processing/algs/qgis/HubDistance.py
  35. +1 −1 python/plugins/processing/algs/qgis/HubLines.py
  36. +1 −1 python/plugins/processing/algs/qgis/HypsometricCurves.py
  37. +1 −1 python/plugins/processing/algs/qgis/Intersection.py
  38. +2 −2 python/plugins/processing/algs/qgis/JoinAttributes.py
  39. +1 −1 python/plugins/processing/algs/qgis/LinesIntersection.py
  40. +1 −1 python/plugins/processing/algs/qgis/LinesToPolygons.py
  41. +2 −2 python/plugins/processing/algs/qgis/MeanCoords.py
  42. +1 −1 python/plugins/processing/algs/qgis/Merge.py
  43. +1 −1 python/plugins/processing/algs/qgis/MergeLines.py
  44. +1 −1 python/plugins/processing/algs/qgis/MultipartToSingleparts.py
  45. +1 −1 python/plugins/processing/algs/qgis/NearestNeighbourAnalysis.py
  46. +2 −2 python/plugins/processing/algs/qgis/OrientedMinimumBoundingBox.py
  47. +2 −2 python/plugins/processing/algs/qgis/PointDistance.py
  48. +1 −1 python/plugins/processing/algs/qgis/PointOnSurface.py
  49. +2 −2 python/plugins/processing/algs/qgis/PointsDisplacement.py
  50. +1 −1 python/plugins/processing/algs/qgis/PointsFromLines.py
  51. +1 −1 python/plugins/processing/algs/qgis/PointsFromPolygons.py
  52. +1 −1 python/plugins/processing/algs/qgis/PointsInPolygon.py
  53. +1 −1 python/plugins/processing/algs/qgis/PointsInPolygonUnique.py
  54. +1 −1 python/plugins/processing/algs/qgis/PointsInPolygonWeighted.py
  55. +1 −1 python/plugins/processing/algs/qgis/PointsLayerFromTable.py
  56. +2 −2 python/plugins/processing/algs/qgis/PointsToPaths.py
  57. +2 −2 python/plugins/processing/algs/qgis/Polygonize.py
  58. +1 −1 python/plugins/processing/algs/qgis/PolygonsToLines.py
  59. +1 −1 python/plugins/processing/algs/qgis/RandomExtract.py
  60. +2 −2 python/plugins/processing/algs/qgis/RandomExtractWithinSubsets.py
  61. +1 −1 python/plugins/processing/algs/qgis/RandomPointsAlongLines.py
  62. +1 −1 python/plugins/processing/algs/qgis/RandomPointsExtent.py
  63. +1 −1 python/plugins/processing/algs/qgis/RandomPointsLayer.py
  64. +1 −1 python/plugins/processing/algs/qgis/RandomPointsPolygonsFixed.py
  65. +1 −1 python/plugins/processing/algs/qgis/RandomPointsPolygonsVariable.py
  66. +1 −1 python/plugins/processing/algs/qgis/RandomSelectionWithinSubsets.py
  67. +1 −1 python/plugins/processing/algs/qgis/RectanglesOvalsDiamondsFixed.py
  68. +1 −1 python/plugins/processing/algs/qgis/RectanglesOvalsDiamondsVariable.py
  69. +1 −1 python/plugins/processing/algs/qgis/RemoveNullGeometry.py
  70. +1 −1 python/plugins/processing/algs/qgis/ReprojectLayer.py
  71. +1 −1 python/plugins/processing/algs/qgis/ReverseLineDirection.py
  72. +1 −1 python/plugins/processing/algs/qgis/SaveSelectedFeatures.py
  73. +1 −1 python/plugins/processing/algs/qgis/SelectByLocation.py
  74. +1 −1 python/plugins/processing/algs/qgis/SimplifyGeometries.py
  75. +1 −1 python/plugins/processing/algs/qgis/SinglePartsToMultiparts.py
  76. +1 −1 python/plugins/processing/algs/qgis/Smooth.py
  77. +1 −1 python/plugins/processing/algs/qgis/SpatialJoin.py
  78. +1 −1 python/plugins/processing/algs/qgis/SplitLinesWithLines.py
  79. +1 −1 python/plugins/processing/algs/qgis/StatisticsByCategories.py
  80. +1 −1 python/plugins/processing/algs/qgis/SumLines.py
  81. +1 −1 python/plugins/processing/algs/qgis/SymmetricalDifference.py
  82. +1 −1 python/plugins/processing/algs/qgis/TextToFloat.py
  83. +2 −2 python/plugins/processing/algs/qgis/Union.py
  84. +1 −1 python/plugins/processing/algs/qgis/VectorSplit.py
  85. +1 −1 python/plugins/processing/algs/qgis/VoronoiPolygons.py
  86. +1 −1 python/plugins/processing/algs/qgis/ZonalStatistics.py
@@ -85,7 +85,7 @@ def processAlgorithm(self, progress):
layer.crs())
outFeat = QgsFeature()
features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, feat in enumerate(features):
progress.setPercentage(int(current * total))
geom = feat.geometry()
@@ -55,7 +55,7 @@ def processAlgorithm(self, progress):
vlayer.crs())
outFeat = QgsFeature()
features = vector.features(vlayer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, feat in enumerate(features):
progress.setPercentage(int(current * total))
geom = feat.geometry()
@@ -124,7 +124,7 @@ def processAlgorithm(self, progress):

features = vector.features(layer)
count = len(features)
total = 100.0 / float(count)
total = 100.0 / count if count > 0 else 1
for current, ft in enumerate(features):
value = ft[fieldName]
if value or value == 0:
@@ -100,7 +100,7 @@ def processAlgorithm(self, progress):

features = vector.features(layer)
count = len(features)
total = 100.0 / count
total = 100.0 / count if count > 0 else 1
for current, ft in enumerate(features):
value = ft[fieldName]
if value:
@@ -73,7 +73,7 @@ def processAlgorithm(self, progress):
layer.crs())

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1

for current, input_feature in enumerate(features):
output_feature = input_feature
@@ -67,7 +67,7 @@ def processAlgorithm(self, progress):
layer.crs())

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1

for current, input_feature in enumerate(features):
output_feature = input_feature
@@ -44,7 +44,7 @@ def buffering(progress, writer, distance, field, useField, layer, dissolve,

current = 0
features = vector.features(layer)
total = 100.0 / float(len(features))
total = 100.0 / len(features) if len(features) > 0 else 1

# With dissolve
if dissolve:
@@ -70,7 +70,7 @@ def processAlgorithm(self, progress):
outFeat = QgsFeature()

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, feat in enumerate(features):
inGeom = feat.geometry()
attrs = feat.attributes()
@@ -132,7 +132,7 @@ def doCheck(self, progress):
error_count = 0

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, inFeat in enumerate(features):
geom = QgsGeometry(inFeat.geometry())
attrs = inFeat.attributes()
@@ -96,7 +96,7 @@ def processAlgorithm(self, progress):
continue

if single_clip_feature:
total = 100.0 / len(input_features)
total = 100.0 / len(features) if len(features) > 0 else 1
else:
total = 0

@@ -77,7 +77,7 @@ def processAlgorithm(self, progress):
# Get max edge length from Delaunay triangles
progress.setText(self.tr('Computing edges max length...'))
features = delaunay_layer.getFeatures()
counter = 50. / delaunay_layer.featureCount()
counter = 50. / delaunay_layer.featureCount() if delaunay_layer.featureCount() > 0 else 1
lengths = []
edges = {}
for feat in features:
@@ -90,7 +90,7 @@ def processAlgorithm(self, progress):

# Get features with longest edge longer than alpha*max_length
progress.setText(self.tr('Removing features...'))
counter = 50. / len(edges)
counter = 50. / len(edges) if len(edges) > 0 else 1
i = 0
ids = []
for id, max_len in edges.iteritems():
@@ -109,7 +109,7 @@ def processAlgorithm(self, progress):
if useField:
unique = layer.uniqueValues(index)
current = 0
total = 100.0 / (len(features) * len(unique))
total = 100.0 / (len(features) * len(unique)) if len(features) * len(unique) > 0 else 1
for i in unique:
first = True
hull = []
@@ -141,7 +141,7 @@ def processAlgorithm(self, progress):
fid += 1
else:
hull = []
total = 100.0 / layer.featureCount()
total = 100.0 / layer.featureCount() if layer.featureCount() > 0 else 1
features = vector.features(layer)
for current, f in enumerate(features):
inGeom = QgsGeometry(f.geometry())
@@ -93,7 +93,7 @@ def mergeDataSources2Vrt(self, dataSources, outFile, union=False, relative=False
if union:
vrt += '<OGRVRTUnionLayer name="UnionedLayer">'

total = 100.0 / len(dataSources)
total = 100.0 / len(dataSources) if len(dataSources) > 0 else 1
for current, inFile in enumerate(dataSources):
progress.setPercentage(int(current * total))

@@ -76,7 +76,7 @@ def processAlgorithm(self, progress):
ptNdx = -1
c = voronoi.Context()
features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, inFeat in enumerate(features):
geom = QgsGeometry(inFeat.geometry())
point = geom.asPoint()
@@ -100,7 +100,7 @@ def processAlgorithm(self, progress):
triangles = c.triangles
feat = QgsFeature()

total = 100.0 / len(triangles)
total = 100.0 / len(triangles) if len(triangles) > 0 else 1
for current, triangle in enumerate(triangles):
indicies = list(triangle)
indicies.append(indicies[0])
@@ -61,7 +61,7 @@ def processAlgorithm(self, progress):
layer.wkbType(), layer.crs())

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1

feat = QgsFeature()
for current, f in enumerate(features):
@@ -56,7 +56,7 @@ def processAlgorithm(self, progress):

features = vector.features(layer)

total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
geoms = dict()
for current, f in enumerate(features):
geoms[f.id()] = QgsGeometry(f.geometry())
@@ -71,7 +71,7 @@ def processAlgorithm(self, progress):
if g.isGeosEqual(cleaned[j]):
del cleaned[j]

total = 100.0 / len(cleaned)
total = 100.0 / len(cleaned) if len(cleaned) > 0 else 1
request = QgsFeatureRequest().setFilterFids(cleaned.keys())
for current, f in enumerate(layer.getFeatures(request)):
writer.addFeature(f)
@@ -54,7 +54,7 @@ def processAlgorithm(self, progress):
layer.crs())

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1

feat = QgsFeature()
for current, f in enumerate(features):
@@ -69,7 +69,7 @@ def processAlgorithm(self, progress):
layer.wkbType(), layer.crs())

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, f in enumerate(features):
featGeometry = QgsGeometry(f.geometry())
attrs = f.attributes()
@@ -66,7 +66,7 @@ def processAlgorithm(self, progress):
layer.wkbType(), layer.crs())

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, f in enumerate(features):
featGeometry = QgsGeometry(f.geometry())
attrs = f.attributes()
@@ -77,7 +77,7 @@ def processAlgorithm(self, progress):
outFeat = QgsFeature()
index = vector.spatialindex(layerB)
selectionA = vector.features(layerA)
total = 100.0 / len(selectionA)
total = 100.0 / len(selectionA) if len(selectionA) > 0 else 1
for current, inFeatA in enumerate(selectionA):
add = True
geom = QgsGeometry(inFeatA.geometry())
@@ -78,7 +78,7 @@ def processAlgorithm(self, progress):
vlayerA.crs())
outFeat = QgsFeature()
features = vector.features(vlayerA)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1

if not useField:
first = True
@@ -154,7 +154,7 @@ def processAlgorithm(self, progress):

geometry_dict[index_attrs].append(tmpInGeom)

nFeat = len(attribute_dict)
nFeat = len(attribute_dict) if len(attribute_dict) > 0 else 1

nElement = 0
for key, value in geometry_dict.items():
@@ -63,7 +63,7 @@ def processAlgorithm(self, progress):
classes = {}

features = vector.features(vlayer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, feature in enumerate(features):
progress.setPercentage(int(current * total))
inGeom = feature.geometry()
@@ -137,7 +137,7 @@ def processAlgorithm(self, progress):
vLayer.crs())

features = vector.features(vLayer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
outFeat = QgsFeature()
for current, inFeat in enumerate(features):
outFeat.setAttributes(inFeat.attributes())
@@ -53,7 +53,7 @@ def processAlgorithm(self, progress):
vlayer.crs())
outFeat = QgsFeature()
features = vector.features(vlayer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, feature in enumerate(features):
progress.setPercentage(int(current * total))
inGeom = feature.geometry()
@@ -116,7 +116,7 @@ def processAlgorithm(self, progress):
outFeat.setFields(fields)

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, f in enumerate(features):
inGeom = f.geometry()

@@ -125,7 +125,7 @@ def layerExtent(self, layer, writer, progress):

def featureExtent(self, layer, writer, progress):
features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
feat = QgsFeature()
for current, f in enumerate(features):
rect = f.geometry().boundingBox()
@@ -80,7 +80,7 @@ def processAlgorithm(self, progress):

selectedSet = []
features = vector.features(selectLayer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, f in enumerate(features):
geom = vector.snapToPrecision(f.geometry(), precision)
bbox = vector.bufferedBoundingBox(geom.boundingBox(), 0.51 * precision)
@@ -69,7 +69,7 @@ def processAlgorithm(self, progress):
outGeom = QgsGeometry()

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, f in enumerate(features):
inGeom = f.geometry()
attrs = f.attributes()
@@ -130,7 +130,7 @@ def processAlgorithm(self, progress):

# Run
features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, feat in enumerate(features):
progress.setPercentage(int(current * total))
attrs = feat.attributes()
@@ -130,7 +130,7 @@ def processAlgorithm(self, progress):
calculationSuccess = True

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1

rownum = 1
for current, f in enumerate(features):
@@ -118,7 +118,7 @@ def processAlgorithm(self, progress):
outFeat = QgsFeature()
features = vector.features(layer)
if len(features):
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, inFeat in enumerate(features):
rownum = current + 1

@@ -81,7 +81,7 @@ def processAlgorithm(self, progress):
layer.pendingFields(), newType, layer.crs())

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1

for current, f in enumerate(features):
geom = f.geometry()
@@ -68,7 +68,7 @@ def processAlgorithm(self, progress):
layer.pendingFields(), layer.wkbType(), layer.crs())

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1

for current, f in enumerate(features):
geom = f.geometry()
@@ -117,7 +117,7 @@ def processAlgorithm(self, progress):

# Scan source points, find nearest hub, and write to output file
features = vector.features(layerPoints)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, f in enumerate(features):
src = f.geometry().boundingBox().center()

@@ -75,7 +75,7 @@ def processAlgorithm(self, progress):

spokes = vector.features(layerSpoke)
hubs = vector.features(layerHub)
total = 100.0 / len(spokes)
total = 100.0 / len(spokes) if len(spokes) > 0 else 1

for current, spokepoint in enumerate(spokes):
p = spokepoint.geometry().boundingBox().center()
@@ -100,7 +100,7 @@ def processAlgorithm(self, progress):
memRasterDriver = gdal.GetDriverByName('MEM')

features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1

for current, f in enumerate(features):
geom = f.geometry()
@@ -86,7 +86,7 @@ def processAlgorithm(self, progress):
outFeat = QgsFeature()
index = vector.spatialindex(vlayerB)
selectionA = vector.features(vlayerA)
total = 100.0 / len(selectionA)
total = 100.0 / len(selectionA) if len(selectionA) > 0 else 1
for current, inFeatA in enumerate(selectionA):
progress.setPercentage(int(current * total))
geom = inFeatA.geometry()
@@ -81,7 +81,7 @@ def processAlgorithm(self, progress):
# Cache attributes of Layer 2
cache = {}
features = vector.features(layer2)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, feat in enumerate(features):
attrs = feat.attributes()
joinValue2 = unicode(attrs[joinField2Index])
@@ -92,7 +92,7 @@ def processAlgorithm(self, progress):
# Create output vector layer with additional attribute
outFeat = QgsFeature()
features = vector.features(layer)
total = 100.0 / len(features)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, feat in enumerate(features):
outFeat.setGeometry(feat.geometry())
attrs = feat.attributes()

0 comments on commit 70cd0e4

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