26using namespace Qt::StringLiterals;
30QString QgsJoinWithLinesAlgorithm::name()
const
35QString QgsJoinWithLinesAlgorithm::displayName()
const
37 return QObject::tr(
"Join by lines (hub lines)" );
40QStringList QgsJoinWithLinesAlgorithm::tags()
const
42 return QObject::tr(
"join,connect,lines,points,hub,spoke,geodesic,great,circle" ).split(
',' );
45QString QgsJoinWithLinesAlgorithm::group()
const
47 return QObject::tr(
"Vector analysis" );
50QString QgsJoinWithLinesAlgorithm::groupId()
const
52 return u
"vectoranalysis"_s;
55void QgsJoinWithLinesAlgorithm::initAlgorithm(
const QVariantMap & )
73 auto distanceParam = std::make_unique<QgsProcessingParameterDistance>( u
"GEODESIC_DISTANCE"_s, QObject::tr(
"Distance between vertices (geodesic lines only)" ), 1000 );
76 distanceParam->setIsDynamic(
true );
78 distanceParam->setDynamicLayerParameterName( u
"HUBS"_s );
79 addParameter( distanceParam.release() );
81 auto breakParam = std::make_unique<QgsProcessingParameterBoolean>( u
"ANTIMERIDIAN_SPLIT"_s, QObject::tr(
"Split lines at antimeridian (±180 degrees longitude)" ),
false );
83 addParameter( breakParam.release() );
88QString QgsJoinWithLinesAlgorithm::shortHelpString()
const
91 "This algorithm creates hub and spoke diagrams by connecting lines from points on the Spoke layer to matching points in the Hub layer.\n\n"
92 "Determination of which hub goes with each point is based on a match between the Hub ID field on the hub points and the Spoke ID field on the spoke points.\n\n"
93 "If input layers are not point layers, a point on the surface of the geometries will be taken as the connecting location.\n\n"
94 "Optionally, geodesic lines can be created, which represent the shortest path on the surface of an ellipsoid. When "
95 "geodesic mode is used, it is possible to split the created lines at the antimeridian (±180 degrees longitude), which can improve "
96 "rendering of the lines. Additionally, the distance between vertices can be specified. A smaller distance results in a denser, more "
101QString QgsJoinWithLinesAlgorithm::shortDescription()
const
103 return QObject::tr(
"Creates lines joining two point layers, based on a common attribute value." );
111QgsJoinWithLinesAlgorithm *QgsJoinWithLinesAlgorithm::createInstance()
const
113 return new QgsJoinWithLinesAlgorithm();
118 if ( parameters.value( u
"SPOKES"_s ) == parameters.value( u
"HUBS"_s ) )
121 std::unique_ptr<QgsProcessingFeatureSource> hubSource( parameterAsSource( parameters, u
"HUBS"_s, context ) );
125 std::unique_ptr<QgsProcessingFeatureSource> spokeSource( parameterAsSource( parameters, u
"SPOKES"_s, context ) );
129 const QString fieldHubName = parameterAsString( parameters, u
"HUB_FIELD"_s, context );
130 const int fieldHubIndex = hubSource->fields().lookupField( fieldHubName );
131 const QStringList hubFieldsToCopy = parameterAsStrings( parameters, u
"HUB_FIELDS"_s, context );
133 const QString fieldSpokeName = parameterAsString( parameters, u
"SPOKE_FIELD"_s, context );
134 const int fieldSpokeIndex = spokeSource->fields().lookupField( fieldSpokeName );
135 const QStringList spokeFieldsToCopy = parameterAsStrings( parameters, u
"SPOKE_FIELDS"_s, context );
137 if ( fieldHubIndex < 0 || fieldSpokeIndex < 0 )
140 const bool geodesic = parameterAsBoolean( parameters, u
"GEODESIC"_s, context );
141 const double geodesicDistance = parameterAsDouble( parameters, u
"GEODESIC_DISTANCE"_s, context ) * 1000;
143 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, hubSource.get() );
145 if ( dynamicGeodesicDistance )
147 geodesicDistanceProperty = parameters.
value( u
"GEODESIC_DISTANCE"_s ).value<
QgsProperty>();
150 const bool splitAntimeridian = parameterAsBoolean( parameters, u
"ANTIMERIDIAN_SPLIT"_s, context );
157 if ( hubFieldsToCopy.empty() )
159 hubOutFields = hubSource->fields();
160 hubFieldIndices.reserve( hubOutFields.
count() );
161 for (
int i = 0; i < hubOutFields.
count(); ++i )
163 hubFieldIndices << i;
168 hubFieldIndices.reserve( hubOutFields.
count() );
169 for (
const QString &field : hubFieldsToCopy )
171 const int index = hubSource->fields().lookupField( field );
174 hubFieldIndices << index;
175 hubOutFields.
append( hubSource->fields().at( index ) );
181 hubFields2Fetch << fieldHubIndex;
185 if ( spokeFieldsToCopy.empty() )
187 spokeOutFields = spokeSource->fields();
188 spokeFieldIndices.reserve( spokeOutFields.
count() );
189 for (
int i = 0; i < spokeOutFields.
count(); ++i )
191 spokeFieldIndices << i;
196 for (
const QString &field : spokeFieldsToCopy )
198 const int index = spokeSource->fields().lookupField( field );
201 spokeFieldIndices << index;
202 spokeOutFields.
append( spokeSource->fields().at( index ) );
208 spokeFields2Fetch << fieldSpokeIndex;
228 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields, outType, hubSource->sourceCrs(),
QgsFeatureSink::RegeneratePrimaryKey ) );
235 p = *
static_cast<const QgsPoint *
>( feature.geometry().constGet() );
237 p = *
static_cast<const QgsPoint *
>( feature.geometry().pointOnSurface().constGet() );
238 if ( hasZ && !p.
is3D() )
246 const double step = hubSource->featureCount() > 0 ? 100.0 / hubSource->featureCount() : 1;
262 const QgsPoint hubPoint = getPointFromFeature( hubFeature );
267 for (
int j = 0; j < attributeCount; ++j )
269 if ( !hubFieldIndices.contains( j ) )
271 hubAttributes << hubFeature.
attribute( j );
280 while ( spokeFeatures.
nextFeature( spokeFeature ) )
289 const QgsPoint spokePoint = getPointFromFeature( spokeFeature );
294 if ( splitAntimeridian )
299 double distance = geodesicDistance;
300 if ( dynamicGeodesicDistance )
303 distance = geodesicDistanceProperty.
valueAsDouble( expressionContext, distance );
306 auto ml = std::make_unique<QgsMultiLineString>();
307 auto l = std::make_unique<QgsLineString>( QVector<QgsPoint>() << hubPoint );
309 QVector<QgsPointXY> points1 = points.at( 0 );
311 if ( points.count() == 1 )
315 l->append( &geodesicPoints );
316 if ( points.count() == 1 )
317 l->addVertex( spokePoint );
319 ml->addGeometry( l.release() );
320 if ( points.count() > 1 )
322 QVector<QgsPointXY> points2 = points.at( 1 );
324 l = std::make_unique<QgsLineString>( points2 );
326 l->addZValue( std::numeric_limits<double>::quiet_NaN() );
328 l->addMValue( std::numeric_limits<double>::quiet_NaN() );
330 l->addVertex( spokePoint );
331 ml->addGeometry( l.release() );
342 for (
int j = 0; j < attributeCount; ++j )
344 if ( !spokeFieldIndices.contains( j ) )
346 spokeAttributes << spokeFeature.
attribute( j );
349 outAttributes.append( spokeAttributes );
359 outputs.insert( u
"OUTPUT"_s, dest );
@ VectorLine
Vector line layers.
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
WkbType
The WKB type describes the number of dimensions a geometry has.
@ MultiLineString
MultiLineString.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
bool isMeasure() const
Returns true if the geometry contains m values.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
QVector< QVector< QgsPointXY > > geodesicLine(const QgsPointXY &p1, const QgsPointXY &p2, double interval, bool breakLine=false) const
Calculates the geodesic line between p1 and p2, which represents the shortest path on the ellipsoid b...
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
QgsGeometry splitGeometryAtAntimeridian(const QgsGeometry &geometry) const
Splits a (Multi)LineString geometry at the antimeridian (longitude +/- 180 degrees).
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value, QMetaType::Type fieldType=QMetaType::Type::UnknownType)
Create an expression allowing to evaluate if a field is equal to a value.
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).
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
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.
int attributeCount() const
Returns the number of attributes attached to the feature.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
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.
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.
Line string geometry type, with support for z-dimension and m-values.
Point geometry type, with support for z-dimension and m-values.
bool addMValue(double mValue=0) override
Adds a measure to the geometry, initialized to a preset value.
bool addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
QString ellipsoid() const
Returns the ellipsoid to use for distance and area calculations.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A boolean parameter for processing algorithms.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
static bool isDynamic(const QVariantMap ¶meters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
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).
Definition for a property.
@ DoublePositive
Positive double value (including 0).
A store for object properties.
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
QList< int > QgsAttributeList