Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
split thin algorithm into two
  • Loading branch information
alexbruy committed May 2, 2023
1 parent 89015d8 commit 694759c
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 70 deletions.
3 changes: 2 additions & 1 deletion src/analysis/CMakeLists.txt
Expand Up @@ -436,8 +436,9 @@ if (WITH_PDAL AND PDAL_2_5_OR_HIGHER)
processing/pdal/qgsalgorithmpdalinformation.cpp
processing/pdal/qgsalgorithmpdalmerge.cpp
processing/pdal/qgsalgorithmpdalreproject.cpp
processing/pdal/qgsalgorithmpdalthinbydecimate.cpp
processing/pdal/qgsalgorithmpdalthinbyradius.cpp
processing/pdal/qgsalgorithmpdaltile.cpp
processing/pdal/qgsalgorithmpdalthin.cpp
)

set(QGIS_ANALYSIS_HDRS ${QGIS_ANALYSIS_HDRS}
Expand Down
94 changes: 94 additions & 0 deletions src/analysis/processing/pdal/qgsalgorithmpdalthinbydecimate.cpp
@@ -0,0 +1,94 @@
/***************************************************************************
qgsalgorithmpdalthinbydecimate.cpp
---------------------
begin : May 2023
copyright : (C) 2023 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsalgorithmpdalthinbydecimate.h"

#include "qgsrunprocess.h"
#include "qgspointcloudlayer.h"

///@cond PRIVATE

QString QgsPdalThinByDecimateAlgorithm::name() const
{
return QStringLiteral( "thinbydecimate" );
}

QString QgsPdalThinByDecimateAlgorithm::displayName() const
{
return QObject::tr( "Thin (by skipping points)" );
}

QString QgsPdalThinByDecimateAlgorithm::group() const
{
return QObject::tr( "Point cloud data management" );
}

QString QgsPdalThinByDecimateAlgorithm::groupId() const
{
return QStringLiteral( "pointclouddatamanagement" );
}

QStringList QgsPdalThinByDecimateAlgorithm::tags() const
{
return QObject::tr( "pdal,lidar,thin,reduce,decrease,size,decimate,skip" ).split( ',' );
}

QString QgsPdalThinByDecimateAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm creates a thinned version of the point cloud by keeping only every N-th point." );
}

QgsPdalThinByDecimateAlgorithm *QgsPdalThinByDecimateAlgorithm::createInstance() const
{
return new QgsPdalThinByDecimateAlgorithm();
}

void QgsPdalThinByDecimateAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterPointCloudLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "POINTS_NUMBER" ), QObject::tr( "Number of points to skip" ), QgsProcessingParameterNumber::Integer, 1, false, 1 ) );
createCommonParameters();
addParameter( new QgsProcessingParameterPointCloudDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Thinned (by decimation)" ) ) );
}

QStringList QgsPdalThinByDecimateAlgorithm::createArgumentLists( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
Q_UNUSED( feedback );

QgsPointCloudLayer *layer = parameterAsPointCloudLayer( parameters, QStringLiteral( "INPUT" ), context, QgsProcessing::LayerOptionsFlag::SkipIndexGeneration );
if ( !layer )
throw QgsProcessingException( invalidPointCloudError( parameters, QStringLiteral( "INPUT" ) ) );

const QString outputName = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
QString outputFile = fixOutputFileName( layer->source(), outputName, context );
setOutputValue( QStringLiteral( "OUTPUT" ), outputFile );

int step = parameterAsInt( parameters, QStringLiteral( "POINTS_NUMBER" ), context );

QStringList args = { QStringLiteral( "thin" ),
QStringLiteral( "--input=%1" ).arg( layer->source() ),
QStringLiteral( "--output=%1" ).arg( outputFile ),
QStringLiteral( "--mode=every-nth" ),
QStringLiteral( "--step-every-nth=%1" ).arg( step )
};

applyCommonParameters( args, layer->crs(), parameters, context );
applyThreadsParameter( args, context );
return args;
}

///@endcond
54 changes: 54 additions & 0 deletions src/analysis/processing/pdal/qgsalgorithmpdalthinbydecimate.h
@@ -0,0 +1,54 @@
/***************************************************************************
qgsalgorithmpdalthinbyradius.h
---------------------
begin : February 2023
copyright : (C) 2023 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSALGORITHMPDALTHINBYDECIMATE_H
#define QGSALGORITHMPDALTHINBYDECIMATE_H

#define SIP_NO_FILE

#include "qgis_sip.h"
#include "qgspdalalgorithmbase.h"

///@cond PRIVATE

/**
* Native point cloud thin by decimation (skipping points).
*/
class QgsPdalThinByDecimateAlgorithm : public QgsPdalAlgorithmBase
{

public:

QgsPdalThinByDecimateAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
QString group() const override;
QString groupId() const override;
QStringList tags() const override;
QString shortHelpString() const override;
QgsPdalThinByDecimateAlgorithm *createInstance() const override SIP_FACTORY;

QStringList createArgumentLists( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;

friend class TestQgsProcessingPdalAlgs;
};

///@endcond PRIVATE

#endif // QGSALGORITHMPDALTHINBYDECIMATE_H
@@ -1,5 +1,5 @@
/***************************************************************************
qgsalgorithmpdalthin.cpp
qgsalgorithmpdalthinbyradius.cpp
---------------------
begin : February 2023
copyright : (C) 2023 by Alexander Bruy
Expand All @@ -15,59 +15,57 @@
* *
***************************************************************************/

#include "qgsalgorithmpdalthin.h"
#include "qgsalgorithmpdalthinbyradius.h"

#include "qgsrunprocess.h"
#include "qgspointcloudlayer.h"

///@cond PRIVATE

QString QgsPdalThinAlgorithm::name() const
QString QgsPdalThinByRadiusAlgorithm::name() const
{
return QStringLiteral( "thin" );
return QStringLiteral( "thinbyradius" );
}

QString QgsPdalThinAlgorithm::displayName() const
QString QgsPdalThinByRadiusAlgorithm::displayName() const
{
return QObject::tr( "Thin" );
return QObject::tr( "Thin (by sampling radius)" );
}

QString QgsPdalThinAlgorithm::group() const
QString QgsPdalThinByRadiusAlgorithm::group() const
{
return QObject::tr( "Point cloud data management" );
}

QString QgsPdalThinAlgorithm::groupId() const
QString QgsPdalThinByRadiusAlgorithm::groupId() const
{
return QStringLiteral( "pointclouddatamanagement" );
}

QStringList QgsPdalThinAlgorithm::tags() const
QStringList QgsPdalThinByRadiusAlgorithm::tags() const
{
return QObject::tr( "pdal,lidar,thin,reduce,decrease,size" ).split( ',' );
return QObject::tr( "pdal,lidar,thin,reduce,decrease,size,sampling,radius" ).split( ',' );
}

QString QgsPdalThinAlgorithm::shortHelpString() const
QString QgsPdalThinByRadiusAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm creates a thinned version of the point cloud by only keeping every N-th or by performing sampling by distance point." );
return QObject::tr( "This algorithm creates a thinned version of the point cloud by performing sampling by distance point." );
}

QgsPdalThinAlgorithm *QgsPdalThinAlgorithm::createInstance() const
QgsPdalThinByRadiusAlgorithm *QgsPdalThinByRadiusAlgorithm::createInstance() const
{
return new QgsPdalThinAlgorithm();
return new QgsPdalThinByRadiusAlgorithm();
}

void QgsPdalThinAlgorithm::initAlgorithm( const QVariantMap & )
void QgsPdalThinByRadiusAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterPointCloudLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterEnum( QStringLiteral( "MODE" ), QObject::tr( "Mode" ), QStringList() << QObject::tr( "Every N-th" ) << QObject::tr( "Sample" ), false, 0 ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "POINTS_NUMBER" ), QObject::tr( "Number of points to skip" ), QgsProcessingParameterNumber::Integer, QVariant(), true, 1 ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "SAMPLING_RADIUS" ), QObject::tr( "Sampling radius (in map units)" ), QgsProcessingParameterNumber::Double, QVariant(), true, 1e-9 ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "SAMPLING_RADIUS" ), QObject::tr( "Sampling radius (in map units)" ), QgsProcessingParameterNumber::Double, 1.0, false, 1e-9 ) );
createCommonParameters();
addParameter( new QgsProcessingParameterPointCloudDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Thinned" ) ) );
addParameter( new QgsProcessingParameterPointCloudDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Thinned (by radius)" ) ) );
}

QStringList QgsPdalThinAlgorithm::createArgumentLists( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
QStringList QgsPdalThinByRadiusAlgorithm::createArgumentLists( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
Q_UNUSED( feedback );

Expand All @@ -79,30 +77,13 @@ QStringList QgsPdalThinAlgorithm::createArgumentLists( const QVariantMap &parame
QString outputFile = fixOutputFileName( layer->source(), outputName, context );
setOutputValue( QStringLiteral( "OUTPUT" ), outputFile );

int mode = parameterAsInt( parameters, QStringLiteral( "MODE" ), context );
double step;
if ( mode == 0 )
{
step = parameterAsInt( parameters, QStringLiteral( "POINTS_NUMBER" ), context );
if ( step == 0 )
{
throw QgsProcessingException( QObject::tr( "Point count should be greater or equal to 1 for 'Every N-th' mode." ) );
}
}
else if ( mode == 1 )
{
step = parameterAsDouble( parameters, QStringLiteral( "SAMPLING_RADIUS" ), context );
if ( step == 0 )
{
throw QgsProcessingException( QObject::tr( "Sampling radius should be greater that 0 for 'Sample' mode." ) );
}
}
double step = parameterAsDouble( parameters, QStringLiteral( "SAMPLING_RADIUS" ), context );

QStringList args = { QStringLiteral( "thin" ),
QStringLiteral( "--input=%1" ).arg( layer->source() ),
QStringLiteral( "--output=%1" ).arg( outputFile ),
QStringLiteral( "--mode=%1" ).arg( mode == 0 ? QStringLiteral( "every-nth" ) : QStringLiteral( "sample" ) ),
QStringLiteral( "--%1=%2" ).arg( mode == 0 ? QStringLiteral( "step-every-nth" ) : QStringLiteral( "step-sample" ) ).arg( step )
QStringLiteral( "--mode=sample" ),
QStringLiteral( "--step-sample=%1" ).arg( step )
};

applyCommonParameters( args, layer->crs(), parameters, context );
Expand Down
@@ -1,5 +1,5 @@
/***************************************************************************
qgsalgorithmpdalthin.h
qgsalgorithmpdalthinbyradius.h
---------------------
begin : February 2023
copyright : (C) 2023 by Alexander Bruy
Expand All @@ -15,8 +15,8 @@
* *
***************************************************************************/

#ifndef QGSALGORITHMPDALTHIN_H
#define QGSALGORITHMPDALTHIN_H
#ifndef QGSALGORITHMPDALTHINBYRADIUS_H
#define QGSALGORITHMPDALTHINBYRADIUS_H

#define SIP_NO_FILE

Expand All @@ -26,22 +26,22 @@
///@cond PRIVATE

/**
* Native point cloud thin algorithm.
* Native point cloud thin by radius algorithm.
*/
class QgsPdalThinAlgorithm : public QgsPdalAlgorithmBase
class QgsPdalThinByRadiusAlgorithm : public QgsPdalAlgorithmBase
{

public:

QgsPdalThinAlgorithm() = default;
QgsPdalThinByRadiusAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
QString group() const override;
QString groupId() const override;
QStringList tags() const override;
QString shortHelpString() const override;
QgsPdalThinAlgorithm *createInstance() const override SIP_FACTORY;
QgsPdalThinByRadiusAlgorithm *createInstance() const override SIP_FACTORY;

QStringList createArgumentLists( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
Expand All @@ -51,4 +51,4 @@ class QgsPdalThinAlgorithm : public QgsPdalAlgorithmBase

///@endcond PRIVATE

#endif // QGSALGORITHMPDALTHIN_H
#endif // QGSALGORITHMPDALTHINBYRADIUS_H
6 changes: 4 additions & 2 deletions src/analysis/processing/pdal/qgspdalalgorithms.cpp
Expand Up @@ -32,7 +32,8 @@
#include "qgsalgorithmpdalinformation.h"
#include "qgsalgorithmpdalmerge.h"
#include "qgsalgorithmpdalreproject.h"
#include "qgsalgorithmpdalthin.h"
#include "qgsalgorithmpdalthinbydecimate.h"
#include "qgsalgorithmpdalthinbyradius.h"
#include "qgsalgorithmpdaltile.h"

///@cond PRIVATE
Expand Down Expand Up @@ -103,7 +104,8 @@ void QgsPdalAlgorithms::loadAlgorithms()
addAlgorithm( new QgsPdalInformationAlgorithm() );
addAlgorithm( new QgsPdalMergeAlgorithm() );
addAlgorithm( new QgsPdalReprojectAlgorithm() );
addAlgorithm( new QgsPdalThinAlgorithm() );
addAlgorithm( new QgsPdalThinByDecimateAlgorithm() );
addAlgorithm( new QgsPdalThinByRadiusAlgorithm() );
addAlgorithm( new QgsPdalTileAlgorithm() );
}

Expand Down

0 comments on commit 694759c

Please sign in to comment.