27using namespace Qt::StringLiterals;
31QString QgsShortestLineAlgorithm::name()
const
33 return u
"shortestline"_s;
36QString QgsShortestLineAlgorithm::displayName()
const
38 return QObject::tr(
"Shortest line between features" );
41QStringList QgsShortestLineAlgorithm::tags()
const
43 return QObject::tr(
"distance,shortest,minimum,nearest,closest,proximity" ).split(
',' );
46QString QgsShortestLineAlgorithm::group()
const
48 return QObject::tr(
"Vector analysis" );
51QString QgsShortestLineAlgorithm::groupId()
const
53 return u
"vectoranalysis"_s;
56QString QgsShortestLineAlgorithm::shortDescription()
const
58 return QObject::tr(
"Calculates the shortest lines between features in source and destination layers." );
61QString QgsShortestLineAlgorithm::shortHelpString()
const
63 return QObject::tr(
"This algorithm creates a line layer as the "
64 "shortest line between the source and the destination layer. "
65 "By default only the first nearest feature of the "
66 "destination layer is taken into account. "
67 "The n-nearest neighboring features number can be specified.\n\n"
68 "If a maximum distance is specified, then only "
69 "features which are closer than this distance will "
70 "be considered.\n\nThe output features will contain all the "
71 "source layer attributes, all the attributes from the n-nearest "
72 "feature and the additional field of the distance.\n\n"
73 "This algorithm uses purely Cartesian calculations for distance, "
74 "and does not consider geodetic or ellipsoid properties when "
75 "determining feature proximity. The measurement and output coordinate "
76 "system is based on the coordinate system of the source layer."
80QgsShortestLineAlgorithm *QgsShortestLineAlgorithm::createInstance()
const
82 return new QgsShortestLineAlgorithm();
85void QgsShortestLineAlgorithm::initAlgorithm(
const QVariantMap & )
89 addParameter(
new QgsProcessingParameterEnum( u
"METHOD"_s, QObject::tr(
"Method" ), QStringList() <<
"Distance to Nearest Point on feature" <<
"Distance to Feature Centroid",
false, 0 ) );
97 mSource.reset( parameterAsSource( parameters, u
"SOURCE"_s, context ) );
101 mDestination.reset( parameterAsSource( parameters, u
"DESTINATION"_s, context ) );
105 mMethod = parameterAsInt( parameters, u
"METHOD"_s, context );
107 mKNeighbors = parameterAsInt( parameters, u
"NEIGHBORS"_s, context );
109 mMaxDistance = parameterAsDouble( parameters, u
"DISTANCE"_s, context );
116 if ( mKNeighbors > mDestination->featureCount() )
117 mKNeighbors = mDestination->featureCount();
122 newFields.
append(
QgsField( u
"distance"_s, QMetaType::Type::Double ) );
126 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields,
Qgis::WkbType::MultiLineString, mSource->sourceCrs() ) );
131 QHash<QgsFeatureId, QgsAttributes> destinationAttributeCache;
132 double step = mDestination->featureCount() > 0 ? 50.0 / mDestination->featureCount() : 1;
136 if ( feedback-> isCanceled() )
141 destinationAttributeCache.insert( f.
id(), f.
attributes() );
145 step = mSource->featureCount() > 0 ? 50.0 / mSource->featureCount() : 1;
152 while ( sourceIterator.
nextFeature( sourceFeature ) )
158 QgsFeatureIds nearestIds = qgis::listToSet( idx.nearestNeighbor( sourceGeom, mKNeighbors, mMaxDistance ) );
165 destinationGeom = idx.geometry(
id ).
centroid();
176 throw QgsProcessingException( QObject::tr(
"An error occurred while calculating shortest line length" ) );
181 attrs << destinationAttributeCache.value(
id ) << dist;
196 outputs.insert( u
"OUTPUT"_s, dest );
@ VectorAnyGeometry
Any vector layer with geometry.
@ VectorLine
Vector line layers.
@ MultiLineString
MultiLineString.
Custom exception class for Coordinate Reference System related exceptions.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
double measureLength(const QgsGeometry &geometry) const
Measures the length of a geometry.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
A geometry is the spatial representation of a feature.
QgsGeometry centroid() const
Returns the center of mass of a geometry.
QgsGeometry shortestLine(const QgsGeometry &other) const
Returns the shortest line joining this geometry to another geometry.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A double numeric parameter for distance values.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A numeric parameter for processing algorithms.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
A spatial index for QgsFeature objects.
@ FlagStoreFeatureGeometries
Indicates that the spatial index should also store feature geometries. This requires more memory,...
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features