Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use FastInsert when adding features from processing
  • Loading branch information
nyalldawson committed Jun 23, 2017
1 parent 99cfb8f commit 81855a7
Show file tree
Hide file tree
Showing 120 changed files with 287 additions and 198 deletions.
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/AddTableField.py
Expand Up @@ -28,6 +28,7 @@
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsField,
QgsFeature,
QgsFeatureSink,
QgsApplication,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
Expand Down Expand Up @@ -111,7 +112,7 @@ def processAlgorithm(self, parameters, context, feedback):
attributes.append(None)
output_feature.setAttributes(attributes)

sink.addFeature(output_feature)
sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

return {self.OUTPUT_LAYER: dest_id}
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/Aspect.py
Expand Up @@ -33,7 +33,8 @@
from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterOutput,
QgsProcessingOutputRasterLayer)
QgsProcessingOutputRasterLayer,
QgsFeatureSink)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterNumber
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/AutoincrementalField.py
Expand Up @@ -28,6 +28,7 @@
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsField,
QgsFeature,
QgsFeatureSink,
QgsApplication,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
Expand Down Expand Up @@ -84,7 +85,7 @@ def processAlgorithm(self, parameters, context, feedback):
attributes.append(current)
output_feature.setAttributes(attributes)

sink.addFeature(output_feature)
sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

return {self.OUTPUT: dest_id}
1 change: 1 addition & 0 deletions python/plugins/processing/algs/qgis/BarPlot.py
Expand Up @@ -30,6 +30,7 @@


