Skip to content

Commit ce0d966

Browse files
committed
Merge pull request #2452 from nyalldawson/processing_select
[processing] speedup some QGIS algorithms
2 parents d8f5dbe + a10936f commit ce0d966

File tree

4 files changed

+34
-44
lines changed

4 files changed

+34
-44
lines changed

python/plugins/processing/algs/qgis/PointsInPolygon.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,25 @@ def processAlgorithm(self, progress):
7676
geom = QgsGeometry()
7777

7878
current = 0
79-
hasIntersections = False
8079

8180
features = vector.features(polyLayer)
8281
total = 100.0 / float(len(features))
8382
for ftPoly in features:
8483
geom = ftPoly.geometry()
84+
engine = QgsGeometry.createGeometryEngine(geom.geometry())
85+
engine.prepareGeometry()
86+
8587
attrs = ftPoly.attributes()
8688

8789
count = 0
88-
hasIntersections = False
8990
points = spatialIndex.intersects(geom.boundingBox())
9091
if len(points) > 0:
91-
hasIntersections = True
92-
93-
if hasIntersections:
94-
for i in points:
95-
request = QgsFeatureRequest().setFilterFid(i)
96-
ftPoint = pointLayer.getFeatures(request).next()
97-
tmpGeom = QgsGeometry(ftPoint.geometry())
98-
if geom.contains(tmpGeom):
92+
request = QgsFeatureRequest().setFilterFids(points)
93+
fit = pointLayer.getFeatures(request)
94+
ftPoint = QgsFeature()
95+
while fit.nextFeature(ftPoint):
96+
tmpGeom = ftPoint.geometry()
97+
if engine.contains(tmpGeom.geometry()):
9998
count += 1
10099

101100
outFeat.setGeometry(geom)

python/plugins/processing/algs/qgis/PointsInPolygonUnique.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,28 @@ def processAlgorithm(self, progress):
8080
geom = QgsGeometry()
8181

8282
current = 0
83-
hasIntersections = False
8483

8584
features = vector.features(polyLayer)
8685
total = 100.0 / float(len(features))
8786
for ftPoly in features:
8887
geom = ftPoly.geometry()
88+
engine = QgsGeometry.createGeometryEngine(geom.geometry())
89+
engine.prepareGeometry()
90+
8991
attrs = ftPoly.attributes()
9092

91-
classes = []
92-
hasIntersections = False
93+
classes = set()
9394
points = spatialIndex.intersects(geom.boundingBox())
9495
if len(points) > 0:
95-
hasIntersections = True
96-
97-
if hasIntersections:
98-
for i in points:
99-
request = QgsFeatureRequest().setFilterFid(i)
100-
ftPoint = pointLayer.getFeatures(request).next()
96+
request = QgsFeatureRequest().setFilterFids(points)
97+
fit = pointLayer.getFeatures(request)
98+
ftPoint = QgsFeature()
99+
while fit.nextFeature(ftPoint):
101100
tmpGeom = QgsGeometry(ftPoint.geometry())
102-
if geom.contains(tmpGeom):
101+
if engine.contains(tmpGeom.geometry()):
103102
clazz = ftPoint.attributes()[classFieldIndex]
104103
if clazz not in classes:
105-
classes.append(clazz)
104+
classes.add(clazz)
106105

107106
outFeat.setGeometry(geom)
108107
if idxCount == len(attrs):

python/plugins/processing/algs/qgis/PointsInPolygonWeighted.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,25 @@ def processAlgorithm(self, progress):
8686
geom = QgsGeometry()
8787

8888
current = 0
89-
hasIntersections = False
90-
9189
features = vector.features(polyLayer)
9290
total = 100.0 / float(len(features))
9391
for ftPoly in features:
9492
geom = ftPoly.geometry()
93+
engine = QgsGeometry.createGeometryEngine(geom.geometry())
94+
engine.prepareGeometry()
95+
9596
attrs = ftPoly.attributes()
9697

9798
count = 0
98-
hasIntersections = False
9999
points = spatialIndex.intersects(geom.boundingBox())
100100
if len(points) > 0:
101-
hasIntersections = True
102-
103-
if hasIntersections:
104101
progress.setText(unicode(len(points)))
105-
for i in points:
106-
request = QgsFeatureRequest().setFilterFid(i)
107-
ftPoint = pointLayer.getFeatures(request).next()
102+
request = QgsFeatureRequest().setFilterFids(points)
103+
fit = pointLayer.getFeatures(request)
104+
ftPoint = QgsFeature()
105+
while fit.nextFeature(ftPoint):
108106
tmpGeom = QgsGeometry(ftPoint.geometry())
109-
if geom.contains(tmpGeom):
107+
if engine.contains(tmpGeom.geometry()):
110108
weight = unicode(ftPoint.attributes()[fieldIdx])
111109
try:
112110
count += float(weight)

python/plugins/processing/algs/qgis/SelectByAttribute.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
__revision__ = '$Format:%H$'
2727

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

118-
expression = QgsExpression(expr)
119-
expression.prepare(fields)
120-
121-
features = vector.features(layer)
122-
123-
selected = []
124-
count = len(features)
125-
total = 100.0 / float(count)
126-
for count, f in enumerate(features):
127-
if expression.evaluate(f, fields):
128-
selected.append(f.id())
129-
progress.setPercentage(int(count * total))
118+
qExp = QgsExpression(expr)
119+
if not qExp.hasParserError():
120+
qReq = QgsFeatureRequest(qExp)
121+
else:
122+
raise GeoAlgorithmExecutionException(qExp.parserErrorString())
123+
selected = [f.id() for f in layer.getFeatures(qReq)]
130124

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

0 commit comments

Comments
 (0)