Skip to content

Commit 72b061b

Browse files
authored
Merge pull request #4479 from nyalldawson/save_temp
[processing] Port dataobjects.getLayerFromString to c++
2 parents 1711a41 + 877775d commit 72b061b

File tree

171 files changed

+635
-678
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

171 files changed

+635
-678
lines changed

doc/api_break.dox

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,6 +2231,10 @@ object of type QgsProcessingFeedback, and will need to adapt their use of progre
22312231
- algList was removed. Use QgsApplication.processingRegistry() instead.
22322232
- Processing.algs was removed. QgsApplication.processingRegistry().algorithms() instead.
22332233
- ProcessingLog should not be used when reporting log messages from algorithms. Use QgsMessageLog.logMessage() instead.
2234+
- dataobjects.getLayerFromString() was removed. Use QgsProcessingUtils.mapLayerFromString() instead.
2235+
- vector.bufferedBoundingBox() was removed. Use QgsRectangle.grow() instead.
2236+
- vector.duplicateInMemory() was removed.
2237+
- vector.spatialindex() was removed. Use QgsProcessingUtils.createSpatialIndex() instead.
22342238

22352239
Triangulation {#qgis_api_break_3_0_Triangulation}
22362240
-------------

python/core/processing/qgsprocessingutils.sip

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,16 @@ class QgsProcessingUtils
6666
:rtype: list of QgsMapLayer
6767
%End
6868

69-
70-
static QgsMapLayer *mapLayerFromProject( const QString &string, QgsProject *project );
71-
%Docstring
72-
Interprets a ``string`` as a map layer from a project.
73-
74-
This method attempts to match a string to a project map layer, using
75-
first the layer ID, then layer names, and finally layer source.
76-
If the string matches a normalized version of any layer source
77-
for layers in the specified ``project``, then those matching layers will be
78-
returned.
79-
.. seealso:: mapLayerFromString()
80-
:rtype: QgsMapLayer
81-
%End
82-
83-
static QgsMapLayer *mapLayerFromString( const QString &string ) /Factory/;
69+
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true );
8470
%Docstring
85-
Interprets a string as a map layer. The method will attempt to
86-
load a layer matching the passed ``string``. E.g. if the string is a file path,
87-
then the layer at this file path will be loaded.
88-
The caller takes responsibility for deleting the returned map layer.
89-
.. seealso:: mapLayerFromProject()
71+
Interprets a string as a map layer within the supplied ``context``.
72+
73+
The method will attempt to
74+
load a layer matching the passed ``string``. E.g. if the string matches a layer ID or name
75+
within the context's project or temporary layer store then this layer will be returned.
76+
If the string is a file path and ``allowLoadingNewLayers`` is true, then the layer at this
77+
file path will be loaded and added to the context's temporary layer store.
78+
Ownership of the layer remains with the ``context`` or the context's current project.
9079
:rtype: QgsMapLayer
9180
%End
9281

@@ -115,6 +104,15 @@ class QgsProcessingUtils
115104
:rtype: long
116105
%End
117106

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+
118116
static QList< QVariant > uniqueValues( QgsVectorLayer *layer, int fieldIndex, const QgsProcessingContext &context );
119117
%Docstring
120118
Returns a list of unique values contained in a single field in a ``layer``, when

python/plugins/processing/algs/exampleprovider/ExampleAlgorithm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from processing.core.GeoAlgorithm import GeoAlgorithm
3131
from processing.core.parameters import ParameterVector
3232
from processing.core.outputs import OutputVector
33-
from processing.tools import dataobjects, vector
33+
from processing.tools import dataobjects
3434

3535

3636
class ExampleAlgorithm(GeoAlgorithm):
@@ -92,8 +92,8 @@ def processAlgorithm(self, context, feedback):
9292
# Input layers vales are always a string with its location.
9393
# That string can be converted into a QGIS layer (a
9494
# QgsVectorLayer in this case) using the
95-
# dataobjects.getLayerFromString() method.
96-
vectorLayer = dataobjects.getLayerFromString(inputFilename)
95+
# QgsProcessingUtils.mapLayerFromString() method.
96+
vectorLayer = QgsProcessingUtils.mapLayerFromString(inputFilename, context)
9797

9898
# And now we can process
9999

python/plugins/processing/algs/gdal/ClipByMask.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from qgis.PyQt.QtGui import QIcon
3232

