Skip to content

Commit

Permalink
Change points to lines to points to paths
Browse files Browse the repository at this point in the history
  • Loading branch information
uclaros authored and nyalldawson committed Jan 18, 2021
1 parent 3272a88 commit 7473e84
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/analysis/CMakeLists.txt
Expand Up @@ -123,7 +123,7 @@ set(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmpointtolayer.cpp
processing/qgsalgorithmpointsalonggeometry.cpp
processing/qgsalgorithmpointslayerfromtable.cpp
processing/qgsalgorithmpointstolines.cpp
processing/qgsalgorithmpointstopaths.cpp
processing/qgsalgorithmpoleofinaccessibility.cpp
processing/qgsalgorithmpolygonize.cpp
processing/qgsalgorithmprojectpointcartesian.cpp
Expand Down
@@ -1,5 +1,5 @@
/***************************************************************************
qgsalgorithmdpointstolines.cpp
qgsalgorithmdpointstopaths.cpp
---------------------
begin : November 2020
copyright : (C) 2020 by Stefanos Natsis
Expand All @@ -15,22 +15,22 @@
* *
***************************************************************************/

#include "qgsalgorithmpointstolines.h"
#include "qgsalgorithmpointstopaths.h"
#include "qgsvectorlayer.h"

///@cond PRIVATE

QString QgsPointsToLinesAlgorithm::name() const
QString QgsPointsToPathsAlgorithm::name() const
{
return QStringLiteral( "pointstolines" );
return QStringLiteral( "pointstopaths" );
}

QString QgsPointsToLinesAlgorithm::displayName() const
QString QgsPointsToPathsAlgorithm::displayName() const
{
return QObject::tr( "Points to lines" );
return QObject::tr( "Points to paths" );
}

QString QgsPointsToLinesAlgorithm::shortHelpString() const
QString QgsPointsToPathsAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm takes a point layer and connects its features creating a new line layer.\n\n"
"An attribute or expression may be specified to define the order the points should be connected. "
Expand All @@ -40,50 +40,50 @@ QString QgsPointsToLinesAlgorithm::shortHelpString() const
"An attribute or expression can be selected to group points having the same value into the same resulting line." );
}

QStringList QgsPointsToLinesAlgorithm::tags() const
QStringList QgsPointsToPathsAlgorithm::tags() const
{
return QObject::tr( "create,lines,points,connect,convert,join" ).split( ',' );
}

QString QgsPointsToLinesAlgorithm::group() const
QString QgsPointsToPathsAlgorithm::group() const
{
return QObject::tr( "Vector geometry" );
}

QString QgsPointsToLinesAlgorithm::groupId() const
QString QgsPointsToPathsAlgorithm::groupId() const
{
return QStringLiteral( "vectorgeometry" );
}

void QgsPointsToLinesAlgorithm::initAlgorithm( const QVariantMap & )
void QgsPointsToPathsAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ),
QObject::tr( "Input layer" ), QList< int >() << QgsProcessing::TypeVectorPoint ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "CLOSE_LINES" ),
QObject::tr( "Create closed lines" ), false, true ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "CLOSE_PATHS" ),
QObject::tr( "Create closed paths" ), false, true ) );
addParameter( new QgsProcessingParameterExpression( QStringLiteral( "ORDER_EXPRESSION" ),
QObject::tr( "Order expression" ), QVariant(), QStringLiteral( "INPUT" ), true ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "NATURAL_SORT" ),
QObject::tr( "Sort text containing numbers naturally" ), false, true ) );
addParameter( new QgsProcessingParameterExpression( QStringLiteral( "GROUP_EXPRESSION" ),
QObject::tr( "Line group expression" ), QVariant(), QStringLiteral( "INPUT" ), true ) );
QObject::tr( "Path group expression" ), QVariant(), QStringLiteral( "INPUT" ), true ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ),
QObject::tr( "Lines" ), QgsProcessing::TypeVectorLine ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "NUM_LINES" ), QObject::tr( "Number of lines" ) ) );
QObject::tr( "Paths" ), QgsProcessing::TypeVectorLine ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "NUM_PATHS" ), QObject::tr( "Number of paths" ) ) );
}

QgsPointsToLinesAlgorithm *QgsPointsToLinesAlgorithm::createInstance() const
QgsPointsToPathsAlgorithm *QgsPointsToPathsAlgorithm::createInstance() const
{
return new QgsPointsToLinesAlgorithm();
return new QgsPointsToPathsAlgorithm();
}

QVariantMap QgsPointsToLinesAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
QVariantMap QgsPointsToPathsAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );

const bool closeLines = parameterAsBool( parameters, QStringLiteral( "CLOSE_LINES" ), context );
const bool closePaths = parameterAsBool( parameters, QStringLiteral( "CLOSE_PATHS" ), context );

QString orderExpressionString = parameterAsString( parameters, QStringLiteral( "ORDER_EXPRESSION" ), context );
// If no order expression is given, default to the fid
Expand Down Expand Up @@ -155,10 +155,10 @@ QVariantMap QgsPointsToLinesAlgorithm::processAlgorithm( const QVariantMap &para
++currentPoint;
}

