Skip to content

Commit

Permalink
add reproject algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy authored and wonder-sk committed Mar 22, 2023
1 parent 3e6cffb commit 83efb03
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -425,6 +425,7 @@ if (WITH_PDAL)

processing/pdal/qgsalgorithmpdalinformation.cpp
processing/pdal/qgsalgorithmpdalconvertformat.cpp
processing/pdal/qgsalgorithmpdalreproject.cpp
)

set(QGIS_ANALYSIS_HDRS ${QGIS_ANALYSIS_HDRS}
Expand Down
90 changes: 90 additions & 0 deletions src/analysis/processing/pdal/qgsalgorithmpdalreproject.cpp
@@ -0,0 +1,90 @@
/***************************************************************************
qgsalgorithmpdalreproject.cpp
---------------------
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. *
* *
***************************************************************************/

#include "qgsalgorithmpdalreproject.h"

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

///@cond PRIVATE

QString QgsPdalReprojectAlgorithm::name() const
{
return QStringLiteral( "reproject" );
}

QString QgsPdalReprojectAlgorithm::displayName() const
{
return QObject::tr( "Reproject" );
}

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

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

QStringList QgsPdalReprojectAlgorithm::tags() const
{
return QObject::tr( "assign,set,transform,reproject,crs,srs" ).split( ',' );
}

QString QgsPdalReprojectAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm reprojects point cloud to a different coordinate reference system." );
}

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

void QgsPdalReprojectAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterPointCloudLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Target CRS" ), QStringLiteral( "EPSG:4326" ) ) );
addParameter( new QgsProcessingParameterPointCloudDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output layer" ) ) );
}

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

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

const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
setOutputValue( QStringLiteral( "OUTPUT" ), outputFile );

QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );

QStringList args = { QStringLiteral( "translate" ),
QStringLiteral( "--input=%1" ).arg( layer->source() ),
QStringLiteral( "--output=%1" ).arg( outputFile ),
QStringLiteral( "--transform-crs=%1" ).arg( crs.authid() )
};

addThreadsParameter( args );
return args;
}

///@endcond
54 changes: 54 additions & 0 deletions src/analysis/processing/pdal/qgsalgorithmpdalreproject.h
@@ -0,0 +1,54 @@
/***************************************************************************
qgsalgorithmpdalreproject.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 QGSALGORITHMPDALREPROJECT_H
#define QGSALGORITHMPDALREPROJECT_H

#define SIP_NO_FILE

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

///@cond PRIVATE

/**
* Native point cloud reprojection algorithm.
*/
class QgsPdalReprojectAlgorithm : public QgsPdalAlgorithmBase
{

public:

QgsPdalReprojectAlgorithm() = 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;
QgsPdalReprojectAlgorithm *createInstance() const override SIP_FACTORY;

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

friend class TestQgsProcessingPdalAlgs;
};

///@endcond PRIVATE

#endif // QGSALGORITHMPDALREPROJECT_H
2 changes: 2 additions & 0 deletions src/analysis/processing/pdal/qgspdalalgorithms.cpp
Expand Up @@ -21,6 +21,7 @@

#include "qgsalgorithmpdalinformation.h"
#include "qgsalgorithmpdalconvertformat.h"
#include "qgsalgorithmpdalreproject.h"

///@cond PRIVATE

Expand Down Expand Up @@ -79,6 +80,7 @@ void QgsPdalAlgorithms::loadAlgorithms()

addAlgorithm( new QgsPdalInformationAlgorithm() );
addAlgorithm( new QgsPdalConvertFormatAlgorithm() );
addAlgorithm( new QgsPdalReprojectAlgorithm() );
}

///@endcond
35 changes: 35 additions & 0 deletions tests/src/analysis/testqgsprocessingpdalalgs.cpp
Expand Up @@ -36,6 +36,7 @@ class TestQgsProcessingPdalAlgs: public QObject

void info();
void convertFormat();
void reproject();

private:
QString mPointCloudLayerPath;
Expand Down Expand Up @@ -121,5 +122,39 @@ void TestQgsProcessingPdalAlgs::convertFormat()
);
}

void TestQgsProcessingPdalAlgs::reproject()
{
QgsPdalAlgorithmBase *alg = const_cast<QgsPdalAlgorithmBase *>( static_cast< const QgsPdalAlgorithmBase * >( QgsApplication::processingRegistry()->algorithmById( QStringLiteral( "pdal:reproject" ) ) ) );

std::unique_ptr< QgsProcessingContext > context = std::make_unique< QgsProcessingContext >();
context->setProject( QgsProject::instance() );

QgsProcessingFeedback feedback;

const QString outputPointCloud = QDir::tempPath() + "/reprojected.laz";

QVariantMap parameters;
parameters.insert( QStringLiteral( "INPUT" ), mPointCloudLayerPath );
parameters.insert( QStringLiteral( "CRS" ), QStringLiteral( "EPSG:4326" ) );
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 )
<< QStringLiteral( "--transform-crs=%1" ).arg( QStringLiteral( "EPSG:4326" ) )
);

// 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( "--output=%1" ).arg( outputPointCloud )
<< QStringLiteral( "--transform-crs=%1" ).arg( QStringLiteral( "EPSG:4326" ) )
<< QStringLiteral( "--threads=2" )
);
}

QGSTEST_MAIN( TestQgsProcessingPdalAlgs )
#include "testqgsprocessingpdalalgs.moc"

0 comments on commit 83efb03

Please sign in to comment.