Skip to content

Commit

Permalink
[processing] prevent float division by zero in QGIS algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Sep 12, 2017
1 parent 88dc615 commit 8c46e4b
Show file tree
Hide file tree
Showing 86 changed files with 100 additions and 100 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/AddTableField.py
Expand Up @@ -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()
Expand Down
Expand Up @@ -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()
Expand Down
Expand Up @@ -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:
Expand Down
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Boundary.py
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/BoundingBox.py
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Buffer.py
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Centroids.py
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/CheckValidity.py
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Clip.py
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/ConcaveHull.py
Expand Up @@ -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:
Expand All @@ -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():
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/ConvexHull.py
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Datasources2Vrt.py
Expand Up @@ -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))

Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/Delaunay.py
Expand Up @@ -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()
Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/DeleteColumn.py
Expand Up @@ -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):
Expand Down
Expand Up @@ -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())
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/DeleteHoles.py
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/DensifyGeometries.py
Expand Up @@ -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()
Expand Down
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Difference.py
Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/Dissolve.py
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/EquivalentNumField.py
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ExecuteSQL.py
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Explode.py
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ExportGeometryInfo.py
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ExtentFromLayer.py
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ExtractByLocation.py
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ExtractNodes.py
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/FieldPyculator.py
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/FieldsCalculator.py
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/FieldsMapper.py
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/GeometryConvert.py
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Gridify.py
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/HubDistance.py
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/HubLines.py
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/HypsometricCurves.py
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Intersection.py
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/JoinAttributes.py
Expand Up @@ -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])
Expand All @@ -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()
Expand Down

0 comments on commit 8c46e4b

Please sign in to comment.