Navigation Menu

Skip to content

Commit

Permalink
[processing] add layer options flag enum
Browse files Browse the repository at this point in the history
This enum can be used in the QgsProcessingUtils::mapLayerFromString()
and parameterAsPointCloudLayer() calls to control layer loading, e.g.
skipping index generation or not applying default style, etc.
  • Loading branch information
alexbruy authored and nyalldawson committed Mar 28, 2023
1 parent f6eb1a3 commit 0325cf0
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 27 deletions.
5 changes: 5 additions & 0 deletions python/core/auto_additions/qgsprocessing.py
@@ -0,0 +1,5 @@
# The following has been generated automatically from src/core/processing/qgsprocessing.h
# monkey patching scoped based enum
QgsProcessing.LayerOptionsFlag.SkipIndexGeneration.__doc__ = "Do not generate index when creating a layer. Makes sense only for point cloud layers"
QgsProcessing.LayerOptionsFlag.__doc__ = 'Layer options flags\n\n.. versionadded:: 3.32\n\n' + '* ``SkipIndexGeneration``: ' + QgsProcessing.LayerOptionsFlag.SkipIndexGeneration.__doc__
# --
7 changes: 7 additions & 0 deletions python/core/auto_generated/processing/qgsprocessing.sip.in
Expand Up @@ -47,6 +47,13 @@ and parameters.
PythonQgsProcessingAlgorithmSubclass,
};

enum class LayerOptionsFlag
{
SkipIndexGeneration,
};
typedef QFlags<QgsProcessing::LayerOptionsFlag> LayerOptionsFlags;


static QString sourceTypeToString( SourceType type );
%Docstring
Converts a source ``type`` to a string representation.
Expand Down
Expand Up @@ -1002,9 +1002,10 @@ Evaluates the parameter with matching ``name`` to a DateTime, or returns an inva
.. versionadded:: 3.14
%End

QgsPointCloudLayer *parameterAsPointCloudLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
QgsPointCloudLayer *parameterAsPointCloudLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() ) const;
%Docstring
Evaluates the parameter with matching ``name`` to a point cloud layer.
The ``flags`` are used to set options for loading layer (e.g. skip index generation).

Layers will either be taken from ``context``'s active project, or loaded from external
sources and stored temporarily in the ``context``. In either case, callers do not
Expand Down
Expand Up @@ -1162,7 +1162,7 @@ a conversion in this case and will return the target layer name in the ``layerNa
.. versionadded:: 3.10
%End

static QgsMapLayer *parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint = QgsProcessingUtils::LayerHint::UnknownType );
static QgsMapLayer *parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint = QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
%Docstring
Evaluates the parameter with matching ``definition`` to a map layer.

Expand All @@ -1171,7 +1171,7 @@ sources and stored temporarily in the ``context``. In either case, callers do no
need to handle deletion of the returned layer.
%End

static QgsMapLayer *parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint = QgsProcessingUtils::LayerHint::UnknownType );
static QgsMapLayer *parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint = QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
%Docstring
Evaluates the parameter with matching ``definition`` and ``value`` to a map layer.

Expand Down Expand Up @@ -1588,9 +1588,10 @@ Evaluates the parameter with matching ``definition`` and ``value`` to a database
.. versionadded:: 3.14
%End

static QgsPointCloudLayer *parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
static QgsPointCloudLayer *parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
%Docstring
Evaluates the parameter with matching ``definition`` to a point cloud layer.
The ``flags`` are used to set options for loading layer (e.g. skip index generation).

Layers will either be taken from ``context``'s active project, or loaded from external
sources and stored temporarily in the ``context``. In either case, callers do not
Expand All @@ -1599,9 +1600,10 @@ need to handle deletion of the returned layer.
.. versionadded:: 3.22
%End

static QgsPointCloudLayer *parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context );
static QgsPointCloudLayer *parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
%Docstring
Evaluates the parameter with matching ``definition`` and ``value`` to a point cloud layer.
The ``flags`` are used to set options for loading layer (e.g. skip index generation).

Layers will either be taken from ``context``'s active project, or loaded from external
sources and stored temporarily in the ``context``. In either case, callers do not
Expand Down
Expand Up @@ -211,7 +211,7 @@ Decodes a provider key and layer ``uri`` from an encoded string, for use with
Annotation,
};

static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
%Docstring
Interprets a string as a map layer within the supplied ``context``.

Expand Down
11 changes: 11 additions & 0 deletions src/core/processing/qgsprocessing.h
Expand Up @@ -65,6 +65,17 @@ class CORE_EXPORT QgsProcessing
PythonQgsProcessingAlgorithmSubclass, //!< Full Python QgsProcessingAlgorithm subclass
};

