Skip to content

Commit df1ead5

Browse files
committed
[processing] Remove vector.spatialindex()
Use QgsProcessingUtils.createSpatialIndex() instead.
1 parent bde1bf4 commit df1ead5

23 files changed

+53
-41
lines changed

doc/api_break.dox

+1-1
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,7 @@ object of type QgsProcessingFeedback, and will need to adapt their use of progre
22342234
- dataobjects.getLayerFromString() was removed. Use QgsProcessingUtils.mapLayerFromString() instead.
22352235
- vector.bufferedBoundingBox() was removed. Use QgsRectangle.grow() instead.
22362236
- vector.duplicateInMemory() was removed.
2237-
2237+
- vector.spatialindex() was removed. Use QgsProcessingUtils.createSpatialIndex() instead.
22382238

22392239
Triangulation {#qgis_api_break_3_0_Triangulation}
22402240
-------------

python/core/processing/qgsprocessingutils.sip

+9
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ class QgsProcessingUtils
104104
:rtype: long
105105
%End
106106

107+
static QgsSpatialIndex createSpatialIndex( QgsVectorLayer *layer, const QgsProcessingContext &context );
108+
%Docstring
109+
Creates a spatial index for a layer, when
110+
the settings from the supplied ``context`` are respected. E.g. if the
111+
context is set to only use selected features, then calling this will
112+
return an index containing only selected features in the layer.
113+
:rtype: QgsSpatialIndex
114+
%End
115+
107116
static QList< QVariant > uniqueValues( QgsVectorLayer *layer, int fieldIndex, const QgsProcessingContext &context );
108117
%Docstring
109118
Returns a list of unique values contained in a single field in a ``layer``, when

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def processAlgorithm(self, context, feedback):
7878
Difference.OUTPUT).getVectorWriter(layerA.fields(), geomType, layerA.crs(), context)
7979

8080
outFeat = QgsFeature()
81-
index = vector.spatialindex(layerB)
81+
index = QgsProcessingUtils.createSpatialIndex(layerB, context)
8282
selectionA = QgsProcessingUtils.getFeatures(layerA, context)
8383
total = 100.0 / QgsProcessingUtils.featureCount(layerA, context)
8484
for current, inFeatA in enumerate(selectionA):

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def processAlgorithm(self, context, feedback):
9494
predicates = self.getParameterValue(self.PREDICATE)
9595
precision = self.getParameterValue(self.PRECISION)
9696

97-
index = vector.spatialindex(layer)
97+
index = QgsProcessingUtils.createSpatialIndex(layer, context)
9898

9999
output = self.getOutputFromName(self.OUTPUT)
100100
writer = output.getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(), context)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def processAlgorithm(self, context, feedback):
113113
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.LineString, layerPoints.crs(),
114114
context)
115115

116-
index = vector.spatialindex(layerHubs)
116+
index = QgsProcessingUtils.createSpatialIndex(layerHubs, context)
117117

118118
distance = QgsDistanceArea()
119119
distance.setSourceCrs(layerPoints.crs())

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def processAlgorithm(self, context, feedback):
113113
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.Point, layerPoints.crs(),
114114
context)
115115

116-
index = vector.spatialindex(layerHubs)
116+
index = QgsProcessingUtils.createSpatialIndex(layerHubs, context)
117117

118118
distance = QgsDistanceArea()
119119
distance.setSourceCrs(layerPoints.crs())

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def processAlgorithm(self, context, feedback):
8686
fields = vector.combineVectorFields(vlayerA, vlayerB)
8787
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, geomType, vlayerA.crs(), context)
8888
outFeat = QgsFeature()
89-
index = vector.spatialindex(vlayerB)
89+
index = QgsProcessingUtils.createSpatialIndex(vlayerB, context)
9090
selectionA = QgsProcessingUtils.getFeatures(vlayerA, context)
9191
total = 100.0 / QgsProcessingUtils.featureCount(vlayerA, context)
9292
for current, inFeatA in enumerate(selectionA):

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def processAlgorithm(self, context, feedback):
108108
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldListA, QgsWkbTypes.Point, layerA.crs(),
109109
context)
110110

111-
spatialIndex = vector.spatialindex(layerB)
111+
spatialIndex = QgsProcessingUtils.createSpatialIndex(layerB, context)
112112

113113
outFeat = QgsFeature()
114114
features = QgsProcessingUtils.getFeatures(layerA, context)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def processAlgorithm(self, context, feedback):
8686
layer = dataobjects.QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.POINTS), context)
8787
output = self.getOutputValue(self.OUTPUT)
8888

