Skip to content

Commit

Permalink
[processing] Skip more unnecessary geometry checks
Browse files Browse the repository at this point in the history
(cherry-picked from 405d8f2)
  • Loading branch information
nyalldawson committed Apr 10, 2018
1 parent 9b0ddc6 commit 07207e0
Show file tree
Hide file tree
Showing 34 changed files with 109 additions and 32 deletions.
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/qgis/AddTableField.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
QgsProcessing,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsProcessingParameterEnum)
QgsProcessingParameterEnum,
QgsProcessingFeatureSource)
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm


Expand Down Expand Up @@ -93,6 +94,9 @@ def outputFields(self, inputFields):
inputFields.append(self.field)
return inputFields

def sourceFlags(self):
return QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks

def processFeature(self, feature, context, feedback):
attributes = feature.attributes()
attributes.append(None)
Expand Down
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/BasicStatistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingParameterFileDestination,
QgsProcessingOutputNumber)
QgsProcessingOutputNumber,
QgsProcessingFeatureSource)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm

Expand Down Expand Up @@ -133,7 +134,7 @@ def processAlgorithm(self, parameters, context, feedback):
output_file = self.parameterAsFileOutput(parameters, self.OUTPUT_HTML_FILE, context)

request = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes([field_name], source.fields())
features = source.getFeatures(request)
features = source.getFeatures(request, QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks)
count = source.featureCount()

data = []
Expand Down
3 changes: 1 addition & 2 deletions python/plugins/processing/algs/qgis/CheckValidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
QgsProcessingParameterFeatureSource,
QgsProcessingParameterEnum,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputNumber
)
QgsProcessingOutputNumber)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm

settings_method_key = "/qgis/digitizing/validate_geometries"
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/qgis/DeleteColumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
__revision__ = '$Format:%H$'

from qgis.core import (QgsProcessingParameterField,
QgsProcessing)
QgsProcessing,
QgsProcessingFeatureSource)
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm


Expand Down Expand Up @@ -83,6 +84,9 @@ def outputFields(self, input_fields):
input_fields.remove(index)
return input_fields

def sourceFlags(self):
return QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks

def processFeature(self, feature, context, feedback):
attributes = feature.attributes()
for index in self.field_indices:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
QgsVertexId,
QgsFeature,
QgsFeatureSink,
QgsFeatureRequest,
QgsGeometry,
QgsField,
QgsProcessing,
QgsProcessingFeatureSource,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterString,
QgsProcessingParameterFeatureSink,
Expand Down Expand Up @@ -103,7 +105,7 @@ def processAlgorithm(self, parameters, context, feedback):
raise QgsProcessingException(
self.tr('\'{}\' is not a valid vertex index').format(vertex))

features = source.getFeatures()
features = source.getFeatures(QgsFeatureRequest(), QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks)
total = 100.0 / source.featureCount() if source.featureCount() else 0

for current, f in enumerate(features):
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/qgis/GeometryByExpression.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
QgsProcessingException,
QgsProcessingParameterBoolean,
QgsProcessingParameterEnum,
QgsProcessingParameterExpression)
QgsProcessingParameterExpression,
QgsProcessingFeatureSource)

from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm

Expand Down Expand Up @@ -104,6 +105,9 @@ def prepareAlgorithm(self, parameters, context, feedback):
def outputWkbType(self, input_wkb_type):
return self.wkb_type

def sourceFlags(self):
return QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks

def processFeature(self, feature, context, feedback):
self.expression_context.setFeature(feature)
value = self.expression.evaluate(self.expression_context)
Expand Down
6 changes: 4 additions & 2 deletions python/plugins/processing/algs/qgis/PointsLayerFromTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
QgsProcessingParameterFeatureSink,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterCrs,
QgsProcessingParameterField)
QgsProcessingParameterField,
QgsProcessingFeatureSource,
QgsFeatureRequest)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm


Expand Down Expand Up @@ -107,7 +109,7 @@ def processAlgorithm(self, parameters, context, feedback):
fields, wkb_type, target_crs)

request = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry)
features = source.getFeatures()
features = source.getFeatures(QgsFeatureRequest(), QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks)
total = 100.0 / source.featureCount() if source.featureCount() else 0

for current, feature in enumerate(features):
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/PointsToPaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingParameterString,
QgsProcessingFeatureSource,
QgsProcessing,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterFolderDestination)
Expand Down Expand Up @@ -126,7 +127,7 @@ def processAlgorithm(self, parameters, context, feedback):
fields, output_wkb, source.sourceCrs())

points = dict()
features = source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([group_field_index, order_field_index]))
features = source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([group_field_index, order_field_index]), QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks)
total = 100.0 / source.featureCount() if source.featureCount() else 0
for current, f in enumerate(features):
if feedback.isCanceled():
Expand Down
6 changes: 4 additions & 2 deletions python/plugins/processing/algs/qgis/RandomExtract.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
QgsProcessingParameterFeatureSource,
QgsProcessingParameterEnum,
QgsProcessingParameterNumber,
QgsProcessingParameterFeatureSink)
QgsProcessingParameterFeatureSink,
QgsFeatureRequest,
QgsProcessingFeatureSource)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm


Expand Down Expand Up @@ -78,7 +80,7 @@ def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.INPUT, context)
method = self.parameterAsEnum(parameters, self.METHOD, context)

features = source.getFeatures()
features = source.getFeatures(QgsFeatureRequest(), QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks)
featureCount = source.featureCount()
value = self.parameterAsInt(parameters, self.NUMBER, context)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
QgsProcessingParameterEnum,
QgsProcessingParameterField,
QgsProcessingParameterNumber,
QgsProcessingParameterFeatureSink)
QgsProcessingParameterFeatureSink,
QgsProcessingFeatureSource,
QgsFeatureRequest)
from collections import defaultdict
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm

Expand Down Expand Up @@ -88,7 +90,7 @@ def processAlgorithm(self, parameters, context, feedback):

index = source.fields().lookupField(field)

features = source.getFeatures()
features = source.getFeatures(QgsFeatureRequest(), QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks)
featureCount = source.featureCount()
unique = source.uniqueValues(index)
value = self.parameterAsInt(parameters, self.NUMBER, context)
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/qgis/SetMValue.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
QgsWkbTypes,
QgsPropertyDefinition,
QgsProcessingParameters,
QgsProcessingParameterNumber)
QgsProcessingParameterNumber,
QgsProcessingFeatureSource)


from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
Expand Down Expand Up @@ -85,6 +86,9 @@ def prepareAlgorithm(self, parameters, context, feedback):
self.m_property = parameters[self.M_VALUE]
return True

def sourceFlags(self):
return QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks

def processFeature(self, feature, context, feedback):
input_geometry = feature.geometry()
if input_geometry:
Expand Down
3 changes: 3 additions & 0 deletions python/plugins/processing/algs/qgis/SetZValue.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def initParameters(self, config=None):
def outputWkbType(self, inputWkb):
return QgsWkbTypes.addZ(inputWkb)

def sourceFlags(self):
return QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks

def prepareAlgorithm(self, parameters, context, feedback):
self.z_value = self.parameterAsDouble(parameters, self.Z_VALUE, context)
self.dynamic_z = QgsProcessingParameters.isDynamic(parameters, self.Z_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
QgsFeature,
QgsFeatureSink,
QgsProcessing,
QgsProcessingFeatureSource,
NULL)
from qgis.PyQt.QtCore import QVariant
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
Expand Down Expand Up @@ -155,7 +156,7 @@ def addField(name):
attrs = []
attrs.extend(category_field_indexes)
request.setSubsetOfAttributes(attrs)
features = source.getFeatures(request)
features = source.getFeatures(request, QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks)
total = 50.0 / source.featureCount() if source.featureCount() else 0
if field_type == 'none':
values = defaultdict(lambda: 0)
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/qgis/TextToFloat.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsField,
QgsProcessing,
QgsProcessingParameterField)
QgsProcessingParameterField,
QgsProcessingFeatureSource)
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm


