Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #4412 from nyalldawson/processing_get_features
Browse files Browse the repository at this point in the history
Port processing vector.features to c++
  • Loading branch information
nyalldawson committed Apr 26, 2017
2 parents fea6bff + a4cd66f commit 5169e0d
Show file tree
Hide file tree
Showing 195 changed files with 1,242 additions and 767 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -276,6 +276,7 @@
%Include layertree/qgslayertreeutils.sip

%Include processing/qgsprocessingalgorithm.sip
%Include processing/qgsprocessingcontext.sip
%Include processing/qgsprocessingfeedback.sip
%Include processing/qgsprocessingprovider.sip
%Include processing/qgsprocessingregistry.sip
Expand Down
127 changes: 127 additions & 0 deletions python/core/processing/qgsprocessingcontext.sip
@@ -0,0 +1,127 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/processing/qgsprocessingcontext.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsProcessingContext
{
%Docstring
Contains information about the context in which a processing algorithm is executed.

Contextual information includes settings such as the associated project, and
expression context.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsprocessingcontext.h"
%End
public:

enum Flag
{
UseSelectionIfPresent,
};
typedef QFlags<QgsProcessingContext::Flag> Flags;


QgsProcessingContext();
%Docstring
Constructor for QgsProcessingContext.
%End

QgsProcessingContext::Flags flags() const;
%Docstring
Returns any flags set in the context.
\see setFlags()
:rtype: QgsProcessingContext.Flags
%End

void setFlags( const QgsProcessingContext::Flags &flags );
%Docstring
Sets ``flags`` for the context.
\see flags()
%End

QgsProject *project() const;
%Docstring
Returns the project in which the algorithm is being executed.
\see setProject()
:rtype: QgsProject
%End

void setProject( QgsProject *project );
%Docstring
Sets the ``project`` in which the algorithm will be executed.
\see project()
%End

QgsExpressionContext expressionContext() const;
%Docstring
Returns the expression context.
:rtype: QgsExpressionContext
%End

void setExpressionContext( const QgsExpressionContext &context );
%Docstring
Sets the expression ``context``.
%End

QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const;
%Docstring
Returns the behavior used for checking invalid geometries in input layers.
\see setInvalidGeometryCheck()
:rtype: QgsFeatureRequest.InvalidGeometryCheck
%End

void setInvalidGeometryCheck( const QgsFeatureRequest::InvalidGeometryCheck &check );
%Docstring
Sets the behavior used for checking invalid geometries in input layers.
\see invalidGeometryCheck()
%End


void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
%Docstring
Sets a callback function to use when encountering an invalid geometry and
invalidGeometryCheck() is set to GeometryAbortOnInvalid. This function will be
called using the feature with invalid geometry as a parameter.
.. versionadded:: 3.0
\see invalidGeometryCallback()
%End
%MethodCode
Py_BEGIN_ALLOW_THREADS

sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
{
SIP_BLOCK_THREADS
Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
SIP_UNBLOCK_THREADS
} );

Py_END_ALLOW_THREADS
%End


};
QFlags<QgsProcessingContext::Flag> operator|(QgsProcessingContext::Flag f1, QFlags<QgsProcessingContext::Flag> f2);






