Skip to content
Permalink
Browse files

Merge pull request #2452 from nyalldawson/processing_select

[processing] speedup some QGIS algorithms
  • Loading branch information
alexbruy committed Nov 13, 2015
2 parents d8f5dbe + a10936f commit ce0d966b2914bb8d915e2ffac98336b0cd8479d5
@@ -76,26 +76,25 @@ def processAlgorithm(self, progress):
geom = QgsGeometry()

current = 0
hasIntersections = False

features = vector.features(polyLayer)
total = 100.0 / float(len(features))
for ftPoly in features:
geom = ftPoly.geometry()
engine = QgsGeometry.createGeometryEngine(geom.geometry())
engine.prepareGeometry()

attrs = ftPoly.attributes()

count = 0
hasIntersections = False
points = spatialIndex.intersects(geom.boundingBox())
if len(points) > 0:
hasIntersections = True

if hasIntersections:
for i in points:
request = QgsFeatureRequest().setFilterFid(i)
ftPoint = pointLayer.getFeatures(request).next()
tmpGeom = QgsGeometry(ftPoint.geometry())
if geom.contains(tmpGeom):
request = QgsFeatureRequest().setFilterFids(points)
fit = pointLayer.getFeatures(request)
ftPoint = QgsFeature()
while fit.nextFeature(ftPoint):
tmpGeom = ftPoint.geometry()
if engine.contains(tmpGeom.geometry()):
count += 1

outFeat.setGeometry(geom)
@@ -80,29 +80,28 @@ def processAlgorithm(self, progress):
geom = QgsGeometry()

current = 0
hasIntersections = False

features = vector.features(polyLayer)
total = 100.0 / float(len(features))
for ftPoly in features:
geom = ftPoly.geometry()
engine = QgsGeometry.createGeometryEngine(geom.geometry())
engine.prepareGeometry()

attrs = ftPoly.attributes()

classes = []
hasIntersections = False
classes = set()
points = spatialIndex.intersects(geom.boundingBox())
if len(points) > 0:
hasIntersections = True

if hasIntersections:
for i in points:
request = QgsFeatureRequest().setFilterFid(i)
ftPoint = pointLayer.getFeatures(request).next()
request = QgsFeatureRequest().setFilterFids(points)
fit = pointLayer.getFeatures(request)
ftPoint = QgsFeature()
while fit.nextFeature(ftPoint):
tmpGeom = QgsGeometry(ftPoint.geometry())
if geom.contains(tmpGeom):
if engine.contains(tmpGeom.geometry()):
clazz = ftPoint.attributes()[classFieldIndex]
if clazz not in classes:
classes.append(clazz)
classes.add(clazz)

outFeat.setGeometry(geom)
if idxCount == len(attrs):
@@ -86,27 +86,25 @@ def processAlgorithm(self, progress):
geom = QgsGeometry()

current = 0
hasIntersections = False

features = vector.features(polyLayer)
total = 100.0 / float(len(features))
for ftPoly in features:
geom = ftPoly.geometry()
engine = QgsGeometry.createGeometryEngine(geom.geometry())
engine.prepareGeometry()

attrs = ftPoly.attributes()

count = 0
hasIntersections = False
points = spatialIndex.intersects(geom.boundingBox())
if len(points) > 0:
hasIntersections = True

if hasIntersections:
progress.setText(unicode(len(points)))
for i in points:
request = QgsFeatureRequest().setFilterFid(i)
ftPoint = pointLayer.getFeatures(request).next()
request = QgsFeatureRequest().setFilterFids(points)
fit = pointLayer.getFeatures(request)
ftPoint = QgsFeature()
while fit.nextFeature(ftPoint):
tmpGeom = QgsGeometry(ftPoint.geometry())
if geom.contains(tmpGeom):
if engine.contains(tmpGeom.geometry()):
weight = unicode(ftPoint.attributes()[fieldIdx])
try:
count += float(weight)
@@ -26,7 +26,7 @@
__revision__ = '$Format:%H$'

from PyQt4.QtCore import QVariant
from qgis.core import QgsExpression
from qgis.core import QgsExpression, QgsFeatureRequest
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
@@ -115,18 +115,12 @@ def processAlgorithm(self, progress):
raise GeoAlgorithmExecutionException(
self.tr('Unsupported field type "%s"' % fields[idx].typeName()))

expression = QgsExpression(expr)
expression.prepare(fields)

features = vector.features(layer)

selected = []
count = len(features)
total = 100.0 / float(count)
for count, f in enumerate(features):
if expression.evaluate(f, fields):
selected.append(f.id())
progress.setPercentage(int(count * total))
qExp = QgsExpression(expr)
if not qExp.hasParserError():
qReq = QgsFeatureRequest(qExp)
else:
raise GeoAlgorithmExecutionException(qExp.parserErrorString())
selected = [f.id() for f in layer.getFeatures(qReq)]

layer.setSelectedFeatures(selected)
self.setOutputValue(self.OUTPUT, fileName)

0 comments on commit ce0d966

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