Skip to content

Commit

Permalink
[processing] load algorithms only when requred 3rd party modules are
Browse files Browse the repository at this point in the history
available or compatible QGIS version is used

Some housekeeping in QGIS algorithms provider
  • Loading branch information
alexbruy committed Feb 17, 2016
1 parent b4b382b commit e7fe61e
Show file tree
Hide file tree
Showing 84 changed files with 515 additions and 669 deletions.
15 changes: 6 additions & 9 deletions python/plugins/processing/algs/qgis/AddTableField.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,13 @@ def processAlgorithm(self, progress):
writer = output.getVectorWriter(fields, provider.geometryType(),
layer.crs())
outFeat = QgsFeature()
inGeom = QgsGeometry()
nElement = 0
features = vector.features(layer)
nFeat = len(features)
for inFeat in features:
progress.setPercentage(int(100 * nElement / nFeat))
nElement += 1
inGeom = inFeat.geometry()
outFeat.setGeometry(inGeom)
atMap = inFeat.attributes()
total = 100.0 / len(features)
for current, feat in enumerate(features):
progress.setPercentage(int(current * total))
geom = feat.geometry()
outFeat.setGeometry(geom)
atMap = feat.attributes()
atMap.append(None)
outFeat.setAttributes(atMap)
writer.addFeature(outFeat)
Expand Down
32 changes: 14 additions & 18 deletions python/plugins/processing/algs/qgis/AutoincrementalField.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class AutoincrementalField(GeoAlgorithm):
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Add autoincremental field')
self.group, self.i18n_group = self.trAlgorithm('Vector table tools')
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY]))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Incremented')))

def processAlgorithm(self, progress):
output = self.getOutputFromName(self.OUTPUT)
vlayer = \
Expand All @@ -47,26 +54,15 @@ def processAlgorithm(self, progress):
fields.append(QgsField('AUTO', QVariant.Int))
writer = output.getVectorWriter(fields, vprovider.geometryType(),
vlayer.crs())
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
nElement = 0
features = vector.features(vlayer)
nFeat = len(features)
for inFeat in features:
progress.setPercentage(int(100 * nElement / nFeat))
nElement += 1
inGeom = inFeat.geometry()
outFeat.setGeometry(inGeom)
attrs = inFeat.attributes()
attrs.append(nElement)
total = 100.0 / len(features)
for current, feat in enumerate(features):
progress.setPercentage(int(current * total))
geom = feat.geometry()
outFeat.setGeometry(geom)
attrs = feat.attributes()
attrs.append(count)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
del writer

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Add autoincremental field')
self.group, self.i18n_group = self.trAlgorithm('Vector table tools')
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY]))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Incremented')))
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,10 @@ def processAlgorithm(self, progress):
features = vector.features(layer)
count = len(features)
total = 100.0 / float(count)
current = 0
for ft in features:
for current, ft in enumerate(features):
if ft.attributes()[index]:
values.append(float(ft.attributes()[index]))

current += 1
progress.setPercentage(int(current * total))

stat = QgsStatisticalSummary()
Expand Down
6 changes: 2 additions & 4 deletions python/plugins/processing/algs/qgis/BasicStatisticsStrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ def processAlgorithm(self, progress):

features = vector.features(layer)
count = len(features)
total = 100.0 / float(count)
current = 0
for ft in features:
total = 100.0 / count
for current, ft in enumerate(features):
length = float(len(ft.attributes()[index]))

if isFirst:
Expand All @@ -115,7 +114,6 @@ def processAlgorithm(self, progress):
values.append(length)
sumValue += length

current += 1
progress.setPercentage(int(current * total))

n = float(len(values))
Expand Down
11 changes: 4 additions & 7 deletions python/plugins/processing/algs/qgis/Centroids.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ def processAlgorithm(self, progress):
outFeat = QgsFeature()

features = vector.features(layer)
total = 100.0 / float(len(features))
current = 0

for inFeat in features:
inGeom = inFeat.geometry()
attrs = inFeat.attributes()
total = 100.0 / len(features)
for current, feat in enumerate(features):
inGeom = feat.geometry()
attrs = feat.attributes()

if not inGeom:
outGeom = QgsGeometry(None)
Expand All @@ -78,7 +76,6 @@ def processAlgorithm(self, progress):
outFeat.setGeometry(outGeom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
current += 1
progress.setPercentage(int(current * total))

del writer
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/CheckValidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def doCheck(self, progress):
error_count = 0

features = vector.features(layer)
count = len(features)
total = 100.0 / len(features)
for current, inFeat in enumerate(features):
geom = QgsGeometry(inFeat.geometry())
attrs = inFeat.attributes()
Expand Down Expand Up @@ -166,7 +166,7 @@ def doCheck(self, progress):
invalid_writer.addFeature(outFeat)
invalid_count += 1

progress.setPercentage(100 * current / float(count))
progress.setPercentage(int(current * total))

del valid_writer
del invalid_writer
Expand Down
6 changes: 2 additions & 4 deletions python/plugins/processing/algs/qgis/Clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ def processAlgorithm(self, progress):

selectionA = vector.features(layerA)

current = 0
total = 100.0 / float(len(selectionA))
total = 100.0 / len(selectionA)

for inFeatA in selectionA:
for current, inFeatA in enumerate(selectionA):
geom = QgsGeometry(inFeatA.geometry())
attrs = inFeatA.attributes()
intersects = index.intersects(geom.boundingBox())
Expand Down Expand Up @@ -132,7 +131,6 @@ def processAlgorithm(self, progress):
'invalid geometry.'))
continue