/************************************************************************
* This file has been generated automatically from *
* *
* src/core/processing/qgsprocessingcontext.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
27 changes: 27 additions & 0 deletions python/core/processing/qgsprocessingutils.sip
Expand Up @@ -97,6 +97,33 @@ class QgsProcessingUtils
:rtype: str
%End

static QgsFeatureIterator getFeatures( QgsVectorLayer *layer, const QgsProcessingContext &context, const QgsFeatureRequest &request = QgsFeatureRequest() );
%Docstring
Returns an iterator for the features in a ``layer``, respecting
the settings from the supplied ``context``.
An optional base ``request`` can be used to optimise the returned
iterator, eg by restricting the returned attributes or geometry.
:rtype: QgsFeatureIterator
%End

static long featureCount( QgsVectorLayer *layer, const QgsProcessingContext &context );
%Docstring
Returns an approximate feature count for a ``layer``, when
the settings from the supplied ``context`` are respected. E.g. if the
context is set to only use selected features, then calling this will
return the count of selected features in the layer.
:rtype: long
%End

static QList< QVariant > uniqueValues( QgsVectorLayer *layer, int fieldIndex, const QgsProcessingContext &context );
%Docstring
Returns a list of unique values contained in a single field in a ``layer``, when
the settings from the supplied ``context`` are respected. E.g. if the
context is set to only use selected features, then calling this will
return unique values from selected features in the layer.
:rtype: list of QVariant
%End

};


Expand Down
Expand Up @@ -25,7 +25,7 @@

__revision__ = '$Format:%H$'

from qgis.core import QgsVectorFileWriter, QgsSettings
from qgis.core import QgsVectorFileWriter, QgsSettings, QgsProcessingUtils

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
Expand Down Expand Up @@ -79,8 +79,10 @@ def defineCharacteristics(self):
self.addOutput(OutputVector(self.OUTPUT_LAYER,
self.tr('Output layer with selected features')))

def processAlgorithm(self, feedback):
"""Here is where the processing itself takes place."""
def processAlgorithm(self, context, feedback):
"""Here is where the processing itself takes place.
:param context:
"""

# The first thing to do is retrieve the values of the parameters
# entered by the user
Expand Down Expand Up @@ -111,7 +113,7 @@ def processAlgorithm(self, feedback):
# selection that might exist in layer and the configuration that
# indicates should algorithm use only selected features or all
# of them
features = vector.features(vectorLayer)
features = QgsProcessingUtils.getFeatures(vectorLayer, context)
for f in features:
writer.addFeature(f)

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/GdalAlgorithm.py
Expand Up @@ -58,7 +58,7 @@ def svgIconPath(self):
def getCustomParametersDialog(self):
return GdalAlgorithmDialog(self)

def processAlgorithm(self, feedback):
def processAlgorithm(self, context, feedback):
commands = self.getConsoleCommands()
layers = QgsProcessingUtils.compatibleVectorLayers(QgsProject.instance())
supported = QgsVectorFileWriter.supportedFormatExtensions()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/extractprojection.py
Expand Up @@ -64,7 +64,7 @@ def defineCharacteristics(self):
def getConsoleCommands(self):
return ["extractprojection"]

def processAlgorithm(self, feedback):
def processAlgorithm(self, context, feedback):
rasterPath = self.getParameterValue(self.INPUT)
createPrj = self.getParameterValue(self.PRJ_FILE)

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/information.py
Expand Up @@ -77,7 +77,7 @@ def getConsoleCommands(self):
arguments.append(self.getParameterValue(information.INPUT))
return ['gdalinfo', GdalUtils.escapeAndJoin(arguments)]

def processAlgorithm(self, feedback):
def processAlgorithm(self, context, feedback):
GdalUtils.runGdal(self.getConsoleCommands(), feedback)
output = self.getOutputValue(information.OUTPUT)
with open(output, 'w') as f:
Expand Down
Expand Up @@ -125,9 +125,9 @@ def defineCharacteristics(self):
self.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options'), '', optional=True))

def processAlgorithm(self, feedback):
def processAlgorithm(self, context, feedback):
self.processing = True
GdalAlgorithm.processAlgorithm(self, feedback)
GdalAlgorithm.processAlgorithm(self, context, feedback)
self.processing = False

def getConsoleCommands(self):
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/gdal/ogr2ogrtopostgislist.py
Expand Up @@ -177,9 +177,9 @@ def defineCharacteristics(self):
self.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options'), '', optional=True))

def processAlgorithm(self, feedback):
def processAlgorithm(self, context, feedback):
self.processing = True
GdalAlgorithm.processAlgorithm(self, feedback)
GdalAlgorithm.processAlgorithm(None, self)
self.processing = False

def getConsoleCommands(self):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/ogrinfo.py
Expand Up @@ -70,7 +70,7 @@ def getConsoleCommands(self):
arguments.append(conn)
return arguments

def processAlgorithm(self, feedback):
def processAlgorithm(self, context, feedback):
GdalUtils.runGdal(self.getConsoleCommands(), feedback)
output = self.getOutputValue(self.OUTPUT)
with open(output, 'w') as f:
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/Grass7Algorithm.py
Expand Up @@ -247,7 +247,7 @@ def getDefaultCellsize(self):
cellsize = 100
return cellsize

def processAlgorithm(self, feedback):
def processAlgorithm(self, context, feedback):
if system.isWindows():
path = Grass7Utils.grassPath()
if path == '':
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/nviz7.py
Expand Up @@ -92,7 +92,7 @@ def defineCharacteristics(self):
self.tr('GRASS region cellsize (leave 0 for default)'),
0, None, 0.0))

def processAlgorithm(self, feedback):
def processAlgorithm(self, context, feedback):
commands = []
vector = self.getParameterValue(self.VECTOR)
elevation = self.getParameterValue(self.ELEVATION)
Expand Down
9 changes: 5 additions & 4 deletions python/plugins/processing/algs/qgis/AddTableField.py
Expand Up @@ -28,7 +28,8 @@
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsField,
QgsFeature,
QgsApplication)
QgsApplication,
QgsProcessingUtils)
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
Expand Down Expand Up @@ -83,7 +84,7 @@ def defineCharacteristics(self):
self.addOutput(OutputVector(
self.OUTPUT_LAYER, self.tr('Added')))

def processAlgorithm(self, feedback):
def processAlgorithm(self, context, feedback):
fieldType = self.getParameterValue(self.FIELD_TYPE)
fieldName = self.getParameterValue(self.FIELD_NAME)
fieldLength = self.getParameterValue(self.FIELD_LENGTH)
Expand All @@ -99,8 +100,8 @@ def processAlgorithm(self, feedback):
writer = output.getVectorWriter(fields, layer.wkbType(),
layer.crs())
outFeat = QgsFeature()
features = vector.features(layer)
total = 100.0 / len(features)
features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / QgsProcessingUtils.featureCount(layer, context)
for current, feat in enumerate(features):
feedback.setProgress(int(current * total))
geom = feat.geometry()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Aspect.py
Expand Up @@ -66,7 +66,7 @@ def defineCharacteristics(self):
self.addOutput(OutputRaster(self.OUTPUT_LAYER,
self.tr('Aspect')))

def processAlgorithm(self, feedback):
def processAlgorithm(self, context, feedback):
inputFile = self.getParameterValue(self.INPUT_LAYER)
zFactor = self.getParameterValue(self.Z_FACTOR)
outputFile = self.getOutputValue(self.OUTPUT_LAYER)
Expand Down
9 changes: 5 additions & 4 deletions python/plugins/processing/algs/qgis/AutoincrementalField.py
Expand Up @@ -28,7 +28,8 @@
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsField,
QgsFeature,
QgsApplication)
QgsApplication,
QgsProcessingUtils)
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
Expand Down Expand Up @@ -60,7 +61,7 @@ def defineCharacteristics(self):
self.tr('Input layer')))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Incremented')))

def processAlgorithm(self, feedback):
def processAlgorithm(self, context, feedback):
output = self.getOutputFromName(self.OUTPUT)
vlayer = \
dataobjects.getLayerFromString(self.getParameterValue(self.INPUT))
Expand All @@ -69,8 +70,8 @@ def processAlgorithm(self, feedback):
writer = output.getVectorWriter(fields, vlayer.wkbType(),
vlayer.crs())
outFeat = QgsFeature()
features = vector.features(vlayer)
total = 100.0 / len(features)
features = QgsProcessingUtils.getFeatures(vlayer, context)
total = 100.0 / QgsProcessingUtils.featureCount(vlayer, context)
for current, feat in enumerate(features):
feedback.setProgress(int(current * total))
geom = feat.geometry()
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/BarPlot.py
Expand Up @@ -73,15 +73,15 @@ def defineCharacteristics(self):

self.addOutput(OutputHTML(self.OUTPUT, self.tr('Bar plot')))

def processAlgorithm(self, feedback):
def processAlgorithm(self, context, feedback):
layer = dataobjects.getLayerFromString(
self.getParameterValue(self.INPUT))
namefieldname = self.getParameterValue(self.NAME_FIELD)
valuefieldname = self.getParameterValue(self.VALUE_FIELD)

output = self.getOutputValue(self.OUTPUT)

values = vector.values(layer, valuefieldname)
values = vector.values(layer, context, valuefieldname)

x_var = [i[namefieldname] for i in layer.getFeatures()]

Expand Down

0 comments on commit 5169e0d

Please sign in to comment.