mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
Move method to evaluate a variant to a feature source to QgsProcessingUtils
This commit is contained in:
parent
8f19f74893
commit
b7ae44fb30
@ -79,6 +79,20 @@ class QgsProcessingUtils
|
|||||||
:rtype: QgsMapLayer
|
:rtype: QgsMapLayer
|
||||||
%End
|
%End
|
||||||
|
|
||||||
|
static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) /Factory/;
|
||||||
|
%Docstring
|
||||||
|
Converts a variant ``value`` to a new feature source.
|
||||||
|
|
||||||
|
Sources will either be taken from ``context``'s active project, or loaded from external
|
||||||
|
sources and stored temporarily in the ``context``.
|
||||||
|
|
||||||
|
The optional ``fallbackValue`` can be used to specify a "default" value which is used
|
||||||
|
if ``value`` cannot be successfully converted to a source.
|
||||||
|
|
||||||
|
This function creates a new object and the caller takes responsibility for deleting the returned object.
|
||||||
|
:rtype: QgsProcessingFeatureSource
|
||||||
|
%End
|
||||||
|
|
||||||
static QString normalizeLayerSource( const QString &source );
|
static QString normalizeLayerSource( const QString &source );
|
||||||
%Docstring
|
%Docstring
|
||||||
Normalizes a layer ``source`` string for safe comparison across different
|
Normalizes a layer ``source`` string for safe comparison across different
|
||||||
|
@ -277,55 +277,7 @@ QgsProcessingFeatureSource *QgsProcessingParameters::parameterAsSource( const Qg
|
|||||||
|
|
||||||
QVariant val = parameters.value( definition->name() );
|
QVariant val = parameters.value( definition->name() );
|
||||||
|
|
||||||
bool selectedFeaturesOnly = false;
|
return QgsProcessingUtils::variantToSource( val, context, definition->defaultValue() );
|
||||||
if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
|
|
||||||
{
|
|
||||||
// input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
|
|
||||||
QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
|
|
||||||
selectedFeaturesOnly = fromVar.selectedFeaturesOnly;
|
|
||||||
val = fromVar.source;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) ) )
|
|
||||||
{
|
|
||||||
return new QgsProcessingFeatureSource( layer, context );
|
|
||||||
}
|
|
||||||
|
|
||||||
QString layerRef;
|
|
||||||
if ( val.canConvert<QgsProperty>() )
|
|
||||||
{
|
|
||||||
layerRef = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
||||||
}
|
|
||||||
else if ( !val.isValid() || val.toString().isEmpty() )
|
|
||||||
{
|
|
||||||
// fall back to default
|
|
||||||
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
|
|
||||||
{
|
|
||||||
return new QgsProcessingFeatureSource( layer, context );
|
|
||||||
}
|
|
||||||
|
|
||||||
layerRef = definition->defaultValue().toString();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
layerRef = val.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( layerRef.isEmpty() )
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context ) );
|
|
||||||
if ( !vl )
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
if ( selectedFeaturesOnly )
|
|
||||||
{
|
|
||||||
return new QgsProcessingFeatureSource( new QgsVectorLayerSelectedFeatureSource( vl ), context, true );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return new QgsProcessingFeatureSource( vl, context );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback )
|
QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback )
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#include "qgsmemoryproviderutils.h"
|
#include "qgsmemoryproviderutils.h"
|
||||||
#include "qgsprocessingparameters.h"
|
#include "qgsprocessingparameters.h"
|
||||||
#include "qgsprocessingalgorithm.h"
|
#include "qgsprocessingalgorithm.h"
|
||||||
|
#include "qgsvectorlayerfeatureiterator.h"
|
||||||
|
|
||||||
QList<QgsRasterLayer *> QgsProcessingUtils::compatibleRasterLayers( QgsProject *project, bool sort )
|
QList<QgsRasterLayer *> QgsProcessingUtils::compatibleRasterLayers( QgsProject *project, bool sort )
|
||||||
{
|
{
|
||||||
@ -208,6 +209,60 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsP
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QgsProcessingFeatureSource *QgsProcessingUtils::variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue )
|
||||||
|
{
|
||||||
|
QVariant val = value;
|
||||||
|
bool selectedFeaturesOnly = false;
|
||||||
|
if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
|
||||||
|
{
|
||||||
|
// input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
|
||||||
|
QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
|
||||||
|
selectedFeaturesOnly = fromVar.selectedFeaturesOnly;
|
||||||
|
val = fromVar.source;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) ) )
|
||||||
|
{
|
||||||
|
return new QgsProcessingFeatureSource( layer, context );
|
||||||
|
}
|
||||||
|
|
||||||
|
QString layerRef;
|
||||||
|
if ( val.canConvert<QgsProperty>() )
|
||||||
|
{
|
||||||
|
layerRef = val.value< QgsProperty >().valueAsString( context.expressionContext(), fallbackValue.toString() );
|
||||||
|
}
|
||||||
|
else if ( !val.isValid() || val.toString().isEmpty() )
|
||||||
|
{
|
||||||
|
// fall back to default
|
||||||
|
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( fallbackValue ) ) )
|
||||||
|
{
|
||||||
|
return new QgsProcessingFeatureSource( layer, context );
|
||||||
|
}
|
||||||
|
|
||||||
|
layerRef = fallbackValue.toString();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
layerRef = val.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( layerRef.isEmpty() )
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context ) );
|
||||||
|
if ( !vl )
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
if ( selectedFeaturesOnly )
|
||||||
|
{
|
||||||
|
return new QgsProcessingFeatureSource( new QgsVectorLayerSelectedFeatureSource( vl ), context, true );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new QgsProcessingFeatureSource( vl, context );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool QgsProcessingUtils::canUseLayer( const QgsRasterLayer *layer )
|
bool QgsProcessingUtils::canUseLayer( const QgsRasterLayer *layer )
|
||||||
{
|
{
|
||||||
// only gdal file-based layers
|
// only gdal file-based layers
|
||||||
|
@ -29,6 +29,7 @@ class QgsProject;
|
|||||||
class QgsProcessingContext;
|
class QgsProcessingContext;
|
||||||
class QgsMapLayerStore;
|
class QgsMapLayerStore;
|
||||||
class QgsProcessingFeedback;
|
class QgsProcessingFeedback;
|
||||||
|
class QgsProcessingFeatureSource;
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
@ -95,6 +96,19 @@ class CORE_EXPORT QgsProcessingUtils
|
|||||||
*/
|
*/
|
||||||
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true );
|
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a variant \a value to a new feature source.
|
||||||
|
*
|
||||||
|
* Sources will either be taken from \a context's active project, or loaded from external
|
||||||
|
* sources and stored temporarily in the \a context.
|
||||||
|
*
|
||||||
|
* The optional \a fallbackValue can be used to specify a "default" value which is used
|
||||||
|
* if \a value cannot be successfully converted to a source.
|
||||||
|
*
|
||||||
|
* This function creates a new object and the caller takes responsibility for deleting the returned object.
|
||||||
|
*/
|
||||||
|
static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) SIP_FACTORY;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalizes a layer \a source string for safe comparison across different
|
* Normalizes a layer \a source string for safe comparison across different
|
||||||
* operating system environments.
|
* operating system environments.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user