Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] new pont cloud parameter for processing
  • Loading branch information
alexbruy committed Aug 5, 2021
1 parent d01aa28 commit f801cb6
Show file tree
Hide file tree
Showing 12 changed files with 338 additions and 15 deletions.
5 changes: 4 additions & 1 deletion python/core/auto_additions/qgsprocessingutils.py
Expand Up @@ -12,5 +12,8 @@
QgsProcessingUtils.Mesh = QgsProcessingUtils.LayerHint.Mesh
QgsProcessingUtils.Mesh.is_monkey_patched = True
QgsProcessingUtils.LayerHint.Mesh.__doc__ = "Mesh layer type, since QGIS 3.6"
QgsProcessingUtils.LayerHint.__doc__ = 'Layer type hints.\n\n.. versionadded:: 3.4\n\n' + '* ``UnknownType``: ' + QgsProcessingUtils.LayerHint.UnknownType.__doc__ + '\n' + '* ``Vector``: ' + QgsProcessingUtils.LayerHint.Vector.__doc__ + '\n' + '* ``Raster``: ' + QgsProcessingUtils.LayerHint.Raster.__doc__ + '\n' + '* ``Mesh``: ' + QgsProcessingUtils.LayerHint.Mesh.__doc__
QgsProcessingUtils.PointCloud = QgsProcessingUtils.LayerHint.PointCloud
QgsProcessingUtils.PointCloud.is_monkey_patched = True
QgsProcessingUtils.LayerHint.PointCloud.__doc__ = "Point cloud layer type, since QGIS 3.22"
QgsProcessingUtils.LayerHint.__doc__ = 'Layer type hints.\n\n.. versionadded:: 3.4\n\n' + '* ``UnknownType``: ' + QgsProcessingUtils.LayerHint.UnknownType.__doc__ + '\n' + '* ``Vector``: ' + QgsProcessingUtils.LayerHint.Vector.__doc__ + '\n' + '* ``Raster``: ' + QgsProcessingUtils.LayerHint.Raster.__doc__ + '\n' + '* ``Mesh``: ' + QgsProcessingUtils.LayerHint.Mesh.__doc__ + '\n' + '* ``PointCloud``: ' + QgsProcessingUtils.LayerHint.PointCloud.__doc__
# --
Expand Up @@ -795,7 +795,6 @@ need to handle deletion of the returned layer.
.. versionadded:: 3.6
%End


QString parameterAsOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
%Docstring
Evaluates the parameter with matching ``name`` to a output layer destination.
Expand Down Expand Up @@ -887,8 +886,6 @@ Returns the coordinate reference system associated with a geometry parameter val
.. seealso:: :py:func:`parameterAsGeometry`
%End



QString parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
%Docstring
Evaluates the parameter with matching ``name`` to a file/folder name.
Expand Down Expand Up @@ -981,6 +978,16 @@ 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;
%Docstring
Evaluates the parameter with matching ``name`` to a point cloud layer.

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
need to handle deletion of the returned layer.

.. versionadded:: 3.22
%End

static QString invalidSourceError( const QVariantMap &parameters, const QString &name );
%Docstring
Expand Down
Expand Up @@ -321,6 +321,8 @@ their acceptable ranges, defaults, etc.
sipType = sipType_QgsProcessingParameterMeshDatasetGroups;
else if ( sipCpp->type() == QgsProcessingParameterMeshDatasetTime::typeName() )
sipType = sipType_QgsProcessingParameterMeshDatasetTime;
else if ( sipCpp->type() == QgsProcessingParameterPointCloudLayer::typeName() )
sipType = sipType_QgsProcessingParameterPointCloudLayer;
else
sipType = nullptr;
%End
Expand Down Expand Up @@ -1194,7 +1196,6 @@ need to handle deletion of the returned layer.
.. versionadded:: 3.6
%End


static QgsCoordinateReferenceSystem parameterAsCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``definition`` to a coordinate reference system.
Expand Down Expand Up @@ -1515,6 +1516,29 @@ 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 );
%Docstring
Evaluates the parameter with matching ``definition`` to a point cloud layer.

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
need to handle deletion of the returned layer.

.. versionadded:: 3.22
%End