from qgis.core import (QgsApplication,
QgsFeatureSink,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
Expand Down
1 change: 1 addition & 0 deletions python/plugins/processing/algs/qgis/BasicStatistics.py
Expand Up @@ -32,6 +32,7 @@
from qgis.PyQt.QtGui import QIcon

from qgis.core import (QgsStatisticalSummary,
QgsFeatureSink,
QgsStringStatisticalSummary,
QgsDateTimeStatisticalSummary,
QgsFeatureRequest,
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/Boundary.py
Expand Up @@ -29,6 +29,7 @@

from qgis.core import (QgsGeometry,
QgsWkbTypes,
QgsFeatureSink,
QgsProcessingUtils,
QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSource,
Expand Down Expand Up @@ -101,7 +102,7 @@ def processAlgorithm(self, parameters, context, feedback):

output_feature.setGeometry(output_geometry)

sink.addFeature(output_feature)
sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

return {self.OUTPUT_LAYER: dest_id}
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/BoundingBox.py
Expand Up @@ -29,6 +29,7 @@

from qgis.core import (QgsGeometry,
QgsWkbTypes,
QgsFeatureSink,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
Expand Down Expand Up @@ -93,7 +94,7 @@ def processAlgorithm(self, parameters, context, feedback):

output_feature.setGeometry(output_geometry)

sink.addFeature(output_feature)
sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

return {self.OUTPUT_LAYER: dest_id}
1 change: 1 addition & 0 deletions python/plugins/processing/algs/qgis/BoxPlot.py
Expand Up @@ -29,6 +29,7 @@
import plotly.graph_objs as go

from qgis.core import (QgsApplication,
QgsFeatureSink,
QgsProcessingUtils)
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterTableField
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/Buffer.py
Expand Up @@ -60,7 +60,7 @@ def buffering(feedback, context, writer, distance, field, useField, layer, disso
final_geometry = QgsGeometry.unaryUnion(buffered_geometries)
outFeat.setGeometry(final_geometry)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
else:
# Without dissolve
for inFeat in features:
Expand All @@ -74,7 +74,7 @@ def buffering(feedback, context, writer, distance, field, useField, layer, disso
outGeom = inGeom.buffer(float(value), segments, endCapStyle, joinStyle, mitreLimit)
outFeat.setGeometry(outGeom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
current += 1
feedback.setProgress(int(current * total))

Expand Down
7 changes: 4 additions & 3 deletions python/plugins/processing/algs/qgis/CheckValidity.py
Expand Up @@ -34,6 +34,7 @@
QgsGeometry,
QgsFeature,
QgsField,
QgsFeatureSink,
QgsWkbTypes,
QgsProcessingUtils,
QgsFields,
Expand Down Expand Up @@ -151,7 +152,7 @@ def doCheck(self, method, parameters, context, feedback):
errFeat.setGeometry(error_geom)
errFeat.setAttributes([error.what()])
if error_output_sink:
error_output_sink.addFeature(errFeat)
error_output_sink.addFeature(errFeat, QgsFeatureSink.FastInsert)
error_count += 1

reasons.append(error.what())
Expand All @@ -167,12 +168,12 @@ def doCheck(self, method, parameters, context, feedback):

if valid:
if valid_output_sink:
valid_output_sink.addFeature(outFeat)
valid_output_sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
valid_count += 1

else:
if invalid_output_sink:
invalid_output_sink.addFeature(outFeat)
invalid_output_sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
invalid_count += 1

feedback.setProgress(int(current * total))
Expand Down
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/ConcaveHull.py
Expand Up @@ -29,6 +29,7 @@
from qgis.core import (QgsFeatureRequest,
QgsFeature,
QgsGeometry,
QgsFeatureSink,
QgsWkbTypes,
QgsApplication,
QgsProcessingUtils)
Expand Down Expand Up @@ -152,13 +153,13 @@ def processAlgorithm(self, parameters, context, feedback):
while deleted:
deleted = single_geom.deleteRing(1)
single_feature.setGeometry(single_geom)
writer.addFeature(single_feature)
writer.addFeature(single_feature, QgsFeatureSink.FastInsert)
else:
# Multipart geometries are allowed
if not holes:
# Delete holes
deleted = True
while deleted:
deleted = geom.deleteRing(1)
writer.addFeature(feat)
writer.addFeature(feat, QgsFeatureSink.FastInsert)
del writer
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/ConvexHull.py
Expand Up @@ -33,6 +33,7 @@

from qgis.core import (QgsField,
QgsFeature,
QgsFeatureSink,
QgsGeometry,
QgsWkbTypes,
QgsProcessingUtils,
Expand Down Expand Up @@ -145,7 +146,7 @@ def processAlgorithm(self, parameters, context, feedback):
(area, perim) = vector.simpleMeasure(outGeom)
outFeat.setGeometry(outGeom)
outFeat.setAttributes([fid, val, area, perim])
writer.addFeature(outFeat)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
except:
raise GeoAlgorithmExecutionException(
self.tr('Exception while computing convex hull'))
Expand All @@ -166,7 +167,7 @@ def processAlgorithm(self, parameters, context, feedback):
(area, perim) = vector.simpleMeasure(outGeom)
outFeat.setGeometry(outGeom)
outFeat.setAttributes([0, 'all', area, perim])
writer.addFeature(outFeat)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
except:
raise GeoAlgorithmExecutionException(
self.tr('Exception while computing convex hull'))
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/Delaunay.py
Expand Up @@ -33,6 +33,7 @@

from qgis.core import (QgsField,
QgsFeatureRequest,
QgsFeatureSink,
QgsFeature,
QgsGeometry,
QgsPointXY,
Expand Down Expand Up @@ -145,7 +146,7 @@ def processAlgorithm(self, parameters, context, feedback):
feat.setAttributes(attrs)
geometry = QgsGeometry().fromPolygon([polygon])
feat.setGeometry(geometry)
writer.addFeature(feat)
writer.addFeature(feat, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

del writer
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/DeleteColumn.py
Expand Up @@ -26,6 +26,7 @@
__revision__ = '$Format:%H$'

from qgis.core import (QgsApplication,
QgsFeatureSink,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
Expand Down Expand Up @@ -101,7 +102,7 @@ def processAlgorithm(self, parameters, context, feedback):
for index in field_indices:
del attributes[index]
f.setAttributes(attributes)
sink.addFeature(f)
sink.addFeature(f, QgsFeatureSink.FastInsert)

feedback.setProgress(int(current * total))

Expand Down
Expand Up @@ -27,6 +27,7 @@

from qgis.core import (QgsFeatureRequest,
QgsApplication,
QgsFeatureSink,
QgsProcessingUtils)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
Expand Down Expand Up @@ -86,7 +87,7 @@ def processAlgorithm(self, parameters, context, feedback):
total = 100.0 / len(cleaned) if cleaned else 1
request = QgsFeatureRequest().setFilterFids(list(cleaned.keys()))
for current, f in enumerate(layer.getFeatures(request)):
writer.addFeature(f)
writer.addFeature(f, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

del writer
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/DeleteHoles.py
Expand Up @@ -25,6 +25,7 @@
__revision__ = '$Format:%H$'

from qgis.core import (QgsApplication,
QgsFeatureSink,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterNumber,
Expand Down Expand Up @@ -87,7 +88,7 @@ def processAlgorithm(self, parameters, context, feedback):

if f.hasGeometry():
f.setGeometry(f.geometry().removeInteriorRings(min_area))
sink.addFeature(f)
sink.addFeature(f, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

return {self.OUTPUT: dest_id}
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/DensifyGeometries.py
Expand Up @@ -29,6 +29,7 @@
import os

from qgis.core import (QgsWkbTypes,
QgsFeatureSink,
QgsApplication,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterNumber,
Expand Down Expand Up @@ -92,7 +93,7 @@ def processAlgorithm(self, parameters, context, feedback):
if feature.hasGeometry():
new_geometry = feature.geometry().densifyByCount(vertices)
feature.setGeometry(new_geometry)
sink.addFeature(feature)
sink.addFeature(feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

return {self.OUTPUT: dest_id}
Expand Up @@ -31,6 +31,7 @@

from qgis.core import (QgsWkbTypes,
QgsApplication,
QgsFeatureSink,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterNumber,
QgsProcessingParameterFeatureSink,
Expand Down Expand Up @@ -89,7 +90,7 @@ def processAlgorithm(self, parameters, context, feedback):
if feature.hasGeometry():
new_geometry = feature.geometry().densifyByDistance(float(interval))
feature.setGeometry(new_geometry)
sink.addFeature(feature)
sink.addFeature(feature, QgsFeatureSink.FastInsert)

feedback.setProgress(int(current * total))

Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/Difference.py
Expand Up @@ -31,6 +31,7 @@

from qgis.core import (QgsFeatureRequest,
QgsFeature,
QgsFeatureSink,
QgsGeometry,
QgsWkbTypes,
QgsMessageLog,
Expand Down Expand Up @@ -96,7 +97,7 @@ def processAlgorithm(self, parameters, context, feedback):
try:
outFeat.setGeometry(diff_geom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
except:
QgsMessageLog.logMessage(self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'), self.tr('Processing'), QgsMessageLog.WARNING)
continue
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/DropGeometry.py
Expand Up @@ -27,6 +27,7 @@

from qgis.core import (QgsFeatureRequest,
QgsWkbTypes,
QgsFeatureSink,
QgsCoordinateReferenceSystem,
QgsApplication,
QgsProcessingParameterDefinition,
Expand Down Expand Up @@ -80,7 +81,7 @@ def processAlgorithm(self, parameters, context, feedback):
break

input_feature.clearGeometry()
sink.addFeature(input_feature)
sink.addFeature(input_feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

return {self.OUTPUT_TABLE: dest_id}
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/EliminateSelection.py
Expand Up @@ -32,6 +32,7 @@

from qgis.core import (QgsFeatureRequest,
QgsFeature,
QgsFeatureSink,
QgsGeometry,
QgsMessageLog,
QgsProcessingUtils)
Expand Down Expand Up @@ -101,7 +102,7 @@ def processAlgorithm(self, parameters, context, feedback):
featToEliminate.append(aFeat)
else:
# write the others to output
writer.addFeature(aFeat)
writer.addFeature(aFeat, QgsFeatureSink.FastInsert)

# Delete all features to eliminate in processLayer
processLayer = output.layer
Expand Down Expand Up @@ -207,4 +208,4 @@ def processAlgorithm(self, parameters, context, feedback):
raise GeoAlgorithmExecutionException(self.tr('Could not commit changes'))

for feature in featNotEliminated:
writer.addFeature(feature)
writer.addFeature(feature, QgsFeatureSink.FastInsert)
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/EquivalentNumField.py
Expand Up @@ -28,6 +28,7 @@
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsField,
QgsFeature,
QgsFeatureSink,
QgsApplication,
QgsProcessingUtils)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
Expand Down Expand Up @@ -90,6 +91,6 @@ def processAlgorithm(self, parameters, context, feedback):

atMap.append(classes[clazz])
outFeat.setAttributes(atMap)
writer.addFeature(outFeat)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)

del writer
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ExecuteSQL.py
Expand Up @@ -157,6 +157,6 @@ def processAlgorithm(self, parameters, context, feedback):
outFeat.setAttributes(inFeat.attributes())
if geometry_type != 1:
outFeat.setGeometry(inFeat.geometry())
writer.addFeature(outFeat)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
del writer

1 comment on commit 81855a7

@haubourg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.