33+
from qgis.core import QgsProcessingUtils
3334
from osgeo import gdal
3435

3536
from processing.core.parameters import (ParameterRaster,
@@ -103,8 +104,8 @@ def defineCharacteristics(self):
103104
def getConsoleCommands(self):
104105
out = self.getOutputValue(self.OUTPUT)
105106
mask = self.getParameterValue(self.MASK)
106-
maskLayer = dataobjects.getLayerFromString(
107-
self.getParameterValue(self.MASK))
107+
context = dataobjects.createContext()
108+
maskLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.MASK), context)
108109
ogrMask = ogrConnectionString(mask)[1:-1]
109110
noData = self.getParameterValue(self.NO_DATA)
110111
opts = self.getParameterValue(self.OPTIONS)

python/plugins/processing/algs/gdal/rasterize_over.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import os
3030

31+
from qgis.core import QgsProcessingUtils
32+
3133
from qgis.PyQt.QtGui import QIcon
3234

3335
from processing.core.parameters import ParameterVector
@@ -69,8 +71,9 @@ def defineCharacteristics(self):
6971
self.tr('Existing raster layer'), False))
7072

7173
def getConsoleCommands(self):
72-
inLayer = dataobjects.getLayerFromString(self.getParameterValue(self.INPUT))
73-
inRasterLayer = dataobjects.getLayerFromString(self.getParameterValue(self.INPUT_RASTER))
74+
context = dataobjects.createContext()
75+
inLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
76+
inRasterLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_RASTER), context)
7477

7578
ogrLayer = ogrConnectionString(inLayer)[1:-1]
7679
ogrRasterLayer = ogrConnectionString(inRasterLayer)[1:-1]

python/plugins/processing/algs/grass7/Grass7Algorithm.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,22 +219,23 @@ def defineCharacteristicsFromFile(self):
219219
self.addParameter(param)
220220

221221
def getDefaultCellsize(self):
222+
context = dataobjects.createContext()
222223
cellsize = 0
223224
for param in self.parameters:
224225
if param.value:
225226
if isinstance(param, ParameterRaster):
226227
if isinstance(param.value, QgsRasterLayer):
227228
layer = param.value
228229
else:
229-
layer = dataobjects.getLayerFromString(param.value)
230+
layer = QgsProcessingUtils.mapLayerFromString(param.value, context)
230231
cellsize = max(cellsize, (layer.extent().xMaximum() -
231232
layer.extent().xMinimum()) /
232233
layer.width())
233234
elif isinstance(param, ParameterMultipleInput):
234235

