24 QString QgsJoinWithLinesAlgorithm::name()
const 26 return QStringLiteral(
"hublines" );
29 QString QgsJoinWithLinesAlgorithm::displayName()
const 31 return QObject::tr(
"Join by lines (hub lines)" );
34 QStringList QgsJoinWithLinesAlgorithm::tags()
const 36 return QObject::tr(
"join,connect,lines,points,hub,spoke,geodesic,great,circle" ).split(
',' );
39 QString QgsJoinWithLinesAlgorithm::group()
const 41 return QObject::tr(
"Vector analysis" );
44 QString QgsJoinWithLinesAlgorithm::groupId()
const 46 return QStringLiteral(
"vectoranalysis" );
49 void QgsJoinWithLinesAlgorithm::initAlgorithm(
const QVariantMap & )
52 QObject::tr(
"Hub layer" ) ) );
54 QObject::tr(
"Hub ID field" ), QVariant(), QStringLiteral(
"HUBS" ) ) );
57 QObject::tr(
"Hub layer fields to copy (leave empty to copy all fields)" ),
62 QObject::tr(
"Spoke layer" ) ) );
64 QObject::tr(
"Spoke ID field" ), QVariant(), QStringLiteral(
"SPOKES" ) ) );
67 QObject::tr(
"Spoke layer fields to copy (leave empty to copy all fields)" ),
73 auto distanceParam = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral(
"GEODESIC_DISTANCE" ), QObject::tr(
"Distance between vertices (geodesic lines only)" ), 1000 );
76 distanceParam->setIsDynamic(
true );
78 distanceParam->setDynamicLayerParameterName( QStringLiteral(
"HUBS" ) );
79 addParameter( distanceParam.release() );
81 auto breakParam = qgis::make_unique< QgsProcessingParameterBoolean >( QStringLiteral(
"ANTIMERIDIAN_SPLIT" ), QObject::tr(
"Split lines at antimeridian (±180 degrees longitude)" ), false );
83 addParameter( breakParam.release() );
88 QString QgsJoinWithLinesAlgorithm::shortHelpString()
const 90 return QObject::tr(
"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" 91 "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" 92 "If input layers are not point layers, a point on the surface of the geometries will be taken as the connecting location.\n\n" 93 "Optionally, geodesic lines can be created, which represent the shortest path on the surface of an ellipsoid. When " 94 "geodesic mode is used, it is possible to split the created lines at the antimeridian (±180 degrees longitude), which can improve " 95 "rendering of the lines. Additionally, the distance between vertices can be specified. A smaller distance results in a denser, more " 99 QString QgsJoinWithLinesAlgorithm::shortDescription()
const 101 return QObject::tr(
"Creates lines joining two point layers, based on a common attribute value." );
104 QgsJoinWithLinesAlgorithm *QgsJoinWithLinesAlgorithm::createInstance()
const 106 return new QgsJoinWithLinesAlgorithm();
111 if ( parameters.value( QStringLiteral(
"SPOKES" ) ) == parameters.value( QStringLiteral(
"HUBS" ) ) )
114 std::unique_ptr< QgsProcessingFeatureSource > hubSource( parameterAsSource( parameters, QStringLiteral(
"HUBS" ), context ) );
118 std::unique_ptr< QgsProcessingFeatureSource > spokeSource( parameterAsSource( parameters, QStringLiteral(
"SPOKES" ), context ) );
119 if ( !hubSource || !spokeSource )
122 QString fieldHubName = parameterAsString( parameters, QStringLiteral(
"HUB_FIELD" ), context );
123 int fieldHubIndex = hubSource->fields().lookupField( fieldHubName );
124 const QStringList hubFieldsToCopy = parameterAsFields( parameters, QStringLiteral(
"HUB_FIELDS" ), context );
126 QString fieldSpokeName = parameterAsString( parameters, QStringLiteral(
"SPOKE_FIELD" ), context );
127 int fieldSpokeIndex = spokeSource->fields().lookupField( fieldSpokeName );
128 const QStringList spokeFieldsToCopy = parameterAsFields( parameters, QStringLiteral(
"SPOKE_FIELDS" ), context );
130 if ( fieldHubIndex < 0 || fieldSpokeIndex < 0 )
133 const bool geodesic = parameterAsBoolean( parameters, QStringLiteral(
"GEODESIC" ), context );
134 const double geodesicDistance = parameterAsDouble( parameters, QStringLiteral(
"GEODESIC_DISTANCE" ), context ) * 1000;
136 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, hubSource.get() );
138 if ( dynamicGeodesicDistance )
140 geodesicDistanceProperty = parameters.
value( QStringLiteral(
"GEODESIC_DISTANCE" ) ).value<
QgsProperty >();
143 const bool splitAntimeridian = parameterAsBoolean( parameters, QStringLiteral(
"ANTIMERIDIAN_SPLIT" ), context );
150 if ( hubFieldsToCopy.empty() )
152 hubOutFields = hubSource->fields();
153 hubFieldIndices.reserve( hubOutFields.
count() );
154 for (
int i = 0; i < hubOutFields.
count(); ++i )
156 hubFieldIndices << i;
161 hubFieldIndices.reserve( hubOutFields.
count() );
162 for (
const QString &field : hubFieldsToCopy )
164 int index = hubSource->fields().
lookupField( field );
167 hubFieldIndices << index;
168 hubOutFields.
append( hubSource->fields().at( index ) );
174 hubFields2Fetch << fieldHubIndex;
178 if ( spokeFieldsToCopy.empty() )
180 spokeOutFields = spokeSource->fields();
181 spokeFieldIndices.reserve( spokeOutFields.
count() );
182 for (
int i = 0; i < spokeOutFields.
count(); ++i )
184 spokeFieldIndices << i;
189 for (
const QString &field : spokeFieldsToCopy )
191 int index = spokeSource->fields().
lookupField( field );
194 spokeFieldIndices << index;
195 spokeOutFields.
append( spokeSource->fields().at( index ) );
201 spokeFields2Fetch << fieldSpokeIndex;
221 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
230 p = *static_cast< const QgsPoint *>( feature.geometry().constGet() );
232 p = *
static_cast< const QgsPoint *
>( feature.geometry().pointOnSurface().constGet() );
233 if ( hasZ && !p.
is3D() )
241 double step = hubSource->featureCount() > 0 ? 100.0 / hubSource->featureCount() : 1;
257 QgsPoint hubPoint = getPointFromFeature( hubFeature );
261 for (
int j = 0; j < hubFeature.
attributes().count(); ++j )
263 if ( !hubFieldIndices.contains( j ) )
265 hubAttributes << hubFeature.
attribute( j );
274 while ( spokeFeatures.
nextFeature( spokeFeature ) )
283 QgsPoint spokePoint = getPointFromFeature( spokeFeature );
288 if ( splitAntimeridian )
293 double distance = geodesicDistance;
294 if ( dynamicGeodesicDistance )
297 distance = geodesicDistanceProperty.valueAsDouble( expressionContext, distance );
300 std::unique_ptr< QgsMultiLineString > ml = qgis::make_unique< QgsMultiLineString >();
301 std::unique_ptr< QgsLineString > l = qgis::make_unique< QgsLineString >( QVector< QgsPoint >() << hubPoint );
303 QVector< QgsPointXY > points1 = points.at( 0 );
305 if ( points.count() == 1 )
309 l->append( &geodesicPoints );
310 if ( points.count() == 1 )
311 l->addVertex( spokePoint );
313 ml->addGeometry( l.release() );
314 if ( points.count() > 1 )
316 QVector< QgsPointXY > points2 = points.at( 1 );
318 l = qgis::make_unique< QgsLineString >( points2 );
320 l->addZValue( std::numeric_limits<double>::quiet_NaN() );
322 l->addMValue( std::numeric_limits<double>::quiet_NaN() );
324 l->addVertex( spokePoint );
325 ml->addGeometry( l.release() );
335 for (
int j = 0; j < spokeFeature.
attributes().count(); ++j )
337 if ( !spokeFieldIndices.contains( j ) )
339 spokeAttributes << spokeFeature.
attribute( j );
342 outAttributes.append( spokeAttributes );
350 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
bool isMeasure() const
Returns true if the geometry contains m values.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QgsGeometry splitGeometryAtAntimeridian(const QgsGeometry &geometry) const
Splits a (Multi)LineString geometry at the antimeridian (longitude +/- 180 degrees).
A boolean parameter for processing algorithms.
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
Wrapper for iterator of features from vector data provider or vector layer.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Base class for providing feedback from a processing algorithm.
Parameter is an advanced parameter which should be hidden from users by default.
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
A vector layer or feature source field parameter for processing algorithms.
bool addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
A class to represent a 2D point.
void setProgress(double progress)
Sets the current progress for the feedback object.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
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...
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Positive double value (including 0)
bool hasGeometry() const
Returns true if the feature has an associated geometry.
int count() const
Returns number of items.
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
A feature sink output for processing algorithms.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
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...
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Type
The WKB type describes the number of dimensions a geometry has.
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false) ...
A store for object properties.
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value)
Create an expression allowing to evaluate if a field is equal to a value.
Point geometry type, with support for z-dimension and m-values.
Definition for a property.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
bool isCanceled() const
Tells whether the operation has been canceled already.
An input feature source (such as vector layers) parameter for processing algorithms.
Line string geometry type, with support for z-dimension and m-values.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool addMValue(double mValue=0) override
Adds a measure to the geometry, initialized to a preset value.
static bool hasM(Type type)
Tests whether a WKB type contains m values.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
QList< int > QgsAttributeList
bool nextFeature(QgsFeature &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).
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Contains information about the context in which a processing algorithm is executed.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
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...