Skip to content
Permalink
Browse files

[processing] load algorithms only when requred 3rd party modules are

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 e7fe61e6b91414fab8eec8db98e1a794cbb88ec3
Showing with 515 additions and 669 deletions.
  1. +6 −9 python/plugins/processing/algs/qgis/AddTableField.py
  2. +14 −18 python/plugins/processing/algs/qgis/AutoincrementalField.py
  3. +1 −3 python/plugins/processing/algs/qgis/BasicStatisticsNumbers.py
  4. +2 −4 python/plugins/processing/algs/qgis/BasicStatisticsStrings.py
  5. +4 −7 python/plugins/processing/algs/qgis/Centroids.py
  6. +2 −2 python/plugins/processing/algs/qgis/CheckValidity.py
  7. +2 −4 python/plugins/processing/algs/qgis/Clip.py
  8. +16 −11 python/plugins/processing/algs/qgis/ConcaveHull.py
  9. +3 −5 python/plugins/processing/algs/qgis/ConvexHull.py
  10. +129 −163 python/plugins/processing/algs/qgis/Datasources2Vrt.py
  11. +5 −6 python/plugins/processing/algs/qgis/Delaunay.py
  12. +4 −4 python/plugins/processing/algs/qgis/DeleteColumn.py
  13. +7 −8 python/plugins/processing/algs/qgis/DeleteDuplicateGeometries.py
  14. +4 −5 python/plugins/processing/algs/qgis/DeleteHoles.py
  15. +2 −4 python/plugins/processing/algs/qgis/DensifyGeometries.py
  16. +2 −4 python/plugins/processing/algs/qgis/DensifyGeometriesInterval.py
  17. +2 −10 python/plugins/processing/algs/qgis/Difference.py
  18. +15 −17 python/plugins/processing/algs/qgis/Dissolve.py
  19. +16 −15 python/plugins/processing/algs/qgis/EquivalentNumField.py
  20. +14 −4 python/plugins/processing/algs/qgis/ExecuteSQL.py
  21. +10 −13 python/plugins/processing/algs/qgis/Explode.py
  22. +2 −4 python/plugins/processing/algs/qgis/ExportGeometryInfo.py
  23. +2 −4 python/plugins/processing/algs/qgis/ExtentFromLayer.py
  24. +0 −6 python/plugins/processing/algs/qgis/ExtractByAttribute.py
  25. +5 −6 python/plugins/processing/algs/qgis/ExtractByLocation.py
  26. +2 −4 python/plugins/processing/algs/qgis/ExtractNodes.py
  27. +3 −5 python/plugins/processing/algs/qgis/FieldPyculator.py
  28. +0 −1 python/plugins/processing/algs/qgis/FieldsCalculator.py
  29. +8 −9 python/plugins/processing/algs/qgis/FieldsMapper.py
  30. +3 −4 python/plugins/processing/algs/qgis/GeometryConvert.py
  31. +3 −5 python/plugins/processing/algs/qgis/Gridify.py
  32. +3 −4 python/plugins/processing/algs/qgis/HubDistance.py
  33. +3 −5 python/plugins/processing/algs/qgis/HubLines.py
  34. +4 −5 python/plugins/processing/algs/qgis/HypsometricCurves.py
  35. +27 −27 python/plugins/processing/algs/qgis/ImportIntoPostGIS.py
  36. +3 −7 python/plugins/processing/algs/qgis/Intersection.py
  37. +13 −20 python/plugins/processing/algs/qgis/JoinAttributes.py
  38. +2 −10 python/plugins/processing/algs/qgis/LinesIntersection.py
  39. +2 −5 python/plugins/processing/algs/qgis/LinesToPolygons.py
  40. +7 −6 python/plugins/processing/algs/qgis/MeanCoords.py
  41. +1 −1 python/plugins/processing/algs/qgis/Merge.py
  42. +2 −4 python/plugins/processing/algs/qgis/MultipartToSingleparts.py
  43. +2 −4 python/plugins/processing/algs/qgis/NearestNeighbourAnalysis.py
  44. +4 −8 python/plugins/processing/algs/qgis/OrientedMinimumBoundingBox.py
  45. +4 −15 python/plugins/processing/algs/qgis/PointDistance.py
  46. +1 −3 python/plugins/processing/algs/qgis/PointsDisplacement.py
  47. +1 −3 python/plugins/processing/algs/qgis/PointsFromLines.py
  48. +1 −3 python/plugins/processing/algs/qgis/PointsFromPolygons.py
  49. +2 −5 python/plugins/processing/algs/qgis/PointsInPolygon.py
  50. +3 −6 python/plugins/processing/algs/qgis/PointsInPolygonUnique.py
  51. +2 −4 python/plugins/processing/algs/qgis/PointsInPolygonWeighted.py
  52. +16 −18 python/plugins/processing/algs/qgis/PointsLayerFromTable.py
  53. +5 −5 python/plugins/processing/algs/qgis/PointsToPaths.py
  54. +24 −31 python/plugins/processing/algs/qgis/Polygonize.py
  55. +2 −4 python/plugins/processing/algs/qgis/PolygonsToLines.py
  56. +6 −6 python/plugins/processing/algs/qgis/PostGISExecuteSQL.py
  57. +18 −4 python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
  58. +3 −2 python/plugins/processing/algs/qgis/RandomExtract.py
  59. +3 −2 python/plugins/processing/algs/qgis/RandomExtractWithinSubsets.py
  60. +3 −1 python/plugins/processing/algs/qgis/RandomPointsAlongLines.py
  61. +2 −1 python/plugins/processing/algs/qgis/RandomPointsExtent.py
  62. +2 −1 python/plugins/processing/algs/qgis/RandomPointsLayer.py
  63. +2 −1 python/plugins/processing/algs/qgis/RandomPointsPolygonsFixed.py
  64. +2 −1 python/plugins/processing/algs/qgis/RandomPointsPolygonsVariable.py
  65. +1 −1 python/plugins/processing/algs/qgis/RandomSelection.py
  66. +1 −1 python/plugins/processing/algs/qgis/RandomSelectionWithinSubsets.py
  67. +15 −13 python/plugins/processing/algs/qgis/RasterLayerStatistics.py
  68. +3 −2 python/plugins/processing/algs/qgis/RegularPoints.py
  69. +2 −4 python/plugins/processing/algs/qgis/ReprojectLayer.py
  70. +2 −5 python/plugins/processing/algs/qgis/ReverseLineDirection.py
  71. +3 −3 python/plugins/processing/algs/qgis/SaveSelectedFeatures.py
  72. +0 −6 python/plugins/processing/algs/qgis/SelectByAttribute.py
  73. +1 −4 python/plugins/processing/algs/qgis/SelectByExpression.py
  74. +2 −4 python/plugins/processing/algs/qgis/SelectByLocation.py
  75. +3 −5 python/plugins/processing/algs/qgis/SimplifyGeometries.py
  76. +1 −2 python/plugins/processing/algs/qgis/SinglePartsToMultiparts.py
  77. +2 −4 python/plugins/processing/algs/qgis/Smooth.py
  78. +1 −8 python/plugins/processing/algs/qgis/SplitLinesWithLines.py
  79. +3 −5 python/plugins/processing/algs/qgis/StatisticsByCategories.py
  80. +2 −4 python/plugins/processing/algs/qgis/SumLines.py
  81. +3 −4 python/plugins/processing/algs/qgis/TextToFloat.py
  82. +2 −2 python/plugins/processing/algs/qgis/VectorSplit.py
  83. +4 −3 python/plugins/processing/algs/qgis/VoronoiPolygons.py
  84. +1 −3 python/plugins/processing/algs/qgis/ZonalStatistics.py
@@ -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)
@@ -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 = \
@@ -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')))
@@ -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()
@@ -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:
@@ -115,7 +114,6 @@ def processAlgorithm(self, progress):
values.append(length)
sumValue += length

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

n = float(len(values))
@@ -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)
@@ -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
@@ -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()
@@ -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
@@ -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())
@@ -132,7 +131,6 @@ def processAlgorithm(self, progress):
'invalid geometry.'))
continue

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

del writer
@@ -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()
@@ -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
@@ -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)
@@ -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
@@ -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))

0 comments on commit e7fe61e

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