Skip to content

Commit

Permalink
add parameter for centroid based distance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
root676 authored and nyalldawson committed Oct 24, 2021
1 parent 57e2248 commit 95ff6a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/analysis/processing/qgsalgorithmshortestline.cpp
Expand Up @@ -72,6 +72,7 @@ void QgsShortestLineAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "SOURCE" ), QObject::tr( "Source layer" ), QList<int>() << QgsProcessing::TypeVectorAnyGeometry ) );
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "DESTINATION" ), QObject::tr( "Destination layer" ), QList<int>() << QgsProcessing::TypeVectorAnyGeometry ) );
addParameter( new QgsProcessingParameterEnum( QStringLiteral( "METHOD" ), QObject::tr( "Method" ), QStringList() << "distance to nearest point on geometry" << "distance to geometry centroid", false, QVariant( "boundary" ) ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "NEIGHBORS" ), QObject::tr( "Maximum number of neighbors" ), QgsProcessingParameterNumber::Integer, 1, false, 1 ) );
addParameter( new QgsProcessingParameterDistance( QStringLiteral( "DISTANCE" ), QObject::tr( "Maximum distance" ), QVariant(), QString( "SOURCE" ), true ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Shortest lines" ) ) );
Expand All @@ -87,6 +88,8 @@ bool QgsShortestLineAlgorithm::prepareAlgorithm( const QVariantMap &parameters,
if ( !mSource )
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "DESTINATION" ) ) );

mMethod = parameterAsInt( parameters, QStringLiteral( "METHOD" ), context );

mKNeighbors = parameterAsInt( parameters, QStringLiteral( "NEIGHBORS" ), context );

double paramMaxDist = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context ); //defaults to zero if not set
Expand Down Expand Up @@ -128,7 +131,6 @@ QVariantMap QgsShortestLineAlgorithm::processAlgorithm( const QVariantMap &param
if ( feedback->isCanceled() )
break;


const QgsGeometry sourceGeom = sourceFeature.geometry();
QgsFeatureIds nearestIds = qgis::listToSet( idx.nearestNeighbor( sourceGeom, mKNeighbors, mMaxDistance ) );

Expand All @@ -139,7 +141,16 @@ QVariantMap QgsShortestLineAlgorithm::processAlgorithm( const QVariantMap &param
QgsFeature destinationFeature;
while ( destinationIterator.nextFeature( destinationFeature ) )
{
const QgsGeometry destinationGeom = destinationFeature.geometry();
QgsGeometry destinationGeom;

if( mMethod == 1 )
{
destinationGeom = destinationFeature.geometry().centroid();
}
else
{
destinationGeom = destinationFeature.geometry();
}

const QgsGeometry shortestLine = sourceGeom.shortestLine( destinationGeom );
double dist = da.measureLength( shortestLine );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmshortestline.h
Expand Up @@ -47,6 +47,7 @@ class QgsShortestLineAlgorithm : public QgsProcessingAlgorithm
private:
std::unique_ptr< QgsFeatureSource > mSource;
std::unique_ptr< QgsFeatureSource > mDestination;
int mMethod = 0;
long long mKNeighbors = 1;
double mMaxDistance = 0.0;

Expand Down

0 comments on commit 95ff6a0

Please sign in to comment.