Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] Remove vector.spatialindex()
Use QgsProcessingUtils.createSpatialIndex() instead.
  • Loading branch information
nyalldawson committed May 2, 2017
1 parent bde1bf4 commit df1ead5
Show file tree
Hide file tree
Showing 23 changed files with 53 additions and 41 deletions.
2 changes: 1 addition & 1 deletion doc/api_break.dox
Expand Up @@ -2234,7 +2234,7 @@ object of type QgsProcessingFeedback, and will need to adapt their use of progre
- dataobjects.getLayerFromString() was removed. Use QgsProcessingUtils.mapLayerFromString() instead.
- vector.bufferedBoundingBox() was removed. Use QgsRectangle.grow() instead.
- vector.duplicateInMemory() was removed.

- vector.spatialindex() was removed. Use QgsProcessingUtils.createSpatialIndex() instead.

Triangulation {#qgis_api_break_3_0_Triangulation}
-------------
Expand Down
9 changes: 9 additions & 0 deletions python/core/processing/qgsprocessingutils.sip
Expand Up @@ -104,6 +104,15 @@ class QgsProcessingUtils
:rtype: long
%End

static QgsSpatialIndex createSpatialIndex( QgsVectorLayer *layer, const QgsProcessingContext &context );
%Docstring
Creates a spatial index for a layer, when
the settings from the supplied ``context`` are respected. E.g. if the
context is set to only use selected features, then calling this will
return an index containing only selected features in the layer.
:rtype: QgsSpatialIndex
%End

static QList< QVariant > uniqueValues( QgsVectorLayer *layer, int fieldIndex, const QgsProcessingContext &context );
%Docstring
Returns a list of unique values contained in a single field in a ``layer``, when
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Difference.py
Expand Up @@ -78,7 +78,7 @@ def processAlgorithm(self, context, feedback):
Difference.OUTPUT).getVectorWriter(layerA.fields(), geomType, layerA.crs(), context)

outFeat = QgsFeature()
index = vector.spatialindex(layerB)
index = QgsProcessingUtils.createSpatialIndex(layerB, context)
selectionA = QgsProcessingUtils.getFeatures(layerA, context)
total = 100.0 / QgsProcessingUtils.featureCount(layerA, context)
for current, inFeatA in enumerate(selectionA):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ExtractByLocation.py
Expand Up @@ -94,7 +94,7 @@ def processAlgorithm(self, context, feedback):
predicates = self.getParameterValue(self.PREDICATE)
precision = self.getParameterValue(self.PRECISION)

index = vector.spatialindex(layer)
index = QgsProcessingUtils.createSpatialIndex(layer, context)

output = self.getOutputFromName(self.OUTPUT)
writer = output.getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(), context)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/HubDistanceLines.py
Expand Up @@ -113,7 +113,7 @@ def processAlgorithm(self, context, feedback):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.LineString, layerPoints.crs(),
context)

index = vector.spatialindex(layerHubs)
index = QgsProcessingUtils.createSpatialIndex(layerHubs, context)

distance = QgsDistanceArea()
distance.setSourceCrs(layerPoints.crs())
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/HubDistancePoints.py
Expand Up @@ -113,7 +113,7 @@ def processAlgorithm(self, context, feedback):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.Point, layerPoints.crs(),
context)

index = vector.spatialindex(layerHubs)
index = QgsProcessingUtils.createSpatialIndex(layerHubs, context)

distance = QgsDistanceArea()
distance.setSourceCrs(layerPoints.crs())
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Intersection.py
Expand Up @@ -86,7 +86,7 @@ def processAlgorithm(self, context, feedback):
fields = vector.combineVectorFields(vlayerA, vlayerB)
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, geomType, vlayerA.crs(), context)
outFeat = QgsFeature()
index = vector.spatialindex(vlayerB)
index = QgsProcessingUtils.createSpatialIndex(vlayerB, context)
selectionA = QgsProcessingUtils.getFeatures(vlayerA, context)
total = 100.0 / QgsProcessingUtils.featureCount(vlayerA, context)
for current, inFeatA in enumerate(selectionA):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/LinesIntersection.py
Expand Up @@ -108,7 +108,7 @@ def processAlgorithm(self, context, feedback):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldListA, QgsWkbTypes.Point, layerA.crs(),
context)

spatialIndex = vector.spatialindex(layerB)
spatialIndex = QgsProcessingUtils.createSpatialIndex(layerB, context)