/**
* Layer options flags
*
* \since QGIS 3.32
*/
enum class LayerOptionsFlag : int
{
SkipIndexGeneration = 1 << 0, //!< Do not generate index when creating a layer. Makes sense only for point cloud layers
};
Q_DECLARE_FLAGS( LayerOptionsFlags, LayerOptionsFlag )

/**
* Converts a source \a type to a string representation.
*
Expand Down
4 changes: 2 additions & 2 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -875,9 +875,9 @@ QString QgsProcessingAlgorithm::parameterAsDatabaseTableName( const QVariantMap
return QgsProcessingParameters::parameterAsDatabaseTableName( parameterDefinition( name ), parameters, context );
}

QgsPointCloudLayer *QgsProcessingAlgorithm::parameterAsPointCloudLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
QgsPointCloudLayer *QgsProcessingAlgorithm::parameterAsPointCloudLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags ) const
{
return QgsProcessingParameters::parameterAsPointCloudLayer( parameterDefinition( name ), parameters, context );
return QgsProcessingParameters::parameterAsPointCloudLayer( parameterDefinition( name ), parameters, context, flags );
}

QgsAnnotationLayer *QgsProcessingAlgorithm::parameterAsAnnotationLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
Expand Down
3 changes: 2 additions & 1 deletion src/core/processing/qgsprocessingalgorithm.h
Expand Up @@ -1002,14 +1002,15 @@ class CORE_EXPORT QgsProcessingAlgorithm

/**
* Evaluates the parameter with matching \a name to a point cloud layer.
* The \a flags are used to set options for loading layer (e.g. skip index generation).
*
* Layers will either be taken from \a context's active project, or loaded from external
* sources and stored temporarily in the \a context. In either case, callers do not
* need to handle deletion of the returned layer.
*
* \since QGIS 3.22
*/
QgsPointCloudLayer *parameterAsPointCloudLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
QgsPointCloudLayer *parameterAsPointCloudLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() ) const;

/**
* Evaluates the parameter with matching \a name to an annotation layer.
Expand Down
18 changes: 9 additions & 9 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -847,15 +847,15 @@ QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPathAndLayerNam
return parameterAsCompatibleSourceLayerPathInternal( definition, parameters, context, compatibleFormats, preferredFormat, feedback, destLayer );
}

QgsMapLayer *QgsProcessingParameters::parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint )
QgsMapLayer *QgsProcessingParameters::parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint, QgsProcessing::LayerOptionsFlags flags )
{
if ( !definition )
return nullptr;

return parameterAsLayer( definition, parameters.value( definition->name() ), context, layerHint );
return parameterAsLayer( definition, parameters.value( definition->name() ), context, layerHint, flags );
}

QgsMapLayer *QgsProcessingParameters::parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint )
QgsMapLayer *QgsProcessingParameters::parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint, QgsProcessing::LayerOptionsFlags flags )
{
if ( !definition )
return nullptr;
Expand Down Expand Up @@ -901,7 +901,7 @@ QgsMapLayer *QgsProcessingParameters::parameterAsLayer( const QgsProcessingParam
if ( layerRef.isEmpty() )
return nullptr;

return QgsProcessingUtils::mapLayerFromString( layerRef, context, true, layerHint );
return QgsProcessingUtils::mapLayerFromString( layerRef, context, true, layerHint, flags );
}

QgsRasterLayer *QgsProcessingParameters::parameterAsRasterLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
Expand Down Expand Up @@ -2166,14 +2166,14 @@ QString QgsProcessingParameters::parameterAsDatabaseTableName( const QgsProcessi
return parameterAsString( definition, value, context );
}

QgsPointCloudLayer *QgsProcessingParameters::parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
QgsPointCloudLayer *QgsProcessingParameters::parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags )
{
return qobject_cast< QgsPointCloudLayer *>( parameterAsLayer( definition, parameters, context, QgsProcessingUtils::LayerHint::PointCloud ) );
return qobject_cast< QgsPointCloudLayer *>( parameterAsLayer( definition, parameters, context, QgsProcessingUtils::LayerHint::PointCloud, flags ) );
}

QgsPointCloudLayer *QgsProcessingParameters::parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
QgsPointCloudLayer *QgsProcessingParameters::parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags )
{
return qobject_cast< QgsPointCloudLayer *>( parameterAsLayer( definition, value, context, QgsProcessingUtils::LayerHint::PointCloud ) );
return qobject_cast< QgsPointCloudLayer *>( parameterAsLayer( definition, value, context, QgsProcessingUtils::LayerHint::PointCloud, flags ) );
}

QgsAnnotationLayer *QgsProcessingParameters::parameterAsAnnotationLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
Expand Down Expand Up @@ -8770,7 +8770,7 @@ bool QgsProcessingParameterPointCloudLayer::checkValueIsAcceptable( const QVaria
}

// try to load as layer
if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::LayerHint::PointCloud ) )
if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::LayerHint::PointCloud, QgsProcessing::LayerOptionsFlag::SkipIndexGeneration ) )
return true;

return false;
Expand Down
10 changes: 6 additions & 4 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -1280,7 +1280,7 @@ class CORE_EXPORT QgsProcessingParameters
* sources and stored temporarily in the \a context. In either case, callers do not
* need to handle deletion of the returned layer.
*/
static QgsMapLayer *parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint = QgsProcessingUtils::LayerHint::UnknownType );
static QgsMapLayer *parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint = QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );

/**
* Evaluates the parameter with matching \a definition and \a value to a map layer.
Expand All @@ -1291,7 +1291,7 @@ class CORE_EXPORT QgsProcessingParameters
*
* \since QGIS 3.4
*/
static QgsMapLayer *parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint = QgsProcessingUtils::LayerHint::UnknownType );
static QgsMapLayer *parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint = QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );

/**
* Evaluates the parameter with matching \a definition to a raster layer.
Expand Down Expand Up @@ -1678,25 +1678,27 @@ class CORE_EXPORT QgsProcessingParameters

/**
* Evaluates the parameter with matching \a definition to a point cloud layer.
* The \a flags are used to set options for loading layer (e.g. skip index generation).
*
* Layers will either be taken from \a context's active project, or loaded from external
* sources and stored temporarily in the \a context. In either case, callers do not
* need to handle deletion of the returned layer.
*
* \since QGIS 3.22
*/
static QgsPointCloudLayer *parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
static QgsPointCloudLayer *parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );

/**
* Evaluates the parameter with matching \a definition and \a value to a point cloud layer.
* The \a flags are used to set options for loading layer (e.g. skip index generation).
*
* Layers will either be taken from \a context's active project, or loaded from external
* sources and stored temporarily in the \a context. In either case, callers do not
* need to handle deletion of the returned layer.
*
* \since QGIS 3.22
*/
static QgsPointCloudLayer *parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context );
static QgsPointCloudLayer *parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );

/**
* Evaluates the parameter with matching \a definition to an annotation layer.
Expand Down
11 changes: 8 additions & 3 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -257,7 +257,7 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromStore( const QString &string, QgsMa
return nullptr;
}

QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint )
QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint, QgsProcessing::LayerOptionsFlags flags )
{
QString provider;
QString uri;
Expand Down Expand Up @@ -355,6 +355,11 @@ QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string,
QgsPointCloudLayer::LayerOptions pointCloudOptions;
pointCloudOptions.skipCrsValidation = true;

if ( flags & QgsProcessing::LayerOptionsFlag::SkipIndexGeneration )
{
pointCloudOptions.skipIndexGeneration = true;
}

std::unique_ptr< QgsPointCloudLayer > pointCloudLayer;
if ( useProvider )
{
Expand All @@ -376,7 +381,7 @@ QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string,
return nullptr;
}

QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers, LayerHint typeHint )
QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers, LayerHint typeHint, QgsProcessing::LayerOptionsFlags flags )
{
if ( string.isEmpty() )
return nullptr;
Expand All @@ -400,7 +405,7 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsP
if ( !allowLoadingNewLayers )
return nullptr;

layer = loadMapLayerFromString( string, context.transformContext(), typeHint );
layer = loadMapLayerFromString( string, context.transformContext(), typeHint, flags );
if ( layer )
{
context.temporaryLayerStore()->addMapLayer( layer );
Expand Down
5 changes: 3 additions & 2 deletions src/core/processing/qgsprocessingutils.h
Expand Up @@ -23,6 +23,7 @@
#include "qgsrasterlayer.h"
#include "qgsfeaturesink.h"
#include "qgsfeaturesource.h"
#include "qgsprocessing.h"
#include "qgsproxyfeaturesink.h"
#include "qgsremappingproxyfeaturesink.h"

Expand Down Expand Up @@ -218,7 +219,7 @@ class CORE_EXPORT QgsProcessingUtils
*
* The \a typeHint can be used to dictate the type of map layer expected.
*/
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );

/**
* Converts a variant \a value to a new feature source.
Expand Down Expand Up @@ -562,7 +563,7 @@ class CORE_EXPORT QgsProcessingUtils
*
* \since QGIS 3.8
*/
static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType );
static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );

/**
* Interprets a string as a map layer. The method will attempt to
Expand Down

0 comments on commit 0325cf0

Please sign in to comment.