89-
spatialIndex = vector.spatialindex(layer)
89+
spatialIndex = QgsProcessingUtils.createSpatialIndex(layer, context)
9090

9191
neighbour = QgsFeature()
9292
distance = QgsDistanceArea()

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def linearMatrix(self, context, inLayer, inField, targetLayer, targetField,
125125
else:
126126
self.writer.addRecord(['InputID', 'MEAN', 'STDDEV', 'MIN', 'MAX'])
127127

128-
index = vector.spatialindex(targetLayer)
128+
index = QgsProcessingUtils.createSpatialIndex(targetLayer, context)
129129

130130
inIdx = inLayer.fields().lookupField(inField)
131131
outIdx = targetLayer.fields().lookupField(targetField)
@@ -164,7 +164,7 @@ def linearMatrix(self, context, inLayer, inField, targetLayer, targetField,
164164

165165
def regularMatrix(self, context, inLayer, inField, targetLayer, targetField,
166166
nPoints, feedback):
167-
index = vector.spatialindex(targetLayer)
167+
index = QgsProcessingUtils.createSpatialIndex(targetLayer, context)
168168

169169
inIdx = inLayer.fields().lookupField(inField)
170170

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def processAlgorithm(self, context, feedback):
8383
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(), polyLayer.wkbType(),
8484
polyLayer.crs(), context)
8585

86-
spatialIndex = vector.spatialindex(pointLayer)
86+
spatialIndex = QgsProcessingUtils.createSpatialIndex(pointLayer, context)
8787

8888
ftPoly = QgsFeature()
8989
ftPoint = QgsFeature()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def processAlgorithm(self, context, feedback):
9090
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(), polyLayer.wkbType(),
9191
polyLayer.crs(), context)
9292

93-
spatialIndex = vector.spatialindex(pointLayer)
93+
spatialIndex = QgsProcessingUtils.createSpatialIndex(pointLayer, context)
9494

9595
ftPoint = QgsFeature()
9696
outFeat = QgsFeature()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def processAlgorithm(self, context, feedback):
9696
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(), polyLayer.wkbType(),
9797
polyLayer.crs(), context)
9898

99-
spatialIndex = vector.spatialindex(pointLayer)
99+
spatialIndex = QgsProcessingUtils.createSpatialIndex(pointLayer, context)
100100

101101
ftPoint = QgsFeature()
102102
outFeat = QgsFeature()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def processAlgorithm(self, context, feedback):
7878
minDistance = float(self.getParameterValue(self.MIN_DISTANCE))
7979

8080
bbox = layer.extent()
81-
idxLayer = vector.spatialindex(layer)
81+
idxLayer = QgsProcessingUtils.createSpatialIndex(layer, context)
8282

8383
fields = QgsFields()
8484
fields.append(QgsField('id', QVariant.Int, '', 10, 0))

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def processAlgorithm(self, context, feedback):
104104

105105
oldSelection = set(inputLayer.selectedFeatureIds())
106106
inputLayer.removeSelection()
107-
index = vector.spatialindex(inputLayer)
107+
index = QgsProcessingUtils.createSpatialIndex(inputLayer, context)
108108

109109
if 'disjoint' in predicates:
110110
disjoinSet = []

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def processAlgorithm(self, context, feedback):
154154
inFeatB = QgsFeature()
155155
inGeom = QgsGeometry()
156156

157-
index = vector.spatialindex(join)
157+
index = QgsProcessingUtils.createSpatialIndex(join, context)
158158

159159
mapP2 = dict()
160160
features = QgsProcessingUtils.getFeatures(join, context)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def processAlgorithm(self, context, feedback):
8585
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList, QgsWkbTypes.LineString, layerA.crs(),
8686
context)
8787

88-
spatialIndex = vector.spatialindex(layerB)
88+
spatialIndex = QgsProcessingUtils.createSpatialIndex(layerB, context)
8989

9090
outFeat = QgsFeature()
9191
features = QgsProcessingUtils.getFeatures(layerA, context)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def processAlgorithm(self, context, feedback):
8686
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList.toList(), polyLayer.wkbType(),
8787
polyLayer.crs(), context)
8888

89-
spatialIndex = vector.spatialindex(lineLayer)
89+
spatialIndex = QgsProcessingUtils.createSpatialIndex(lineLayer, context)
9090

9191
ftLine = QgsFeature()
9292
ftPoly = QgsFeature()

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ def processAlgorithm(self, context, feedback):
8181
featB = QgsFeature()
8282
outFeat = QgsFeature()
8383

84-
indexA = vector.spatialindex(layerB)
85-
indexB = vector.spatialindex(layerA)
84+
indexA = QgsProcessingUtils.createSpatialIndex(layerB, context)
85+
indexB = QgsProcessingUtils.createSpatialIndex(layerA, context)
8686

8787
featuresA = QgsProcessingUtils.getFeatures(layerA, context)
8888
featuresB = QgsProcessingUtils.getFeatures(layerB, context)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def processAlgorithm(self, context, feedback):
8888
inFeatA = QgsFeature()
8989
inFeatB = QgsFeature()
9090
outFeat = QgsFeature()
91-
indexA = vector.spatialindex(vlayerB)
92-
indexB = vector.spatialindex(vlayerA)
91+
indexA = QgsProcessingUtils.createSpatialIndex(vlayerB, context)
92+
indexB = QgsProcessingUtils.createSpatialIndex(vlayerA, context)
9393

9494
count = 0
9595
nElement = 0

python/plugins/processing/tools/vector.py

+2-19
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,12 @@
4040
import psycopg2
4141
from osgeo import ogr
4242

43-
from qgis.PyQt.QtCore import QVariant, QCoreApplication
43+
from qgis.PyQt.QtCore import QVariant
4444
from qgis.core import (QgsFields,
4545
QgsField,
4646
QgsGeometry,
47-
QgsRectangle,
4847
QgsWkbTypes,
4948
QgsSpatialIndex,
50-
QgsProject,
51-
QgsMapLayer,
5249
QgsVectorLayer,
5350
QgsVectorFileWriter,
5451
QgsDistanceArea,
@@ -57,8 +54,7 @@
5754
QgsFeatureRequest,
5855
QgsSettings,
5956
QgsProcessingContext,
60-
QgsProcessingUtils,
61-
QgsMessageLog)
57+
QgsProcessingUtils)
6258

6359
from processing.core.ProcessingConfig import ProcessingConfig
6460
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
@@ -172,19 +168,6 @@ def testForUniqueness(fieldList1, fieldList2):
172168
return fieldList2
173169

174170

175-
def spatialindex(layer):
176-
"""Creates a spatial index for the passed vector layer.
177-
"""
178-
request = QgsFeatureRequest()
179-
request.setSubsetOfAttributes([])
180-
if ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED) \
181-
and layer.selectedFeatureCount() > 0:
182-
idx = QgsSpatialIndex(layer.getSelectedFeatures(request))
183-
else:
184-
idx = QgsSpatialIndex(layer.getFeatures(request))
185-
return idx
186-
187-
188171
def createUniqueFieldName(fieldName, fieldList):
189172
def nextname(name):
190173
num = 1

src/core/processing/qgsprocessingutils.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,17 @@ long QgsProcessingUtils::featureCount( QgsVectorLayer *layer, const QgsProcessin
228228
return layer->featureCount();
229229
}
230230

231+
QgsSpatialIndex QgsProcessingUtils::createSpatialIndex( QgsVectorLayer *layer, const QgsProcessingContext &context )
232+
{
233+
QgsFeatureRequest request;
234+
request.setSubsetOfAttributes( QgsAttributeList() );
235+
bool useSelection = context.flags() & QgsProcessingContext::UseSelectionIfPresent && layer->selectedFeatureCount() > 0;
236+
if ( useSelection )
237+
return QgsSpatialIndex( layer->getSelectedFeatures( request ) );
238+
else
239+
return QgsSpatialIndex( layer->getFeatures( request ) );
240+
}
241+
231242
QList<QVariant> QgsProcessingUtils::uniqueValues( QgsVectorLayer *layer, int fieldIndex, const QgsProcessingContext &context )
232243
{
233244
if ( !layer )

src/core/processing/qgsprocessingutils.h

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgsrasterlayer.h"
2424
#include "qgsvectorlayer.h"
2525
#include "qgsmessagelog.h"
26+
#include "qgsspatialindex.h"
2627

2728
class QgsProject;
2829
class QgsProcessingContext;
@@ -113,6 +114,14 @@ class CORE_EXPORT QgsProcessingUtils
113114
*/
114115
static long featureCount( QgsVectorLayer *layer, const QgsProcessingContext &context );
115116

117+
/**
118+
* Creates a spatial index for a layer, when
119+
* the settings from the supplied \a context are respected. E.g. if the
120+
* context is set to only use selected features, then calling this will
121+
* return an index containing only selected features in the layer.
122+
*/
123+
static QgsSpatialIndex createSpatialIndex( QgsVectorLayer *layer, const QgsProcessingContext &context );
124+
116125
/**
117126
* Returns a list of unique values contained in a single field in a \a layer, when
118127
* the settings from the supplied \a context are respected. E.g. if the

0 commit comments

Comments
 (0)