outFeat = QgsFeature()
features = QgsProcessingUtils.getFeatures(layerA, context)
Expand Down
Expand Up @@ -86,7 +86,7 @@ def processAlgorithm(self, context, feedback):
layer = dataobjects.QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.POINTS), context)
output = self.getOutputValue(self.OUTPUT)

spatialIndex = vector.spatialindex(layer)
spatialIndex = QgsProcessingUtils.createSpatialIndex(layer, context)

neighbour = QgsFeature()
distance = QgsDistanceArea()
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/PointDistance.py
Expand Up @@ -125,7 +125,7 @@ def linearMatrix(self, context, inLayer, inField, targetLayer, targetField,
else:
self.writer.addRecord(['InputID', 'MEAN', 'STDDEV', 'MIN', 'MAX'])

index = vector.spatialindex(targetLayer)
index = QgsProcessingUtils.createSpatialIndex(targetLayer, context)

inIdx = inLayer.fields().lookupField(inField)
outIdx = targetLayer.fields().lookupField(targetField)
Expand Down Expand Up @@ -164,7 +164,7 @@ def linearMatrix(self, context, inLayer, inField, targetLayer, targetField,

def regularMatrix(self, context, inLayer, inField, targetLayer, targetField,
nPoints, feedback):
index = vector.spatialindex(targetLayer)
index = QgsProcessingUtils.createSpatialIndex(targetLayer, context)

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

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/PointsInPolygon.py
Expand Up @@ -83,7 +83,7 @@ def processAlgorithm(self, context, feedback):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(), polyLayer.wkbType(),
polyLayer.crs(), context)

spatialIndex = vector.spatialindex(pointLayer)
spatialIndex = QgsProcessingUtils.createSpatialIndex(pointLayer, context)

ftPoly = QgsFeature()
ftPoint = QgsFeature()
Expand Down
Expand Up @@ -90,7 +90,7 @@ def processAlgorithm(self, context, feedback):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(), polyLayer.wkbType(),
polyLayer.crs(), context)

spatialIndex = vector.spatialindex(pointLayer)
spatialIndex = QgsProcessingUtils.createSpatialIndex(pointLayer, context)

ftPoint = QgsFeature()
outFeat = QgsFeature()
Expand Down
Expand Up @@ -96,7 +96,7 @@ def processAlgorithm(self, context, feedback):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(), polyLayer.wkbType(),
polyLayer.crs(), context)

spatialIndex = vector.spatialindex(pointLayer)
spatialIndex = QgsProcessingUtils.createSpatialIndex(pointLayer, context)

ftPoint = QgsFeature()
outFeat = QgsFeature()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/RandomPointsLayer.py
Expand Up @@ -78,7 +78,7 @@ def processAlgorithm(self, context, feedback):
minDistance = float(self.getParameterValue(self.MIN_DISTANCE))

bbox = layer.extent()
idxLayer = vector.spatialindex(layer)
idxLayer = QgsProcessingUtils.createSpatialIndex(layer, context)

fields = QgsFields()
fields.append(QgsField('id', QVariant.Int, '', 10, 0))
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/SelectByLocation.py
Expand Up @@ -104,7 +104,7 @@ def processAlgorithm(self, context, feedback):

oldSelection = set(inputLayer.selectedFeatureIds())
inputLayer.removeSelection()
index = vector.spatialindex(inputLayer)
index = QgsProcessingUtils.createSpatialIndex(inputLayer, context)

if 'disjoint' in predicates:
disjoinSet = []
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/SpatialJoin.py
Expand Up @@ -154,7 +154,7 @@ def processAlgorithm(self, context, feedback):
inFeatB = QgsFeature()
inGeom = QgsGeometry()

index = vector.spatialindex(join)
index = QgsProcessingUtils.createSpatialIndex(join, context)

mapP2 = dict()
features = QgsProcessingUtils.getFeatures(join, context)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/SplitLinesWithLines.py
Expand Up @@ -85,7 +85,7 @@ def processAlgorithm(self, context, feedback):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList, QgsWkbTypes.LineString, layerA.crs(),
context)

spatialIndex = vector.spatialindex(layerB)
spatialIndex = QgsProcessingUtils.createSpatialIndex(layerB, context)

outFeat = QgsFeature()
features = QgsProcessingUtils.getFeatures(layerA, context)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/SumLines.py
Expand Up @@ -86,7 +86,7 @@ def processAlgorithm(self, context, feedback):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList.toList(), polyLayer.wkbType(),
polyLayer.crs(), context)

spatialIndex = vector.spatialindex(lineLayer)
spatialIndex = QgsProcessingUtils.createSpatialIndex(lineLayer, context)

