Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #4479 from nyalldawson/save_temp
[processing] Port dataobjects.getLayerFromString to c++
  • Loading branch information
nyalldawson committed May 2, 2017
2 parents 1711a41 + 877775d commit 72b061b
Show file tree
Hide file tree
Showing 171 changed files with 635 additions and 678 deletions.
4 changes: 4 additions & 0 deletions doc/api_break.dox
Expand Up @@ -2231,6 +2231,10 @@ object of type QgsProcessingFeedback, and will need to adapt their use of progre
- algList was removed. Use QgsApplication.processingRegistry() instead.
- Processing.algs was removed. QgsApplication.processingRegistry().algorithms() instead.
- ProcessingLog should not be used when reporting log messages from algorithms. Use QgsMessageLog.logMessage() instead.
- 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
38 changes: 18 additions & 20 deletions python/core/processing/qgsprocessingutils.sip
Expand Up @@ -66,27 +66,16 @@ class QgsProcessingUtils
:rtype: list of QgsMapLayer
%End


static QgsMapLayer *mapLayerFromProject( const QString &string, QgsProject *project );
%Docstring
Interprets a ``string`` as a map layer from a project.

This method attempts to match a string to a project map layer, using
first the layer ID, then layer names, and finally layer source.
If the string matches a normalized version of any layer source
for layers in the specified ``project``, then those matching layers will be
returned.
.. seealso:: mapLayerFromString()
:rtype: QgsMapLayer
%End

static QgsMapLayer *mapLayerFromString( const QString &string ) /Factory/;
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true );
%Docstring
Interprets a string as a map layer. The method will attempt to
load a layer matching the passed ``string``. E.g. if the string is a file path,
then the layer at this file path will be loaded.
The caller takes responsibility for deleting the returned map layer.
.. seealso:: mapLayerFromProject()
Interprets a string as a map layer within the supplied ``context``.

The method will attempt to
load a layer matching the passed ``string``. E.g. if the string matches a layer ID or name
within the context's project or temporary layer store then this layer will be returned.
If the string is a file path and ``allowLoadingNewLayers`` is true, then the layer at this
file path will be loaded and added to the context's temporary layer store.
Ownership of the layer remains with the ``context`` or the context's current project.
:rtype: QgsMapLayer
%End

Expand Down Expand Up @@ -115,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
Expand Up @@ -30,7 +30,7 @@
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector
from processing.tools import dataobjects


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

# And now we can process

Expand Down
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/gdal/ClipByMask.py
Expand Up @@ -30,6 +30,7 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsProcessingUtils
from osgeo import gdal

from processing.core.parameters import (ParameterRaster,
Expand Down Expand Up @@ -103,8 +104,8 @@ def defineCharacteristics(self):
def getConsoleCommands(self):
out = self.getOutputValue(self.OUTPUT)
mask = self.getParameterValue(self.MASK)
maskLayer = dataobjects.getLayerFromString(
self.getParameterValue(self.MASK))
context = dataobjects.createContext()
maskLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.MASK), context)
ogrMask = ogrConnectionString(mask)[1:-1]
noData = self.getParameterValue(self.NO_DATA)
opts = self.getParameterValue(self.OPTIONS)
Expand Down
7 changes: 5 additions & 2 deletions python/plugins/processing/algs/gdal/rasterize_over.py
Expand Up @@ -28,6 +28,8 @@

import os

from qgis.core import QgsProcessingUtils

from qgis.PyQt.QtGui import QIcon

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

def getConsoleCommands(self):
inLayer = dataobjects.getLayerFromString(self.getParameterValue(self.INPUT))
inRasterLayer = dataobjects.getLayerFromString(self.getParameterValue(self.INPUT_RASTER))
context = dataobjects.createContext()
inLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
inRasterLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_RASTER), context)

ogrLayer = ogrConnectionString(inLayer)[1:-1]
ogrRasterLayer = ogrConnectionString(inRasterLayer)[1:-1]
Expand Down
13 changes: 8 additions & 5 deletions python/plugins/processing/algs/grass7/Grass7Algorithm.py
Expand Up @@ -219,22 +219,23 @@ def defineCharacteristicsFromFile(self):
self.addParameter(param)

def getDefaultCellsize(self):
context = dataobjects.createContext()
cellsize = 0
for param in self.parameters:
if param.value:
if isinstance(param, ParameterRaster):
if isinstance(param.value, QgsRasterLayer):
layer = param.value
else:
layer = dataobjects.getLayerFromString(param.value)
layer = QgsProcessingUtils.mapLayerFromString(param.value, context)
cellsize = max(cellsize, (layer.extent().xMaximum() -
layer.extent().xMinimum()) /
layer.width())
elif isinstance(param, ParameterMultipleInput):

