25 QString QgsShortestLineAlgorithm::name()
const
27 return QStringLiteral(
"shortestline" );
30 QString QgsShortestLineAlgorithm::displayName()
const
32 return QObject::tr(
"Shortest line between features" );
35 QStringList QgsShortestLineAlgorithm::tags()
const
37 return QObject::tr(
"distance,shortest,minimum,nearest,closest,proximity" ).split(
',' );
40 QString QgsShortestLineAlgorithm::group()
const
42 return QObject::tr(
"Vector analysis" );
45 QString QgsShortestLineAlgorithm::groupId()
const
47 return QStringLiteral(
"vectoranalysis" );
50 QString QgsShortestLineAlgorithm::shortHelpString()
const
52 return QObject::tr(
"This algorithm creates a line layer as the "
53 "shortest line between the source and the destination layer. "
54 "By default only the first nearest feature of the "
55 "destination layer is taken into account. "
56 "The n-nearest neighboring features number can be specified.\n\n"
57 "If a maximum distance is specified, then only "
58 "features which are closer than this distance will "
59 "be considered.\n\nThe output features will contain all the "
60 "source layer attributes, all the attributes from the n-nearest "
61 "feature and the additional field of the distance.\n\n"
62 "This algorithm uses purely Cartesian calculations for distance, "
63 "and does not consider geodetic or ellipsoid properties when "
64 "determining feature proximity. The measurement and output coordinate "
65 "system is based on the coordinate system of the source layer."
69 QgsShortestLineAlgorithm *QgsShortestLineAlgorithm::createInstance()
const
71 return new QgsShortestLineAlgorithm();
74 void QgsShortestLineAlgorithm::initAlgorithm(
const QVariantMap & )
78 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"METHOD" ), QObject::tr(
"Method" ), QStringList() <<
"Distance to Nearest Point on feature" <<
"Distance to Feature Centroid",
false, 0 ) );
80 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"DISTANCE" ), QObject::tr(
"Maximum distance" ), QVariant(), QString(
"SOURCE" ),
true ) );
86 mSource.reset( parameterAsSource( parameters, QStringLiteral(
"SOURCE" ), context ) );
90 mDestination.reset( parameterAsSource( parameters, QStringLiteral(
"DESTINATION" ), context ) );
94 mMethod = parameterAsInt( parameters, QStringLiteral(
"METHOD" ), context );
96 mKNeighbors = parameterAsInt( parameters, QStringLiteral(
"NEIGHBORS" ), context );
98 mMaxDistance = parameterAsDouble( parameters, QStringLiteral(
"DISTANCE" ), context );
105 if ( mKNeighbors > mDestination->featureCount() )
106 mKNeighbors = mDestination->featureCount();
109 fields.
append(
QgsField( QStringLiteral(
"distance" ), QVariant::Double ) );
112 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
QgsWkbTypes::MultiLineString, mSource->sourceCrs() ) );
117 QHash< QgsFeatureId, QgsAttributes > destinationAttributeCache;
118 double step = mDestination->featureCount() > 0 ? 50.0 / mDestination->featureCount() : 1;
123 if ( feedback-> isCanceled() )
128 destinationAttributeCache.insert( f.
id(), f.
attributes() );
133 step = mSource->featureCount() > 0 ? 50.0 / mSource->featureCount() : 1;
140 while ( sourceIterator.
nextFeature( sourceFeature ) )
146 QgsFeatureIds nearestIds = qgis::listToSet( idx.nearestNeighbor( sourceGeom, mKNeighbors, mMaxDistance ) );
153 destinationGeom = idx.geometry(
id ).
centroid();
160 attrs << destinationAttributeCache.value(
id ) << dist;
173 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );