Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 ce0d966
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 44 deletions.
19 changes: 9 additions & 10 deletions python/plugins/processing/algs/qgis/PointsInPolygon.py
Expand Up @@ -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)
Expand Down
21 changes: 10 additions & 11 deletions python/plugins/processing/algs/qgis/PointsInPolygonUnique.py
Expand Up @@ -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):
Expand Down
18 changes: 8 additions & 10 deletions python/plugins/processing/algs/qgis/PointsInPolygonWeighted.py
Expand Up @@ -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)
Expand Down
20 changes: 7 additions & 13 deletions python/plugins/processing/algs/qgis/SelectByAttribute.py
Expand Up @@ -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
Expand Down Expand Up @@ -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.