current += 1
progress.setPercentage(int(current * total))

del writer
27 changes: 16 additions & 11 deletions python/plugins/processing/algs/qgis/ConcaveHull.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ def defineCharacteristics(self):
self.addOutput(OutputVector(ConcaveHull.OUTPUT, self.tr('Concave hull')))

def processAlgorithm(self, progress):
#get parameters
layer = dataobjects.getObjectFromUri(self.getParameterValue(ConcaveHull.INPUT))
alpha = self.getParameterValue(self.ALPHA)
holes = self.getParameterValue(self.HOLES)
no_multigeom = self.getParameterValue(self.NO_MULTIGEOMETRY)
#Delaunay triangulation from input point layer

# Delaunay triangulation from input point layer
progress.setText(self.tr('Creating Delaunay triangles...'))
delone_triangles = processing.runalg("qgis:delaunaytriangulation", layer, None, progress=None)['OUTPUT']
delaunay_layer = processing.getObject(delone_triangles)
#get max edge length from Delaunay triangles

# Get max edge length from Delaunay triangles
progress.setText(self.tr('Computing edges max length...'))
features = delaunay_layer.getFeatures()
counter = 50. / delaunay_layer.featureCount()
Expand All @@ -81,7 +82,8 @@ def processAlgorithm(self, progress):
edges[feat.id()] = max(lengths[-3:])
progress.setPercentage(feat.id() * counter)
max_length = max(lengths)
#get features with longest edge longer than alpha*max_length

# Get features with longest edge longer than alpha*max_length
progress.setText(self.tr('Removing features...'))
counter = 50. / len(edges)
i = 0
Expand All @@ -91,40 +93,43 @@ def processAlgorithm(self, progress):
ids.append(id)
progress.setPercentage(50 + i * counter)
i += 1
#remove features

# Remove features
delaunay_layer.setSelectedFeatures(ids)
delaunay_layer.startEditing()
delaunay_layer.deleteSelectedFeatures()
delaunay_layer.commitChanges()
#dissolve all Delaunay triangles

# Dissolve all Delaunay triangles
progress.setText(self.tr('Dissolving Delaunay triangles...'))
dissolved = processing.runalg("qgis:dissolve", delaunay_layer,
True, None, None, progress=None)['OUTPUT']
dissolved_layer = processing.getObject(dissolved)
#save result

# Save result
progress.setText(self.tr('Saving data...'))
feat = QgsFeature()
dissolved_layer.getFeatures(QgsFeatureRequest().setFilterFid(0)).nextFeature(feat)
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
layer.pendingFields().toList(), QGis.WKBPolygon, layer.crs())
geom = feat.geometry()
if no_multigeom and geom.isMultipart():
#only singlepart geometries are allowed
# Only singlepart geometries are allowed
geom_list = geom.asMultiPolygon()
for single_geom_list in geom_list:
single_feature = QgsFeature()
single_geom = QgsGeometry.fromPolygon(single_geom_list)
if not holes:
#delete holes
# Delete holes
deleted = True
while deleted:
deleted = single_geom.deleteRing(1)
single_feature.setGeometry(single_geom)
writer.addFeature(single_feature)
else:
#multipart geometries are allowed
# Multipart geometries are allowed
if not holes:
#delete holes
# Delete holes
deleted = True
while deleted:
deleted = geom.deleteRing(1)
Expand Down
8 changes: 3 additions & 5 deletions python/plugins/processing/algs/qgis/ConvexHull.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,12 @@ def processAlgorithm(self, progress):
inGeom = QgsGeometry()
outGeom = QgsGeometry()

current = 0

fid = 0
val = None
features = vector.features(layer)
if useField:
unique = layer.uniqueValues(index)
current = 0
total = 100.0 / (len(features) * len(unique))
for i in unique:
first = True
Expand Down Expand Up @@ -133,13 +132,12 @@ def processAlgorithm(self, progress):
fid += 1
else:
hull = []
total = 100.0 / float(layer.featureCount())
total = 100.0 / layer.featureCount()
features = vector.features(layer)
for f in features:
for current, f in enumerate(features):
inGeom = QgsGeometry(f.geometry())
points = vector.extractPoints(inGeom)
hull.extend(points)
current += 1
progress.setPercentage(int(current * total))

tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
Expand Down

0 comments on commit e7fe61e

Please sign in to comment.