mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -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.
|
||||
- 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}
|
||||
-------------
|
||||
|
@ -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
|
||||
|
@ -78,7 +78,7 @@ class Difference(GeoAlgorithm):
|
||||
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):
|
||||
|
@ -94,7 +94,7 @@ class ExtractByLocation(GeoAlgorithm):
|
||||
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)
|
||||
|
@ -113,7 +113,7 @@ class HubDistanceLines(GeoAlgorithm):
|
||||
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())
|
||||
|
@ -113,7 +113,7 @@ class HubDistancePoints(GeoAlgorithm):
|
||||
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())
|
||||
|
@ -86,7 +86,7 @@ class Intersection(GeoAlgorithm):
|
||||
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):
|
||||
|
@ -108,7 +108,7 @@ class LinesIntersection(GeoAlgorithm):
|
||||
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)
|
||||
|
@ -86,7 +86,7 @@ class NearestNeighbourAnalysis(GeoAlgorithm):
|
||||
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()
|
||||
|
@ -125,7 +125,7 @@ class PointDistance(GeoAlgorithm):
|
||||
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)
|
||||
@ -164,7 +164,7 @@ class PointDistance(GeoAlgorithm):
|
||||
|
||||
def regularMatrix(self, context, inLayer, inField, targetLayer, targetField,
|
||||
nPoints, feedback):
|
||||
index = vector.spatialindex(targetLayer)
|
||||
index = QgsProcessingUtils.createSpatialIndex(targetLayer, context)
|
||||
|
||||
inIdx = inLayer.fields().lookupField(inField)
|
||||
|
||||
|
@ -83,7 +83,7 @@ class PointsInPolygon(GeoAlgorithm):
|
||||
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()
|
||||
|
@ -90,7 +90,7 @@ class PointsInPolygonUnique(GeoAlgorithm):
|
||||
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()
|
||||
|
@ -96,7 +96,7 @@ class PointsInPolygonWeighted(GeoAlgorithm):
|
||||
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()
|
||||
|
@ -78,7 +78,7 @@ class RandomPointsLayer(GeoAlgorithm):
|
||||
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))
|
||||
|
@ -104,7 +104,7 @@ class SelectByLocation(GeoAlgorithm):
|
||||
|
||||
oldSelection = set(inputLayer.selectedFeatureIds())
|
||||
inputLayer.removeSelection()
|
||||
index = vector.spatialindex(inputLayer)
|
||||
index = QgsProcessingUtils.createSpatialIndex(inputLayer, context)
|
||||
|
||||
if 'disjoint' in predicates:
|
||||
disjoinSet = []
|
||||
|
@ -154,7 +154,7 @@ class SpatialJoin(GeoAlgorithm):
|
||||
inFeatB = QgsFeature()
|
||||
inGeom = QgsGeometry()
|
||||
|
||||
index = vector.spatialindex(join)
|
||||
index = QgsProcessingUtils.createSpatialIndex(join, context)
|
||||
|
||||
mapP2 = dict()
|
||||
features = QgsProcessingUtils.getFeatures(join, context)
|
||||
|
@ -85,7 +85,7 @@ class SplitLinesWithLines(GeoAlgorithm):
|
||||
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)
|
||||
|
@ -86,7 +86,7 @@ class SumLines(GeoAlgorithm):
|
||||
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()
|
||||
|
@ -81,8 +81,8 @@ class SymmetricalDifference(GeoAlgorithm):
|
||||
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)
|
||||
|
@ -88,8 +88,8 @@ class Union(GeoAlgorithm):
|
||||
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
|
||||
|
@ -40,15 +40,12 @@ import uuid
|
||||
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,
|
||||
@ -57,8 +54,7 @@ from qgis.core import (QgsFields,
|
||||
QgsFeatureRequest,
|
||||
QgsSettings,
|
||||
QgsProcessingContext,
|
||||
QgsProcessingUtils,
|
||||
QgsMessageLog)
|
||||
QgsProcessingUtils)
|
||||
|
||||
from processing.core.ProcessingConfig import ProcessingConfig
|
||||
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
||||
@ -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
|
||||
|
@ -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 )
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "qgsrasterlayer.h"
|
||||
#include "qgsvectorlayer.h"
|
||||
#include "qgsmessagelog.h"
|
||||
#include "qgsspatialindex.h"
|
||||
|
||||
class QgsProject;
|
||||
class QgsProcessingContext;
|
||||
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user