Skip to content

Commit

Permalink
[processing] add filter by rectangle (extent) option to the point cloud
Browse files Browse the repository at this point in the history
filter algorithm
  • Loading branch information
alexbruy authored and wonder-sk committed Apr 14, 2023
1 parent 47c5922 commit 6207574
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
38 changes: 34 additions & 4 deletions src/analysis/processing/pdal/qgsalgorithmpdalfilter.cpp
Expand Up @@ -44,12 +44,12 @@ QString QgsPdalFilterAlgorithm::groupId() const

QStringList QgsPdalFilterAlgorithm::tags() const
{
return QObject::tr( "filter,subset,extract,dimension,attribute" ).split( ',' );
return QObject::tr( "filter,subset,extract,dimension,attribute,extent,bounds,rectangle" ).split( ',' );
}

QString QgsPdalFilterAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm extracts point from the input point cloud which match PDAL expression." );
return QObject::tr( "This algorithm extracts point from the input point cloud which match PDAL expression and/or are inside of a cropping rectangle." );
}

QgsPdalFilterAlgorithm *QgsPdalFilterAlgorithm::createInstance() const
Expand All @@ -60,7 +60,8 @@ QgsPdalFilterAlgorithm *QgsPdalFilterAlgorithm::createInstance() const
void QgsPdalFilterAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterPointCloudLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "FILTER" ), QObject::tr( "Filter expression" ) ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "FILTER_EXPRESSION" ), QObject::tr( "Filter expression" ), QVariant(), false, true ) );
addParameter( new QgsProcessingParameterExtent( QStringLiteral( "FILTER_EXTENT" ), QObject::tr( "Cropping extent" ), QVariant(), true ) );
addParameter( new QgsProcessingParameterPointCloudDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output" ) ) );
}

Expand All @@ -77,10 +78,39 @@ QStringList QgsPdalFilterAlgorithm::createArgumentLists( const QVariantMap &para

QStringList args = { QStringLiteral( "translate" ),
QStringLiteral( "--input=%1" ).arg( layer->source() ),
QStringLiteral( "--filter=%1" ).arg( parameterAsString( parameters, QStringLiteral( "FILTER" ), context ) ),
QStringLiteral( "--output=%1" ).arg( outputFile )
};


const QString filterExpression = parameterAsString( parameters, QStringLiteral( "FILTER_EXPRESSION" ), context ).trimmed();
if ( !filterExpression.isEmpty() )
{
args << QStringLiteral( "--filter=%1" ).arg( filterExpression );
}

if ( parameters.value( QStringLiteral( "FILTER_EXTENT" ) ).isValid() )
{
if ( layer->crs().isValid() )
{
const QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "FILTER_EXTENT" ), context, layer->crs() );
args << QStringLiteral( "--bounds=([%1, %2], [%3, %4])" )
.arg( extent.xMinimum() )
.arg( extent.xMaximum() )
.arg( extent.yMinimum() )
.arg( extent.yMaximum() );

}
else
{
const QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "FILTER_EXTENT" ), context );
args << QStringLiteral( "--bounds=([%1, %2], [%3, %4])" )
.arg( extent.xMinimum() )
.arg( extent.xMaximum() )
.arg( extent.yMinimum() )
.arg( extent.yMaximum() );
}
}

applyThreadsParameter( args );
return args;
}
Expand Down
20 changes: 18 additions & 2 deletions tests/src/analysis/testqgsprocessingpdalalgs.cpp
Expand Up @@ -844,23 +844,39 @@ void TestQgsProcessingPdalAlgs::filter()

QVariantMap parameters;
parameters.insert( QStringLiteral( "INPUT" ), mPointCloudLayerPath );
parameters.insert( QStringLiteral( "FILTER" ), QStringLiteral( "Classification == 7 || Classification == 8" ) );
parameters.insert( QStringLiteral( "OUTPUT" ), outputPointCloud );

QStringList args = alg->createArgumentLists( parameters, *context, &feedback );
QCOMPARE( args, QStringList() << QStringLiteral( "translate" )
<< QStringLiteral( "--input=%1" ).arg( mPointCloudLayerPath )
<< QStringLiteral( "--output=%1" ).arg( outputPointCloud )
);

parameters.insert( QStringLiteral( "FILTER_EXPRESSION" ), QStringLiteral( "Classification == 7 || Classification == 8" ) );
args = alg->createArgumentLists( parameters, *context, &feedback );
QCOMPARE( args, QStringList() << QStringLiteral( "translate" )
<< QStringLiteral( "--input=%1" ).arg( mPointCloudLayerPath )
<< QStringLiteral( "--output=%1" ).arg( outputPointCloud )
<< QStringLiteral( "--filter=Classification == 7 || Classification == 8" )
);

parameters.insert( QStringLiteral( "FILTER_EXTENT" ), QgsRectangle( 1, 2, 3, 4 ) );
args = alg->createArgumentLists( parameters, *context, &feedback );
QCOMPARE( args, QStringList() << QStringLiteral( "translate" )
<< QStringLiteral( "--input=%1" ).arg( mPointCloudLayerPath )
<< QStringLiteral( "--output=%1" ).arg( outputPointCloud )
<< QStringLiteral( "--filter=Classification == 7 || Classification == 8" )
<< QStringLiteral( "--bounds=([1, 3], [2, 4])" )
);

// set max threads to 2, a --threads argument should be added
QgsSettings().setValue( QStringLiteral( "/Processing/Configuration/MAX_THREADS" ), 2 );
args = alg->createArgumentLists( parameters, *context, &feedback );
QCOMPARE( args, QStringList() << QStringLiteral( "translate" )
<< QStringLiteral( "--input=%1" ).arg( mPointCloudLayerPath )
<< QStringLiteral( "--filter=Classification == 7 || Classification == 8" )
<< QStringLiteral( "--output=%1" ).arg( outputPointCloud )
<< QStringLiteral( "--filter=Classification == 7 || Classification == 8" )
<< QStringLiteral( "--bounds=([1, 3], [2, 4])" )
<< QStringLiteral( "--threads=2" )
);
}
Expand Down

0 comments on commit 6207574

Please sign in to comment.