int lineCount = 0;
int pathCount = 0;
currentPoint = 0;
QHashIterator< QVariant, QVector< QPair< QVariant, const QgsPoint * > > > hit( allPoints );
feedback->setProgressText( QObject::tr( "Creating lines" ) );
feedback->setProgressText( QObject::tr( "Creating paths" ) );
while ( hit.hasNext() )
{
hit.next();
Expand Down Expand Up @@ -190,38 +190,38 @@ QVariantMap QgsPointsToLinesAlgorithm::processAlgorithm( const QVariantMap &para
}


QVector<QgsPoint> linePoints;
QVector<QgsPoint> pathPoints;
for ( QVector< QPair< QVariant, const QgsPoint * > >::ConstIterator pit = pairs.constBegin(); pit != pairs.constEnd(); ++pit )
{
if ( feedback->isCanceled() )
{
break;
}
feedback->setProgress( currentPoint * totalPoints );
linePoints.append( *pit->second );
pathPoints.append( *pit->second );
++currentPoint;
}
if ( linePoints.size() < 2 )
if ( pathPoints.size() < 2 )
{
feedback->pushInfo( QObject::tr( "Skipping line with group %1 : insufficient vertices" ).arg( hit.key().toString() ) );
feedback->pushInfo( QObject::tr( "Skipping path with group %1 : insufficient vertices" ).arg( hit.key().toString() ) );
continue;
}
if ( closeLines && linePoints.size() > 2 && linePoints.first() != linePoints.last() )
linePoints.append( linePoints.first() );
if ( closePaths && pathPoints.size() > 2 && pathPoints.first() != pathPoints.last() )
pathPoints.append( pathPoints.first() );

QgsFeature outputFeature;
QgsAttributes attrs;
attrs.append( hit.key() );
outputFeature.setGeometry( QgsGeometry::fromPolyline( linePoints ) );
outputFeature.setGeometry( QgsGeometry::fromPolyline( pathPoints ) );
outputFeature.setAttributes( attrs );
sink->addFeature( outputFeature, QgsFeatureSink::FastInsert );
++lineCount;
++pathCount;
}


QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
outputs.insert( QStringLiteral( "NUM_LINESS" ), lineCount );
outputs.insert( QStringLiteral( "NUM_PATHS" ), pathCount );
return outputs;
}

Expand Down
@@ -1,5 +1,5 @@
/***************************************************************************
qgsalgorithmpointstolines.h
qgsalgorithmpointstopaths.h
---------------------
begin : November 2020
copyright : (C) 2020 by Stefanos Natsis
Expand All @@ -15,8 +15,8 @@
* *
***************************************************************************/

#ifndef QGSALGORITHMPOINTSTOLINES_H
#define QGSALGORITHMPOINTSTOLINES_H
#ifndef QGSALGORITHMPOINTSTOPATHTS_H
#define QGSALGORITHMPOINTSTOPATHS_H

#define SIP_NO_FILE

Expand All @@ -28,20 +28,20 @@
/**
* Native points to lines algorithm.
*/
class QgsPointsToLinesAlgorithm : public QgsProcessingAlgorithm
class QgsPointsToPathsAlgorithm : public QgsProcessingAlgorithm
{

public:

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

protected:

Expand All @@ -51,4 +51,4 @@ class QgsPointsToLinesAlgorithm : public QgsProcessingAlgorithm

///@endcond PRIVATE

#endif // QGSALGORITHMPOINTSTOLINES_H
#endif // QGSALGORITHMPOINTSTOPATHS_H
4 changes: 2 additions & 2 deletions src/analysis/processing/qgsnativealgorithms.cpp
Expand Up @@ -123,7 +123,7 @@
#include "qgsalgorithmpointtolayer.h"
#include "qgsalgorithmpointsalonggeometry.h"
#include "qgsalgorithmpointslayerfromtable.h"
#include "qgsalgorithmpointstolines.h"
#include "qgsalgorithmpointstopaths.h"
#include "qgsalgorithmpoleofinaccessibility.h"
#include "qgsalgorithmpolygonize.h"
#include "qgsalgorithmprojectpointcartesian.h"
Expand Down Expand Up @@ -375,7 +375,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsPointToLayerAlgorithm() );
addAlgorithm( new QgsPointsAlongGeometryAlgorithm() );
addAlgorithm( new QgsPointsLayerFromTableAlgorithm() );
addAlgorithm( new QgsPointsToLinesAlgorithm() );
addAlgorithm( new QgsPointsToPathsAlgorithm() );
addAlgorithm( new QgsPoleOfInaccessibilityAlgorithm() );
addAlgorithm( new QgsPolygonizeAlgorithm() );
addAlgorithm( new QgsProjectPointCartesianAlgorithm() );
Expand Down

0 comments on commit 7473e84

Please sign in to comment.