Skip to content
Permalink
Browse files
add parameter for centroid based distance calculation
  • Loading branch information
root676 authored and nyalldawson committed Oct 24, 2021
1 parent 57e2248 commit 95ff6a0545f71bbc67aa2def8bcbb42d0599a66f
Showing with 14 additions and 2 deletions.
  1. +13 −2 src/analysis/processing/qgsalgorithmshortestline.cpp
  2. +1 −0 src/analysis/processing/qgsalgorithmshortestline.h
@@ -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" ) ) );
@@ -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
@@ -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 ) );

@@ -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 );
@@ -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;

0 comments on commit 95ff6a0

Please sign in to comment.