static QgsPointCloudLayer *parameterAsPointCloudLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``definition`` and ``value`` to a point cloud layer.

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
need to handle deletion of the returned layer.

.. versionadded:: 3.22
%End


static QgsProcessingParameterDefinition *parameterFromVariantMap( const QVariantMap &map ) /Factory/;
%Docstring
Creates a new :py:class:`QgsProcessingParameterDefinition` using the configuration from a
Expand Down Expand Up @@ -4445,6 +4469,46 @@ a new (non-existing) tables.
};


class QgsProcessingParameterPointCloudLayer : QgsProcessingParameterDefinition, QgsFileFilterGenerator
{
%Docstring(signature="appended")
A point cloud layer parameter for processing algorithms.

.. versionadded:: 3.22
%End

%TypeHeaderCode
#include "qgsprocessingparameters.h"
%End
public:

QgsProcessingParameterPointCloudLayer( const QString &name, const QString &description = QString(),
const QVariant &defaultValue = QVariant(), bool optional = false );
%Docstring
Constructor for QgsProcessingParameterPointCloudLayer.
%End

static QString typeName();
%Docstring
Returns the type name for the parameter class.
%End
virtual QgsProcessingParameterDefinition *clone() const /Factory/;

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;

virtual QString createFileFilter() const;


static QgsProcessingParameterPointCloudLayer *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/;
%Docstring
Creates a new parameter using the definition from a script code.
%End
};





Expand Down
Expand Up @@ -145,6 +145,7 @@ Decodes a provider key and layer ``uri`` from an encoded string, for use with
Vector,
Raster,
Mesh,
PointCloud,
};

static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
Expand Down
6 changes: 6 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -28,6 +28,7 @@
#include "qgsvectorlayer.h"
#include "qgsprocessingfeedback.h"
#include "qgsmeshlayer.h"
#include "qgspointcloudlayer.h"
#include "qgsexpressioncontextutils.h"


Expand Down Expand Up @@ -795,6 +796,11 @@ QString QgsProcessingAlgorithm::parameterAsDatabaseTableName( const QVariantMap
return QgsProcessingParameters::parameterAsDatabaseTableName( parameterDefinition( name ), parameters, context );
}

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

QString QgsProcessingAlgorithm::invalidSourceError( const QVariantMap &parameters, const QString &name )
{
if ( !parameters.contains( name ) )
Expand Down
14 changes: 11 additions & 3 deletions src/core/processing/qgsprocessingalgorithm.h
Expand Up @@ -35,6 +35,7 @@ class QgsFeatureSink;
class QgsProcessingModelAlgorithm;
class QgsProcessingAlgorithmConfigurationWidget;
class QgsMeshLayer;
class QgsPointCloudLayer;

#ifdef SIP_RUN
% ModuleHeaderCode
Expand Down Expand Up @@ -797,7 +798,6 @@ class CORE_EXPORT QgsProcessingAlgorithm
*/
QgsMeshLayer *parameterAsMeshLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;


/**
* Evaluates the parameter with matching \a name to a output layer destination.
*/
Expand Down Expand Up @@ -889,8 +889,6 @@ class CORE_EXPORT QgsProcessingAlgorithm
*/
QgsCoordinateReferenceSystem parameterAsGeometryCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );



/**
* Evaluates the parameter with matching \a name to a file/folder name.
*/
Expand Down Expand Up @@ -979,6 +977,16 @@ class CORE_EXPORT QgsProcessingAlgorithm
*/
QDateTime parameterAsDateTime( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );

/**
* Evaluates the parameter with matching \a name to a point cloud layer.
*
* 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;

/**
* Returns a user-friendly string to use as an error when a source parameter could
Expand Down
97 changes: 93 additions & 4 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -30,6 +30,7 @@
#include "qgsrasterfilewriter.h"
#include "qgsvectorlayer.h"
#include "qgsmeshlayer.h"
#include "qgspointcloudlayer.h"
#include "qgsapplication.h"
#include "qgslayoutmanager.h"
#include "qgsprintlayout.h"
Expand Down Expand Up @@ -2156,6 +2157,17 @@ QString QgsProcessingParameters::parameterAsDatabaseTableName( const QgsProcessi
return parameterAsString( definition, value, context );
}

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

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


QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromVariantMap( const QVariantMap &map )
{
QString type = map.value( QStringLiteral( "parameter_type" ) ).toString();
Expand Down Expand Up @@ -2223,6 +2235,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromVariantM
def.reset( new QgsProcessingParameterColor( name ) );
else if ( type == QgsProcessingParameterCoordinateOperation::typeName() )
def.reset( new QgsProcessingParameterCoordinateOperation( name ) );
else if ( type == QgsProcessingParameterPointCloudLayer::typeName() )
def.reset( new QgsProcessingParameterPointCloudLayer( name ) );
else
{
QgsProcessingParameterType *paramType = QgsApplication::instance()->processingRegistry()->parameterType( type );
Expand Down Expand Up @@ -2333,6 +2347,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromScriptCo
return QgsProcessingParameterDatabaseSchema::fromScriptCode( name, description, isOptional, definition );
else if ( type == QLatin1String( "databasetable" ) )
return QgsProcessingParameterDatabaseTable::fromScriptCode( name, description, isOptional, definition );
else if ( type == QLatin1String( "pointcloud" ) )
return QgsProcessingParameterPointCloudLayer::fromScriptCode( name, description, isOptional, definition );

return nullptr;
}
Expand Down Expand Up @@ -5004,10 +5020,8 @@ QgsProcessingParameterVectorLayer *QgsProcessingParameterVectorLayer::fromScript
return new QgsProcessingParameterVectorLayer( name, description, QList< int>(), definition.isEmpty() ? QVariant() : definition, isOptional );
}

QgsProcessingParameterMeshLayer::QgsProcessingParameterMeshLayer( const QString &name,
const QString &description,
const QVariant &defaultValue,
bool optional )
QgsProcessingParameterMeshLayer::QgsProcessingParameterMeshLayer( const QString &name, const QString &description,
const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
{

Expand Down Expand Up @@ -8157,3 +8171,78 @@ void QgsProcessingParameterDatabaseTable::setAllowNewTableNames( bool allowNewTa
{
mAllowNewTableNames = allowNewTableNames;
}

QgsProcessingParameterPointCloudLayer::QgsProcessingParameterPointCloudLayer( const QString &name, const QString &description,
const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
{
}

QgsProcessingParameterDefinition *QgsProcessingParameterPointCloudLayer::clone() const
{
return new QgsProcessingParameterPointCloudLayer( *this );
}

bool QgsProcessingParameterPointCloudLayer::checkValueIsAcceptable( const QVariant &v, QgsProcessingContext *context ) const
{
if ( !v.isValid() )
return mFlags & FlagOptional;

QVariant var = v;

if ( var.canConvert<QgsProperty>() )
{
QgsProperty p = var.value< QgsProperty >();
if ( p.propertyType() == QgsProperty::StaticProperty )
{
var = p.staticValue();
}
else
{
return true;
}
}

if ( qobject_cast< QgsPointCloudLayer * >( qvariant_cast<QObject *>( var ) ) )
return true;

if ( var.type() != QVariant::String || var.toString().isEmpty() )
return mFlags & FlagOptional;

if ( !context )
{
// that's as far as we can get without a context
return true;
}

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

return false;
}

QString QgsProcessingParameterPointCloudLayer::valueAsPythonString( const QVariant &val, QgsProcessingContext &context ) const
{
if ( !val.isValid() )
return QStringLiteral( "None" );

if ( val.canConvert<QgsProperty>() )
return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );

QVariantMap p;
p.insert( name(), val );
QgsPointCloudLayer *layer = QgsProcessingParameters::parameterAsPointCloudLayer( this, p, context );
return layer ? QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer->source() ) )
: QgsProcessingUtils::stringToPythonLiteral( val.toString() );
}

QString QgsProcessingParameterPointCloudLayer::createFileFilter() const
{
return QgsProviderRegistry::instance()->filePointCloudFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
}

QgsProcessingParameterPointCloudLayer *QgsProcessingParameterPointCloudLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
{
return new QgsProcessingParameterPointCloudLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
}

0 comments on commit f801cb6

Please sign in to comment.