mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-09 00:08:52 -04:00
[processing] Remove vector.spatialindex()
Use QgsProcessingUtils.createSpatialIndex() instead.
This commit is contained in:
parent
bde1bf457b
commit
df1ead5ed4
@ -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.
|
- dataobjects.getLayerFromString() was removed. Use QgsProcessingUtils.mapLayerFromString() instead.
|
||||||
- vector.bufferedBoundingBox() was removed. Use QgsRectangle.grow() instead.
|
- vector.bufferedBoundingBox() was removed. Use QgsRectangle.grow() instead.
|
||||||
- vector.duplicateInMemory() was removed.
|
- vector.duplicateInMemory() was removed.
|
||||||
|
- vector.spatialindex() was removed. Use QgsProcessingUtils.createSpatialIndex() instead.
|
||||||
|
|
||||||
Triangulation {#qgis_api_break_3_0_Triangulation}
|
Triangulation {#qgis_api_break_3_0_Triangulation}
|
||||||
-------------
|
-------------
|
||||||
|
@ -104,6 +104,15 @@ class QgsProcessingUtils
|
|||||||
:rtype: long
|
:rtype: long
|
||||||
%End
|
%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 );
|
static QList< QVariant > uniqueValues( QgsVectorLayer *layer, int fieldIndex, const QgsProcessingContext &context );
|
||||||
%Docstring
|
%Docstring
|
||||||
Returns a list of unique values contained in a single field in a ``layer``, when
|
Returns a list of unique values contained in a single field in a ``layer``, when
|
||||||
|
@ -78,7 +78,7 @@ class Difference(GeoAlgorithm):
|
|||||||
Difference.OUTPUT).getVectorWriter(layerA.fields(), geomType, layerA.crs(), context)
|
Difference.OUTPUT).getVectorWriter(layerA.fields(), geomType, layerA.crs(), context)
|
||||||
|
|
||||||
outFeat = QgsFeature()
|
outFeat = QgsFeature()
|
||||||
index = vector.spatialindex(layerB)
|
index = QgsProcessingUtils.createSpatialIndex(layerB, context)
|
||||||
selectionA = QgsProcessingUtils.getFeatures(layerA, context)
|
selectionA = QgsProcessingUtils.getFeatures(layerA, context)
|
||||||
total = 100.0 / QgsProcessingUtils.featureCount(layerA, context)
|
total = 100.0 / QgsProcessingUtils.featureCount(layerA, context)
|
||||||
for current, inFeatA in enumerate(selectionA):
|
for current, inFeatA in enumerate(selectionA):
|
||||||
|
@ -94,7 +94,7 @@ class ExtractByLocation(GeoAlgorithm):
|
|||||||
predicates = self.getParameterValue(self.PREDICATE)
|
predicates = self.getParameterValue(self.PREDICATE)
|
||||||
precision = self.getParameterValue(self.PRECISION)
|
precision = self.getParameterValue(self.PRECISION)
|
||||||
|
|
||||||
index = vector.spatialindex(layer)
|
index = QgsProcessingUtils.createSpatialIndex(layer, context)
|
||||||
|
|
||||||
output = self.getOutputFromName(self.OUTPUT)
|
output = self.getOutputFromName(self.OUTPUT)
|
||||||
writer = output.getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(), context)
|
writer = output.getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(), context)
|
||||||
|
@ -113,7 +113,7 @@ class HubDistanceLines(GeoAlgorithm):
|
|||||||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.LineString, layerPoints.crs(),
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.LineString, layerPoints.crs(),
|
||||||
context)
|
context)
|
||||||
|
|
||||||
index = vector.spatialindex(layerHubs)
|
index = QgsProcessingUtils.createSpatialIndex(layerHubs, context)
|
||||||
|
|
||||||
distance = QgsDistanceArea()
|
distance = QgsDistanceArea()
|
||||||
distance.setSourceCrs(layerPoints.crs())
|
distance.setSourceCrs(layerPoints.crs())
|
||||||
|
@ -113,7 +113,7 @@ class HubDistancePoints(GeoAlgorithm):
|
|||||||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.Point, layerPoints.crs(),
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.Point, layerPoints.crs(),
|
||||||
context)
|
context)
|
||||||
|
|
||||||
index = vector.spatialindex(layerHubs)
|
index = QgsProcessingUtils.createSpatialIndex(layerHubs, context)
|
||||||
|
|
||||||
distance = QgsDistanceArea()
|
distance = QgsDistanceArea()
|
||||||
distance.setSourceCrs(layerPoints.crs())
|
distance.setSourceCrs(layerPoints.crs())
|
||||||
|
@ -86,7 +86,7 @@ class Intersection(GeoAlgorithm):
|
|||||||
fields = vector.combineVectorFields(vlayerA, vlayerB)
|
fields = vector.combineVectorFields(vlayerA, vlayerB)
|
||||||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, geomType, vlayerA.crs(), context)
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, geomType, vlayerA.crs(), context)
|
||||||
outFeat = QgsFeature()
|
outFeat = QgsFeature()
|
||||||
index = vector.spatialindex(vlayerB)
|
index = QgsProcessingUtils.createSpatialIndex(vlayerB, context)
|
||||||
selectionA = QgsProcessingUtils.getFeatures(vlayerA, context)
|
selectionA = QgsProcessingUtils.getFeatures(vlayerA, context)
|
||||||
total = 100.0 / QgsProcessingUtils.featureCount(vlayerA, context)
|
total = 100.0 / QgsProcessingUtils.featureCount(vlayerA, context)
|
||||||
for current, inFeatA in enumerate(selectionA):
|
for current, inFeatA in enumerate(selectionA):
|
||||||
|
@ -108,7 +108,7 @@ class LinesIntersection(GeoAlgorithm):
|
|||||||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldListA, QgsWkbTypes.Point, layerA.crs(),
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldListA, QgsWkbTypes.Point, layerA.crs(),
|
||||||
context)
|
context)
|
||||||
|
|
||||||
spatialIndex = vector.spatialindex(layerB)
|
spatialIndex = QgsProcessingUtils.createSpatialIndex(layerB, context)
|
||||||
|
|
||||||
outFeat = QgsFeature()
|
outFeat = QgsFeature()
|
||||||
features = QgsProcessingUtils.getFeatures(layerA, context)
|
features = QgsProcessingUtils.getFeatures(layerA, context)
|
||||||
|
@ -86,7 +86,7 @@ class NearestNeighbourAnalysis(GeoAlgorithm):
|
|||||||
layer = dataobjects.QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.POINTS), context)
|
layer = dataobjects.QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.POINTS), context)
|
||||||
output = self.getOutputValue(self.OUTPUT)
|
output = self.getOutputValue(self.OUTPUT)
|
||||||
|
|
||||||
spatialIndex = vector.spatialindex(layer)
|
spatialIndex = QgsProcessingUtils.createSpatialIndex(layer, context)
|
||||||
|
|
||||||
neighbour = QgsFeature()
|
neighbour = QgsFeature()
|
||||||
distance = QgsDistanceArea()
|
distance = QgsDistanceArea()
|
||||||
|
@ -125,7 +125,7 @@ class PointDistance(GeoAlgorithm):
|
|||||||
else:
|
else:
|
||||||
self.writer.addRecord(['InputID', 'MEAN', 'STDDEV', 'MIN', 'MAX'])
|
self.writer.addRecord(['InputID', 'MEAN', 'STDDEV', 'MIN', 'MAX'])
|
||||||
|
|
||||||
index = vector.spatialindex(targetLayer)
|
index = QgsProcessingUtils.createSpatialIndex(targetLayer, context)
|
||||||
|
|
||||||
inIdx = inLayer.fields().lookupField(inField)
|
inIdx = inLayer.fields().lookupField(inField)
|
||||||
outIdx = targetLayer.fields().lookupField(targetField)
|
outIdx = targetLayer.fields().lookupField(targetField)
|
||||||
@ -164,7 +164,7 @@ class PointDistance(GeoAlgorithm):
|
|||||||
|
|
||||||
def regularMatrix(self, context, inLayer, inField, targetLayer, targetField,
|
def regularMatrix(self, context, inLayer, inField, targetLayer, targetField,
|
||||||
nPoints, feedback):
|
nPoints, feedback):
|
||||||
index = vector.spatialindex(targetLayer)
|
index = QgsProcessingUtils.createSpatialIndex(targetLayer, context)
|
||||||
|
|
||||||
inIdx = inLayer.fields().lookupField(inField)
|
inIdx = inLayer.fields().lookupField(inField)
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ class PointsInPolygon(GeoAlgorithm):
|
|||||||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(), polyLayer.wkbType(),
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(), polyLayer.wkbType(),
|
||||||
polyLayer.crs(), context)
|
polyLayer.crs(), context)
|
||||||
|
|
||||||
spatialIndex = vector.spatialindex(pointLayer)
|
spatialIndex = QgsProcessingUtils.createSpatialIndex(pointLayer, context)
|
||||||
|
|
||||||
ftPoly = QgsFeature()
|
ftPoly = QgsFeature()
|
||||||
ftPoint = QgsFeature()
|
ftPoint = QgsFeature()
|
||||||
|
@ -90,7 +90,7 @@ class PointsInPolygonUnique(GeoAlgorithm):
|
|||||||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(), polyLayer.wkbType(),
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(), polyLayer.wkbType(),
|
||||||
polyLayer.crs(), context)
|
polyLayer.crs(), context)
|
||||||
|
|
||||||
spatialIndex = vector.spatialindex(pointLayer)
|
spatialIndex = QgsProcessingUtils.createSpatialIndex(pointLayer, context)
|
||||||
|
|
||||||
ftPoint = QgsFeature()
|
ftPoint = QgsFeature()
|
||||||
outFeat = QgsFeature()
|
outFeat = QgsFeature()
|
||||||
|
@ -96,7 +96,7 @@ class PointsInPolygonWeighted(GeoAlgorithm):
|
|||||||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(), polyLayer.wkbType(),
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(), polyLayer.wkbType(),
|
||||||
polyLayer.crs(), context)
|
polyLayer.crs(), context)
|
||||||
|
|
||||||
spatialIndex = vector.spatialindex(pointLayer)
|
spatialIndex = QgsProcessingUtils.createSpatialIndex(pointLayer, context)
|
||||||
|
|
||||||
ftPoint = QgsFeature()
|
ftPoint = QgsFeature()
|
||||||
outFeat = QgsFeature()
|
outFeat = QgsFeature()
|
||||||
|
@ -78,7 +78,7 @@ class RandomPointsLayer(GeoAlgorithm):
|
|||||||
minDistance = float(self.getParameterValue(self.MIN_DISTANCE))
|
minDistance = float(self.getParameterValue(self.MIN_DISTANCE))
|
||||||
|
|
||||||
bbox = layer.extent()
|
bbox = layer.extent()
|
||||||
idxLayer = vector.spatialindex(layer)
|
idxLayer = QgsProcessingUtils.createSpatialIndex(layer, context)
|
||||||
|
|
||||||
fields = QgsFields()
|
fields = QgsFields()
|
||||||
fields.append(QgsField('id', QVariant.Int, '', 10, 0))
|
fields.append(QgsField('id', QVariant.Int, '', 10, 0))
|
||||||
|
@ -104,7 +104,7 @@ class SelectByLocation(GeoAlgorithm):
|
|||||||
|
|
||||||
oldSelection = set(inputLayer.selectedFeatureIds())
|
oldSelection = set(inputLayer.selectedFeatureIds())
|
||||||
inputLayer.removeSelection()
|
inputLayer.removeSelection()
|
||||||
index = vector.spatialindex(inputLayer)
|
index = QgsProcessingUtils.createSpatialIndex(inputLayer, context)
|
||||||
|
|
||||||
if 'disjoint' in predicates:
|
if 'disjoint' in predicates:
|
||||||
disjoinSet = []
|
disjoinSet = []
|
||||||
|
@ -154,7 +154,7 @@ class SpatialJoin(GeoAlgorithm):
|
|||||||
inFeatB = QgsFeature()
|
inFeatB = QgsFeature()
|
||||||
inGeom = QgsGeometry()
|
inGeom = QgsGeometry()
|
||||||
|
|
||||||
index = vector.spatialindex(join)
|
index = QgsProcessingUtils.createSpatialIndex(join, context)
|
||||||
|
|
||||||
mapP2 = dict()
|
mapP2 = dict()
|
||||||
features = QgsProcessingUtils.getFeatures(join, context)
|
features = QgsProcessingUtils.getFeatures(join, context)
|
||||||
|
@ -85,7 +85,7 @@ class SplitLinesWithLines(GeoAlgorithm):
|
|||||||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList, QgsWkbTypes.LineString, layerA.crs(),
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList, QgsWkbTypes.LineString, layerA.crs(),
|
||||||
context)
|
context)
|
||||||
|
|
||||||
spatialIndex = vector.spatialindex(layerB)
|
spatialIndex = QgsProcessingUtils.createSpatialIndex(layerB, context)
|
||||||
|
|
||||||
outFeat = QgsFeature()
|
outFeat = QgsFeature()
|
||||||
features = QgsProcessingUtils.getFeatures(layerA, context)
|
features = QgsProcessingUtils.getFeatures(layerA, context)
|
||||||
|
@ -86,7 +86,7 @@ class SumLines(GeoAlgorithm):
|
|||||||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList.toList(), polyLayer.wkbType(),
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList.toList(), polyLayer.wkbType(),
|
||||||
polyLayer.crs(), context)
|
polyLayer.crs(), context)
|
||||||
|
|
||||||
spatialIndex = vector.spatialindex(lineLayer)
|
spatialIndex = QgsProcessingUtils.createSpatialIndex(lineLayer, context)
|
||||||
|
|
||||||
ftLine = QgsFeature()
|
ftLine = QgsFeature()
|
||||||
ftPoly = QgsFeature()
|
ftPoly = QgsFeature()
|
||||||
|
@ -81,8 +81,8 @@ class SymmetricalDifference(GeoAlgorithm):
|
|||||||
featB = QgsFeature()
|
featB = QgsFeature()
|
||||||
outFeat = QgsFeature()
|
outFeat = QgsFeature()
|
||||||
|
|
||||||
indexA = vector.spatialindex(layerB)
|
indexA = QgsProcessingUtils.createSpatialIndex(layerB, context)
|
||||||
indexB = vector.spatialindex(layerA)
|
indexB = QgsProcessingUtils.createSpatialIndex(layerA, context)
|
||||||
|
|
||||||
featuresA = QgsProcessingUtils.getFeatures(layerA, context)
|
featuresA = QgsProcessingUtils.getFeatures(layerA, context)
|
||||||
featuresB = QgsProcessingUtils.getFeatures(layerB, context)
|
featuresB = QgsProcessingUtils.getFeatures(layerB, context)
|
||||||
|
@ -88,8 +88,8 @@ class Union(GeoAlgorithm):
|
|||||||
inFeatA = QgsFeature()
|
inFeatA = QgsFeature()
|
||||||
inFeatB = QgsFeature()
|
inFeatB = QgsFeature()
|
||||||
outFeat = QgsFeature()
|
outFeat = QgsFeature()
|
||||||
indexA = vector.spatialindex(vlayerB)
|
indexA = QgsProcessingUtils.createSpatialIndex(vlayerB, context)
|
||||||
indexB = vector.spatialindex(vlayerA)
|
indexB = QgsProcessingUtils.createSpatialIndex(vlayerA, context)
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
nElement = 0
|
nElement = 0
|
||||||
|
@ -40,15 +40,12 @@ import uuid
|
|||||||
import psycopg2
|
import psycopg2
|
||||||
from osgeo import ogr
|
from osgeo import ogr
|
||||||
|
|
||||||
from qgis.PyQt.QtCore import QVariant, QCoreApplication
|
from qgis.PyQt.QtCore import QVariant
|
||||||
from qgis.core import (QgsFields,
|
from qgis.core import (QgsFields,
|
||||||
QgsField,
|
QgsField,
|
||||||
QgsGeometry,
|
QgsGeometry,
|
||||||
QgsRectangle,
|
|
||||||
QgsWkbTypes,
|
QgsWkbTypes,
|
||||||
QgsSpatialIndex,
|
QgsSpatialIndex,
|
||||||
QgsProject,
|
|
||||||
QgsMapLayer,
|
|
||||||
QgsVectorLayer,
|
QgsVectorLayer,
|
||||||
QgsVectorFileWriter,
|
QgsVectorFileWriter,
|
||||||
QgsDistanceArea,
|
QgsDistanceArea,
|
||||||
@ -57,8 +54,7 @@ from qgis.core import (QgsFields,
|
|||||||
QgsFeatureRequest,
|
QgsFeatureRequest,
|
||||||
QgsSettings,
|
QgsSettings,
|
||||||
QgsProcessingContext,
|
QgsProcessingContext,
|
||||||
QgsProcessingUtils,
|
QgsProcessingUtils)
|
||||||
QgsMessageLog)
|
|
||||||
|
|
||||||
from processing.core.ProcessingConfig import ProcessingConfig
|
from processing.core.ProcessingConfig import ProcessingConfig
|
||||||
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
||||||
@ -172,19 +168,6 @@ def testForUniqueness(fieldList1, fieldList2):
|
|||||||
return 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 createUniqueFieldName(fieldName, fieldList):
|
||||||
def nextname(name):
|
def nextname(name):
|
||||||
num = 1
|
num = 1
|
||||||
|
@ -228,6 +228,17 @@ long QgsProcessingUtils::featureCount( QgsVectorLayer *layer, const QgsProcessin
|
|||||||
return layer->featureCount();
|
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 )
|
QList<QVariant> QgsProcessingUtils::uniqueValues( QgsVectorLayer *layer, int fieldIndex, const QgsProcessingContext &context )
|
||||||
{
|
{
|
||||||
if ( !layer )
|
if ( !layer )
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
#include "qgsrasterlayer.h"
|
#include "qgsrasterlayer.h"
|
||||||
#include "qgsvectorlayer.h"
|
#include "qgsvectorlayer.h"
|
||||||
#include "qgsmessagelog.h"
|
#include "qgsmessagelog.h"
|
||||||
|
#include "qgsspatialindex.h"
|
||||||
|
|
||||||
class QgsProject;
|
class QgsProject;
|
||||||
class QgsProcessingContext;
|
class QgsProcessingContext;
|
||||||
@ -113,6 +114,14 @@ class CORE_EXPORT QgsProcessingUtils
|
|||||||
*/
|
*/
|
||||||
static long featureCount( QgsVectorLayer *layer, const QgsProcessingContext &context );
|
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
|
* 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
|
* the settings from the supplied \a context are respected. E.g. if the
|
||||||
|
Loading…
x
Reference in New Issue
Block a user