layers = param.value.split(';')
for layername in layers:
layer = dataobjects.getLayerFromString(layername)
layer = QgsProcessingUtils.mapLayerFromString(layername, context)
if isinstance(layer, QgsRasterLayer):
cellsize = max(cellsize, (
layer.extent().xMaximum() -
Expand Down Expand Up @@ -507,16 +508,17 @@ def processOutputs(self):
self.outputCommands.append(command)

def exportVectorLayer(self, orgFilename):
context = dataobjects.createContext()

# TODO: improve this. We are now exporting if it is not a shapefile,
# but the functionality of v.in.ogr could be used for this.
# We also export if there is a selection
if not os.path.exists(orgFilename) or not orgFilename.endswith('shp'):
layer = dataobjects.getLayerFromString(orgFilename, False)
layer = QgsProcessingUtils.mapLayerFromString(orgFilename, context, False)
if layer:
filename = dataobjects.exportVectorLayer(layer)
else:
layer = dataobjects.getLayerFromString(orgFilename, False)
layer = QgsProcessingUtils.mapLayerFromString(orgFilename, context, False)
if layer:
useSelection = \
ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
Expand Down Expand Up @@ -549,8 +551,9 @@ def setSessionProjectionFromProject(self, commands):
Grass7Utils.projectionSet = True

def setSessionProjectionFromLayer(self, layer, commands):
context = dataobjects.createContext()
if not Grass7Utils.projectionSet:
qGisLayer = dataobjects.getLayerFromString(layer)
qGisLayer = QgsProcessingUtils.mapLayerFromString(layer, context)
if qGisLayer:
proj4 = str(qGisLayer.crs().toProj4())
command = 'g.proj'
Expand Down
8 changes: 5 additions & 3 deletions python/plugins/processing/algs/grass7/nviz7.py
Expand Up @@ -33,7 +33,8 @@

from qgis.PyQt.QtGui import QIcon
from qgis.core import (QgsProcessingAlgorithm,
QgsRasterLayer)
QgsRasterLayer,
QgsProcessingUtils)

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterMultipleInput
Expand Down Expand Up @@ -167,21 +168,22 @@ def exportRasterLayer(self, layer):

def getDefaultCellsize(self):
cellsize = 0
context = dataobjects.createContext()
for param in self.parameters:
if param.value:
if isinstance(param, ParameterRaster):
if isinstance(param.value, QgsRasterLayer):
layer = param.value
else:
layer = dataobjects.getLayerFromString(param.value)
layer = QgsProcessingUtils.mapLayerFromString(param.value, context)
cellsize = max(cellsize, (layer.extent().xMaximum() -
layer.extent().xMinimum()) /
layer.width())
elif isinstance(param, ParameterMultipleInput):

layers = param.value.split(';')
for layername in layers:
layer = dataobjects.getLayerFromString(layername)
layer = QgsProcessingUtils.mapLayerFromString(layername, context)
if isinstance(layer, QgsRasterLayer):
cellsize = max(cellsize, (
layer.extent().xMaximum() -
Expand Down
4 changes: 1 addition & 3 deletions python/plugins/processing/algs/qgis/AddTableField.py
Expand Up @@ -36,7 +36,6 @@
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterSelection
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector


class AddTableField(GeoAlgorithm):
Expand Down Expand Up @@ -91,8 +90,7 @@ def processAlgorithm(self, context, feedback):
fieldPrecision = self.getParameterValue(self.FIELD_PRECISION)
output = self.getOutputFromName(self.OUTPUT_LAYER)

layer = dataobjects.getLayerFromString(
self.getParameterValue(self.INPUT_LAYER))
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)

fields = layer.fields()
fields.append(QgsField(fieldName, self.TYPES[fieldType], '',
Expand Down
3 changes: 1 addition & 2 deletions python/plugins/processing/algs/qgis/AutoincrementalField.py
Expand Up @@ -33,7 +33,6 @@
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector


class AutoincrementalField(GeoAlgorithm):
Expand Down Expand Up @@ -64,7 +63,7 @@ def defineCharacteristics(self):
def processAlgorithm(self, context, feedback):
output = self.getOutputFromName(self.OUTPUT)
vlayer = \
dataobjects.getLayerFromString(self.getParameterValue(self.INPUT))
QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
fields = vlayer.fields()
fields.append(QgsField('AUTO', QVariant.Int))
writer = output.getVectorWriter(fields, vlayer.wkbType(), vlayer.crs(), context)
Expand Down
7 changes: 3 additions & 4 deletions python/plugins/processing/algs/qgis/BarPlot.py
Expand Up @@ -29,13 +29,13 @@
import plotly.graph_objs as go


from qgis.core import (QgsApplication)
from qgis.core import (QgsApplication,
QgsProcessingUtils)
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterTableField
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.outputs import OutputHTML
from processing.tools import vector
from processing.tools import dataobjects


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

def processAlgorithm(self, context, feedback):
layer = dataobjects.getLayerFromString(
self.getParameterValue(self.INPUT))
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
namefieldname = self.getParameterValue(self.NAME_FIELD)
valuefieldname = self.getParameterValue(self.VALUE_FIELD)

Expand Down
4 changes: 1 addition & 3 deletions python/plugins/processing/algs/qgis/BasicStatistics.py
Expand Up @@ -42,7 +42,6 @@
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputHTML
from processing.core.outputs import OutputNumber
from processing.tools import dataobjects, vector

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

Expand Down Expand Up @@ -122,8 +121,7 @@ def defineCharacteristics(self):
self.addOutput(OutputNumber(self.IQR, self.tr('Interquartile Range (IQR)')))

def processAlgorithm(self, context, feedback):
layer = dataobjects.getLayerFromString(
self.getParameterValue(self.INPUT_LAYER))
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
field_name = self.getParameterValue(self.FIELD_NAME)
field = layer.fields().at(layer.fields().lookupField(field_name))

Expand Down
Expand Up @@ -41,7 +41,6 @@
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputHTML
from processing.core.outputs import OutputNumber
from processing.tools import dataobjects, vector

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

Expand Down Expand Up @@ -116,8 +115,7 @@ def defineCharacteristics(self):
self.addOutput(OutputNumber(self.IQR, self.tr('Interquartile Range (IQR)')))

def processAlgorithm(self, context, feedback):
layer = dataobjects.getLayerFromString(
self.getParameterValue(self.INPUT_LAYER))
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
fieldName = self.getParameterValue(self.FIELD_NAME)

outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)
Expand Down
Expand Up @@ -41,7 +41,6 @@
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputHTML
from processing.core.outputs import OutputNumber
from processing.tools import dataobjects, vector

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

Expand Down Expand Up @@ -102,8 +101,7 @@ def defineCharacteristics(self):
self.addOutput(OutputNumber(self.MAX_VALUE, self.tr('Maximum string value')))

def processAlgorithm(self, context, feedback):
layer = dataobjects.getLayerFromString(
self.getParameterValue(self.INPUT_LAYER))
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
fieldName = self.getParameterValue(self.FIELD_NAME)

outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)
Expand Down
5 changes: 2 additions & 3 deletions python/plugins/processing/algs/qgis/Boundary.py
Expand Up @@ -35,7 +35,7 @@
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector
from processing.tools import dataobjects

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

Expand Down Expand Up @@ -64,8 +64,7 @@ def defineCharacteristics(self):
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Boundary')))

def processAlgorithm(self, context, feedback):
layer = dataobjects.getLayerFromString(
self.getParameterValue(self.INPUT_LAYER))
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)

input_wkb = layer.wkbType()
if QgsWkbTypes.geometryType(input_wkb) == QgsWkbTypes.LineGeometry:
Expand Down
5 changes: 2 additions & 3 deletions python/plugins/processing/algs/qgis/BoundingBox.py
Expand Up @@ -35,7 +35,7 @@
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector
from processing.tools import dataobjects

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

Expand Down Expand Up @@ -63,8 +63,7 @@ def defineCharacteristics(self):
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Bounds'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))

def processAlgorithm(self, context, feedback):
layer = dataobjects.getLayerFromString(
self.getParameterValue(self.INPUT_LAYER))
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(layer.fields().toList(), QgsWkbTypes.Polygon, layer.crs(), context)
Expand Down
7 changes: 3 additions & 4 deletions python/plugins/processing/algs/qgis/BoxPlot.py
Expand Up @@ -28,14 +28,14 @@
import plotly as plt
import plotly.graph_objs as go

from qgis.core import (QgsApplication)
from qgis.core import (QgsApplication,
QgsProcessingUtils)
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterSelection
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.outputs import OutputHTML
from processing.tools import vector
from processing.tools import dataobjects


class BoxPlot(GeoAlgorithm):
Expand Down Expand Up @@ -83,8 +83,7 @@ def defineCharacteristics(self):
self.addOutput(OutputHTML(self.OUTPUT, self.tr('Box plot')))

def processAlgorithm(self, context, feedback):
layer = dataobjects.getLayerFromString(
self.getParameterValue(self.INPUT))
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
namefieldname = self.getParameterValue(self.NAME_FIELD)
valuefieldname = self.getParameterValue(self.VALUE_FIELD)

Expand Down

0 comments on commit 72b061b

Please sign in to comment.