Expand Down Expand Up @@ -76,6 +77,9 @@ def prepareAlgorithm(self, parameters, context, feedback):
self.field_name = self.parameterAsString(parameters, self.FIELD, context)
return True

def sourceFlags(self):
return QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks

def processFeature(self, feature, context, feedback):
value = feature[self.field_idx]
try:
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/UniqueValues.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
QgsProcessingParameterFeatureSink,
QgsProcessingOutputNumber,
QgsProcessingOutputString,
QgsProcessingFeatureSource,
QgsProcessingParameterFileDestination)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
Expand Down Expand Up @@ -118,7 +119,7 @@ def processAlgorithm(self, parameters, context, feedback):
request = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry)
request.setSubsetOfAttributes(field_indices)
total = 100.0 / source.featureCount() if source.featureCount() else 0
for current, f in enumerate(source.getFeatures(request)):
for current, f in enumerate(source.getFeatures(request, QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks)):
if feedback.isCanceled():
break

Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmaddincrementalfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ QgsAddIncrementalFieldAlgorithm *QgsAddIncrementalFieldAlgorithm::createInstance
return new QgsAddIncrementalFieldAlgorithm();
}

QgsProcessingFeatureSource::Flag QgsAddIncrementalFieldAlgorithm::sourceFlags() const
{
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
}

void QgsAddIncrementalFieldAlgorithm::initParameters( const QVariantMap & )
{
addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_NAME" ), QObject::tr( "Field name" ), QStringLiteral( "AUTO" ) ) );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmaddincrementalfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class QgsAddIncrementalFieldAlgorithm : public QgsProcessingFeatureBasedAlgorith
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;
QString outputName() const override;
QgsFields outputFields( const QgsFields &inputFields ) const override;
QgsProcessingFeatureSource::Flag sourceFlags() const override;

bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmassignprojection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ void QgsAssignProjectionAlgorithm::initParameters( const QVariantMap & )
addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Assigned CRS" ), QStringLiteral( "EPSG:4326" ) ) );
}

QgsProcessingFeatureSource::Flag QgsAssignProjectionAlgorithm::sourceFlags() const
{
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
}

bool QgsAssignProjectionAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
mDestCrs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmassignprojection.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class QgsAssignProjectionAlgorithm : public QgsProcessingFeatureBasedAlgorithm
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;
QgsCoordinateReferenceSystem outputCrs( const QgsCoordinateReferenceSystem & ) const override { return mDestCrs; }
QString outputName() const override;
QgsProcessingFeatureSource::Flag sourceFlags() const override;

bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmdropgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ QgsWkbTypes::Type QgsDropGeometryAlgorithm::outputWkbType( QgsWkbTypes::Type ) c
return QgsWkbTypes::NoGeometry;
}

QgsProcessingFeatureSource::Flag QgsDropGeometryAlgorithm::sourceFlags() const
{
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
}

QgsFeatureRequest QgsDropGeometryAlgorithm::request() const
{
return QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmdropgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class QgsDropGeometryAlgorithm : public QgsProcessingFeatureBasedAlgorithm
QString outputName() const override;
QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const override;
QgsFeatureRequest request() const override;
QgsProcessingFeatureSource::Flag sourceFlags() const override;
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};

Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmdropmzvalues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ QgsWkbTypes::Type QgsDropMZValuesAlgorithm::outputWkbType( QgsWkbTypes::Type inp
return wkb;
}

QgsProcessingFeatureSource::Flag QgsDropMZValuesAlgorithm::sourceFlags() const
{
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
}

