Skip to content
Permalink
Browse files

Merge pull request #4929 from nyalldawson/algs

Restore some more processing algorithms...
  • Loading branch information
nyalldawson committed Jul 27, 2017
2 parents 4aaecb9 + 8218204 commit 5c82a856bf2b4bdd4d9486dae62e553aa076bd58
Showing with 951 additions and 516 deletions.
  1. +22 −1 python/core/processing/qgsprocessingparameters.sip
  2. +14 −3 python/plugins/processing/algs/qgis/ConvexHull.py
  3. +15 −17 python/plugins/processing/algs/qgis/CreateConstantRaster.py
  4. +33 −28 python/plugins/processing/algs/qgis/Explode.py
  5. +76 −58 python/plugins/processing/algs/qgis/ExportGeometryInfo.py
  6. +3 −3 python/plugins/processing/algs/qgis/FieldsCalculator.py
  7. +3 −3 python/plugins/processing/algs/qgis/FieldsMapper.py
  8. +1 −1 python/plugins/processing/algs/qgis/HubDistanceLines.py
  9. +1 −1 python/plugins/processing/algs/qgis/HubDistancePoints.py
  10. +1 −1 python/plugins/processing/algs/qgis/ImportIntoSpatialite.py
  11. +19 −38 python/plugins/processing/algs/qgis/MergeLines.py
  12. +1 −1 python/plugins/processing/algs/qgis/NearestNeighbourAnalysis.py
  13. +2 −2 python/plugins/processing/algs/qgis/PointDistance.py
  14. +1 −1 python/plugins/processing/algs/qgis/PointsToPaths.py
  15. +40 −44 python/plugins/processing/algs/qgis/Polygonize.py
  16. +24 −23 python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
  17. +1 −1 python/plugins/processing/algs/qgis/RandomPointsAlongLines.py
  18. +1 −1 python/plugins/processing/algs/qgis/RandomPointsPolygons.py
  19. +1 −1 python/plugins/processing/algs/qgis/RasterCalculator.py
  20. +2 −3 python/plugins/processing/algs/qgis/ServiceAreaFromLayer.py
  21. +2 −3 python/plugins/processing/algs/qgis/ServiceAreaFromPoint.py
  22. +0 −1 python/plugins/processing/algs/qgis/SetRasterStyle.py
  23. +0 −1 python/plugins/processing/algs/qgis/SetVectorStyle.py
  24. +2 −3 python/plugins/processing/algs/qgis/ShortestPathLayerToPoint.py
  25. +2 −3 python/plugins/processing/algs/qgis/ShortestPathPointToLayer.py
  26. +2 −3 python/plugins/processing/algs/qgis/ShortestPathPointToPoint.py
  27. +10 −12 python/plugins/processing/algs/qgis/SpatialIndex.py
  28. +1 −1 python/plugins/processing/algs/qgis/SpatialiteExecuteSQL.py
  29. +37 −30 python/plugins/processing/algs/qgis/SplitWithLines.py
  30. +1 −1 python/plugins/processing/algs/qgis/SumLines.py
  31. +38 −47 python/plugins/processing/algs/qgis/TextToFloat.py
  32. +9 −14 python/plugins/processing/algs/qgis/TruncateTable.py
  33. +11 −1 python/plugins/processing/gui/wrappers.py
  34. +1 −1 python/plugins/processing/tests/AlgorithmsTestBase.py
  35. +53 −0 python/plugins/processing/tests/testdata/custom/text_to_float.gml
  36. +44 −0 python/plugins/processing/tests/testdata/custom/text_to_float.xsd
  37. BIN python/plugins/processing/tests/testdata/expected/constant_raster.tif
  38. +10 −0 python/plugins/processing/tests/testdata/expected/constant_raster.tif.aux.xml
  39. +16 −0 python/plugins/processing/tests/testdata/expected/explode_lines.gfs
  40. +68 −0 python/plugins/processing/tests/testdata/expected/explode_lines.gml
  41. +16 −0 python/plugins/processing/tests/testdata/expected/explode_multilines.gfs
  42. +58 −0 python/plugins/processing/tests/testdata/expected/explode_multilines.gml
  43. +0 −10 python/plugins/processing/tests/testdata/expected/polygonize.gfs
  44. +0 −6 python/plugins/processing/tests/testdata/expected/polygonize.gml
  45. +31 −0 python/plugins/processing/tests/testdata/expected/text_to_float.gfs
  46. +52 −0 python/plugins/processing/tests/testdata/expected/text_to_float.gml
  47. +162 −110 python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
  48. +0 −32 python/plugins/processing/tools/vector.py
  49. +37 −2 src/core/processing/qgsprocessingparameters.cpp
  50. +24 −1 src/core/processing/qgsprocessingparameters.h
  51. +3 −3 tests/src/core/testqgsprocessing.cpp
@@ -1395,7 +1395,10 @@ class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition
%End
public:

QgsProcessingParameterVectorLayer( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
QgsProcessingParameterVectorLayer( const QString &name,
const QString &description = QString(),
const QList< int > &types = QList< int >(),
const QVariant &defaultValue = QVariant(),
bool optional = false );
%Docstring
Constructor for QgsProcessingParameterVectorLayer.
@@ -1412,6 +1415,24 @@ class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;


QList< int > dataTypes() const;
%Docstring
Returns the geometry types for sources acceptable by the parameter.
.. seealso:: setDataTypes()
:rtype: list of int
%End

void setDataTypes( const QList< int > &types );
%Docstring
Sets the geometry ``types`` for sources acceptable by the parameter.
.. seealso:: dataTypes()
%End

virtual QVariantMap toVariantMap() const;

virtual bool fromVariantMap( const QVariantMap &map );


static QgsProcessingParameterVectorLayer *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/;
%Docstring
Creates a new parameter using the definition from a script code.
@@ -37,7 +37,8 @@
QgsGeometry,
QgsWkbTypes,
QgsProcessingUtils,
QgsFields)
QgsFields,
NULL)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
@@ -145,7 +146,12 @@ def processAlgorithm(self, parameters, context, feedback):
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
try:
outGeom = tmpGeom.convexHull()
(area, perim) = vector.simpleMeasure(outGeom)
if outGeom:
area = outGeom.geometry().area()
perim = outGeom.geometry().perimeter()
else:
area = NULL
perim = NULL
outFeat.setGeometry(outGeom)
outFeat.setAttributes([fid, val, area, perim])
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
@@ -166,7 +172,12 @@ def processAlgorithm(self, parameters, context, feedback):
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
try:
outGeom = tmpGeom.convexHull()
(area, perim) = vector.simpleMeasure(outGeom)
if outGeom:
area = outGeom.geometry().area()
perim = outGeom.geometry().perimeter()
else:
area = NULL
perim = NULL
outFeat.setGeometry(outGeom)
outFeat.setAttributes([0, 'all', area, perim])
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
@@ -27,12 +27,10 @@

from osgeo import gdal

from qgis.core import (QgsApplication,
QgsProcessingUtils)
from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputRaster
from processing.tools.raster import RasterWriter


@@ -49,14 +47,12 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterRaster(self.INPUT,
self.tr('Reference layer')))
self.addParameter(ParameterNumber(self.NUMBER,
self.tr('Constant value'),
default=1.0))

self.addOutput(OutputRaster(self.OUTPUT,
self.tr('Constant')))
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
self.tr('Reference layer')))
self.addParameter(QgsProcessingParameterNumber(self.NUMBER,
self.tr('Constant value'), QgsProcessingParameterNumber.Double,
defaultValue=1))
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Constant')))

def name(self):
return 'createconstantrasterlayer'
@@ -65,18 +61,18 @@ def displayName(self):
return self.tr('Create constant raster layer')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
value = self.getParameterValue(self.NUMBER)
layer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
value = self.parameterAsDouble(parameters, self.NUMBER, context)

output = self.getOutputFromName(self.OUTPUT)
outputFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

raster = gdal.Open(layer.source(), gdal.GA_ReadOnly)
geoTransform = raster.GetGeoTransform()

cellsize = (layer.extent().xMaximum() - layer.extent().xMinimum()) \
/ layer.width()

w = RasterWriter(output.getCompatibleFileName(self),
w = RasterWriter(outputFile,
layer.extent().xMinimum(),
layer.extent().yMinimum(),
layer.extent().xMaximum(),
@@ -88,3 +84,5 @@ def processAlgorithm(self, parameters, context, feedback):
)
w.matrix.fill(value)
w.close()

return {self.OUTPUT: outputFile}
@@ -30,12 +30,11 @@
QgsGeometry,
QgsFeatureSink,
QgsWkbTypes,
QgsApplication,
QgsProcessingUtils)
QgsProcessing,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsLineString)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools import dataobjects


class Explode(QgisAlgorithm):
@@ -50,10 +49,10 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'),
[dataobjects.TYPE_VECTOR_LINE]))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Exploded'), datatype=[dataobjects.TYPE_VECTOR_LINE]))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer'), [QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT,
self.tr('Exploded'), QgsProcessing.TypeVectorLine))

def name(self):
return 'explodelines'
@@ -62,40 +61,46 @@ def displayName(self):
return self.tr('Explode lines')

def processAlgorithm(self, parameters, context, feedback):
vlayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
output = self.getOutputFromName(self.OUTPUT)
fields = vlayer.fields()
writer = output.getVectorWriter(fields, QgsWkbTypes.LineString, vlayer.crs(), context)
outFeat = QgsFeature()
features = QgsProcessingUtils.getFeatures(vlayer, context)
total = 100.0 / vlayer.featureCount() if vlayer.featureCount() else 0
source = self.parameterAsSource(parameters, self.INPUT, context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
source.fields(), QgsWkbTypes.singleType(source.wkbType()), source.sourceCrs())

features = source.getFeatures()
total = 100.0 / source.featureCount() if source.featureCount() else 0
for current, feature in enumerate(features):
if feedback.isCanceled():
break

feedback.setProgress(int(current * total))

if not feature.hasGeometry():
sink.addFeature(feature, QgsFeatureSink.FastInsert)
continue

outFeat = QgsFeature()
inGeom = feature.geometry()
atMap = feature.attributes()
segments = self.extractAsSingleSegments(inGeom)
outFeat.setAttributes(atMap)
outFeat.setAttributes(feature.attributes())
for segment in segments:
outFeat.setGeometry(segment)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
del writer
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
return {self.OUTPUT: dest_id}

def extractAsSingleSegments(self, geom):
segments = []
if geom.isMultipart():
multi = geom.asMultiPolyline()
for polyline in multi:
segments.extend(self.getPolylineAsSingleSegments(polyline))
for part in range(geom.geometry().numGeometries()):
segments.extend(self.getPolylineAsSingleSegments(geom.geometry().geometryN(part)))
else:
segments.extend(self.getPolylineAsSingleSegments(
geom.asPolyline()))
geom.geometry()))
return segments

def getPolylineAsSingleSegments(self, polyline):
segments = []
for i in range(len(polyline) - 1):
ptA = polyline[i]
ptB = polyline[i + 1]
segment = QgsGeometry.fromPolyline([ptA, ptB])
for i in range(polyline.numPoints() - 1):
ptA = polyline.pointN(i)
ptB = polyline.pointN(i + 1)
segment = QgsGeometry(QgsLineString([ptA, ptB]))
segments.append(segment)
return segments

0 comments on commit 5c82a85

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