ftLine = QgsFeature()
ftPoly = QgsFeature()
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/SymmetricalDifference.py
Expand Up @@ -81,8 +81,8 @@ def processAlgorithm(self, context, feedback):
featB = QgsFeature()
outFeat = QgsFeature()

indexA = vector.spatialindex(layerB)
indexB = vector.spatialindex(layerA)
indexA = QgsProcessingUtils.createSpatialIndex(layerB, context)
indexB = QgsProcessingUtils.createSpatialIndex(layerA, context)

featuresA = QgsProcessingUtils.getFeatures(layerA, context)
featuresB = QgsProcessingUtils.getFeatures(layerB, context)
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/Union.py
Expand Up @@ -88,8 +88,8 @@ def processAlgorithm(self, context, feedback):
inFeatA = QgsFeature()
inFeatB = QgsFeature()
outFeat = QgsFeature()
indexA = vector.spatialindex(vlayerB)
indexB = vector.spatialindex(vlayerA)
indexA = QgsProcessingUtils.createSpatialIndex(vlayerB, context)
indexB = QgsProcessingUtils.createSpatialIndex(vlayerA, context)

count = 0
nElement = 0
Expand Down
21 changes: 2 additions & 19 deletions python/plugins/processing/tools/vector.py
Expand Up @@ -40,15 +40,12 @@
import psycopg2
from osgeo import ogr

from qgis.PyQt.QtCore import QVariant, QCoreApplication
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsFields,
QgsField,
QgsGeometry,
QgsRectangle,
QgsWkbTypes,
QgsSpatialIndex,
QgsProject,
QgsMapLayer,
QgsVectorLayer,
QgsVectorFileWriter,
QgsDistanceArea,
Expand All @@ -57,8 +54,7 @@
QgsFeatureRequest,
QgsSettings,
QgsProcessingContext,
QgsProcessingUtils,
QgsMessageLog)
QgsProcessingUtils)

from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
Expand Down Expand Up @@ -172,19 +168,6 @@ def testForUniqueness(fieldList1, fieldList2):
return fieldList2


def spatialindex(layer):
"""Creates a spatial index for the passed vector layer.
"""
request = QgsFeatureRequest()
request.setSubsetOfAttributes([])
if ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED) \
and layer.selectedFeatureCount() > 0:
idx = QgsSpatialIndex(layer.getSelectedFeatures(request))
else:
idx = QgsSpatialIndex(layer.getFeatures(request))
return idx


def createUniqueFieldName(fieldName, fieldList):
def nextname(name):
num = 1
Expand Down
11 changes: 11 additions & 0 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -228,6 +228,17 @@ long QgsProcessingUtils::featureCount( QgsVectorLayer *layer, const QgsProcessin
return layer->featureCount();
}

QgsSpatialIndex QgsProcessingUtils::createSpatialIndex( QgsVectorLayer *layer, const QgsProcessingContext &context )
{
QgsFeatureRequest request;
request.setSubsetOfAttributes( QgsAttributeList() );
bool useSelection = context.flags() & QgsProcessingContext::UseSelectionIfPresent && layer->selectedFeatureCount() > 0;
if ( useSelection )
return QgsSpatialIndex( layer->getSelectedFeatures( request ) );
else
return QgsSpatialIndex( layer->getFeatures( request ) );
}

QList<QVariant> QgsProcessingUtils::uniqueValues( QgsVectorLayer *layer, int fieldIndex, const QgsProcessingContext &context )
{
if ( !layer )
Expand Down
9 changes: 9 additions & 0 deletions src/core/processing/qgsprocessingutils.h
Expand Up @@ -23,6 +23,7 @@
#include "qgsrasterlayer.h"
#include "qgsvectorlayer.h"
#include "qgsmessagelog.h"
#include "qgsspatialindex.h"

class QgsProject;
class QgsProcessingContext;
Expand Down Expand Up @@ -113,6 +114,14 @@ class CORE_EXPORT QgsProcessingUtils
*/
static long featureCount( QgsVectorLayer *layer, const QgsProcessingContext &context );

/**
* Creates a spatial index for a layer, when
* the settings from the supplied \a context are respected. E.g. if the
* context is set to only use selected features, then calling this will
* return an index containing only selected features in the layer.
*/
static QgsSpatialIndex createSpatialIndex( QgsVectorLayer *layer, const QgsProcessingContext &context );

/**
* Returns a list of unique values contained in a single field in a \a layer, when
* the settings from the supplied \a context are respected. E.g. if the
Expand Down

0 comments on commit df1ead5

Please sign in to comment.