bool QgsDropMZValuesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
mDropM = parameterAsBool( parameters, QStringLiteral( "DROP_M_VALUES" ), context );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmdropmzvalues.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class QgsDropMZValuesAlgorithm : public QgsProcessingFeatureBasedAlgorithm
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;
QString outputName() const override;
QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const override;
QgsProcessingFeatureSource::Flag sourceFlags() const override;
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;

Expand Down
4 changes: 2 additions & 2 deletions src/analysis/processing/qgsalgorithmextractvertices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void QgsExtractVerticesAlgorithm::initAlgorithm( const QVariantMap & )

QVariantMap QgsExtractVerticesAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
std::unique_ptr< QgsFeatureSource > featureSource( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
std::unique_ptr< QgsProcessingFeatureSource > featureSource( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !featureSource )
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

Expand Down Expand Up @@ -99,7 +99,7 @@ QVariantMap QgsExtractVerticesAlgorithm::processAlgorithm( const QVariantMap &pa
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

double step = featureSource->featureCount() > 0 ? 100.0 / featureSource->featureCount() : 1;
QgsFeatureIterator fi = featureSource->getFeatures( QgsFeatureRequest() );
QgsFeatureIterator fi = featureSource->getFeatures( QgsFeatureRequest(), QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks );
QgsFeature f;
int i = -1;
while ( fi.nextFeature( f ) )
Expand Down
8 changes: 4 additions & 4 deletions src/analysis/processing/qgsalgorithmjoinwithlines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ QVariantMap QgsJoinWithLinesAlgorithm::processAlgorithm( const QVariantMap &para
if ( parameters.value( QStringLiteral( "SPOKES" ) ) == parameters.value( QStringLiteral( "HUBS" ) ) )
throw QgsProcessingException( QObject::tr( "Same layer given for both hubs and spokes" ) );

std::unique_ptr< QgsFeatureSource > hubSource( parameterAsSource( parameters, QStringLiteral( "HUBS" ), context ) );
std::unique_ptr< QgsFeatureSource > spokeSource( parameterAsSource( parameters, QStringLiteral( "SPOKES" ), context ) );
std::unique_ptr< QgsProcessingFeatureSource > hubSource( parameterAsSource( parameters, QStringLiteral( "HUBS" ), context ) );
std::unique_ptr< QgsProcessingFeatureSource > spokeSource( parameterAsSource( parameters, QStringLiteral( "SPOKES" ), context ) );
if ( !hubSource || !spokeSource )
throw QgsProcessingException( QObject::tr( "Could not load source layers" ) );

Expand Down Expand Up @@ -192,7 +192,7 @@ QVariantMap QgsJoinWithLinesAlgorithm::processAlgorithm( const QVariantMap &para
return p;
};

QgsFeatureIterator hubFeatures = hubSource->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( hubFields2Fetch ) );
QgsFeatureIterator hubFeatures = hubSource->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( hubFields2Fetch ), QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks );
double step = hubSource->featureCount() > 0 ? 100.0 / hubSource->featureCount() : 1;
int i = 0;
QgsFeature hubFeature;
Expand Down Expand Up @@ -224,7 +224,7 @@ QVariantMap QgsJoinWithLinesAlgorithm::processAlgorithm( const QVariantMap &para
spokeRequest.setSubsetOfAttributes( spokeFields2Fetch );
spokeRequest.setFilterExpression( QgsExpression::createFieldEqualityExpression( fieldSpokeName, hubFeature.attribute( fieldHubIndex ) ) );

QgsFeatureIterator spokeFeatures = spokeSource->getFeatures( spokeRequest );
QgsFeatureIterator spokeFeatures = spokeSource->getFeatures( spokeRequest, QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks );
QgsFeature spokeFeature;
while ( spokeFeatures.nextFeature( spokeFeature ) )
{
Expand Down
Loading

0 comments on commit 07207e0

Please sign in to comment.