235236
layers = param.value.split(';')
236237
for layername in layers:
237-
layer = dataobjects.getLayerFromString(layername)
238+
layer = QgsProcessingUtils.mapLayerFromString(layername, context)
238239
if isinstance(layer, QgsRasterLayer):
239240
cellsize = max(cellsize, (
240241
layer.extent().xMaximum() -
@@ -507,16 +508,17 @@ def processOutputs(self):
507508
self.outputCommands.append(command)
508509

509510
def exportVectorLayer(self, orgFilename):
511+
context = dataobjects.createContext()
510512

511513
# TODO: improve this. We are now exporting if it is not a shapefile,
512514
# but the functionality of v.in.ogr could be used for this.
513515
# We also export if there is a selection
514516
if not os.path.exists(orgFilename) or not orgFilename.endswith('shp'):
515-
layer = dataobjects.getLayerFromString(orgFilename, False)
517+
layer = QgsProcessingUtils.mapLayerFromString(orgFilename, context, False)
516518
if layer:
517519
filename = dataobjects.exportVectorLayer(layer)
518520
else:
519-
layer = dataobjects.getLayerFromString(orgFilename, False)
521+
layer = QgsProcessingUtils.mapLayerFromString(orgFilename, context, False)
520522
if layer:
521523
useSelection = \
522524
ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
@@ -549,8 +551,9 @@ def setSessionProjectionFromProject(self, commands):
549551
Grass7Utils.projectionSet = True
550552

551553
def setSessionProjectionFromLayer(self, layer, commands):
554+
context = dataobjects.createContext()
552555
if not Grass7Utils.projectionSet:
553-
qGisLayer = dataobjects.getLayerFromString(layer)
556+
qGisLayer = QgsProcessingUtils.mapLayerFromString(layer, context)
554557
if qGisLayer:
555558
proj4 = str(qGisLayer.crs().toProj4())
556559
command = 'g.proj'

python/plugins/processing/algs/grass7/nviz7.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333

3434
from qgis.PyQt.QtGui import QIcon
3535
from qgis.core import (QgsProcessingAlgorithm,
36-
QgsRasterLayer)
36+
QgsRasterLayer,
37+
QgsProcessingUtils)
3738

3839
from processing.core.GeoAlgorithm import GeoAlgorithm
3940
from processing.core.parameters import ParameterMultipleInput
@@ -167,21 +168,22 @@ def exportRasterLayer(self, layer):
167168

168169
def getDefaultCellsize(self):
169170
cellsize = 0
171+
context = dataobjects.createContext()
170172
for param in self.parameters:
171173
if param.value:
172174
if isinstance(param, ParameterRaster):
173175
if isinstance(param.value, QgsRasterLayer):
174176
layer = param.value
175177
else:
176-
layer = dataobjects.getLayerFromString(param.value)
178+
layer = QgsProcessingUtils.mapLayerFromString(param.value, context)
177179
cellsize = max(cellsize, (layer.extent().xMaximum() -
178180
layer.extent().xMinimum()) /
179181
layer.width())
180182
elif isinstance(param, ParameterMultipleInput):
181183

182184
layers = param.value.split(';')
183185
for layername in layers:
184-
layer = dataobjects.getLayerFromString(layername)
186+
layer = QgsProcessingUtils.mapLayerFromString(layername, context)
185187
if isinstance(layer, QgsRasterLayer):
186188
cellsize = max(cellsize, (
187189
layer.extent().xMaximum() -

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from processing.core.parameters import ParameterNumber
3737
from processing.core.parameters import ParameterSelection
3838
from processing.core.outputs import OutputVector
39-
from processing.tools import dataobjects, vector
4039

4140

4241
class AddTableField(GeoAlgorithm):
@@ -91,8 +90,7 @@ def processAlgorithm(self, context, feedback):
9190
fieldPrecision = self.getParameterValue(self.FIELD_PRECISION)
9291
output = self.getOutputFromName(self.OUTPUT_LAYER)
9392

94-
layer = dataobjects.getLayerFromString(
95-
self.getParameterValue(self.INPUT_LAYER))
93+
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
9694

9795
fields = layer.fields()
9896
fields.append(QgsField(fieldName, self.TYPES[fieldType], '',

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
from processing.core.GeoAlgorithm import GeoAlgorithm
3434
from processing.core.parameters import ParameterVector
3535
from processing.core.outputs import OutputVector
36-
from processing.tools import dataobjects, vector
3736

3837

3938
class AutoincrementalField(GeoAlgorithm):
@@ -64,7 +63,7 @@ def defineCharacteristics(self):
6463
def processAlgorithm(self, context, feedback):
6564
output = self.getOutputFromName(self.OUTPUT)
6665
vlayer = \
67-
dataobjects.getLayerFromString(self.getParameterValue(self.INPUT))
66+
QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
6867
fields = vlayer.fields()
6968
fields.append(QgsField('AUTO', QVariant.Int))
7069
writer = output.getVectorWriter(fields, vlayer.wkbType(), vlayer.crs(), context)

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
import plotly.graph_objs as go
3030

3131

32-
from qgis.core import (QgsApplication)
32+
from qgis.core import (QgsApplication,
33+
QgsProcessingUtils)
3334
from processing.core.parameters import ParameterTable
3435
from processing.core.parameters import ParameterTableField
3536
from processing.core.GeoAlgorithm import GeoAlgorithm
3637
from processing.core.outputs import OutputHTML
3738
from processing.tools import vector
38-
from processing.tools import dataobjects
3939

4040

4141
class BarPlot(GeoAlgorithm):
@@ -74,8 +74,7 @@ def defineCharacteristics(self):
7474
self.addOutput(OutputHTML(self.OUTPUT, self.tr('Bar plot')))
7575

7676
def processAlgorithm(self, context, feedback):
77-
layer = dataobjects.getLayerFromString(
78-
self.getParameterValue(self.INPUT))
77+
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
7978
namefieldname = self.getParameterValue(self.NAME_FIELD)
8079
valuefieldname = self.getParameterValue(self.VALUE_FIELD)
8180

0 commit comments

Comments
 (0)