23 QString QgsJoinWithLinesAlgorithm::name()
const 25 return QStringLiteral(
"hublines" );
28 QString QgsJoinWithLinesAlgorithm::displayName()
const 30 return QObject::tr(
"Join by lines (hub lines)" );
33 QStringList QgsJoinWithLinesAlgorithm::tags()
const 35 return QObject::tr(
"join,connect,lines,points,hub,spoke" ).split(
',' );
38 QString QgsJoinWithLinesAlgorithm::group()
const 40 return QObject::tr(
"Vector analysis" );
43 QString QgsJoinWithLinesAlgorithm::groupId()
const 45 return QStringLiteral(
"vectoranalysis" );
48 void QgsJoinWithLinesAlgorithm::initAlgorithm(
const QVariantMap & )
51 QObject::tr(
"Hub layer" ) ) );
53 QObject::tr(
"Hub ID field" ), QVariant(), QStringLiteral(
"HUBS" ) ) );
56 QObject::tr(
"Hub layer fields to copy (leave empty to copy all fields)" ),
61 QObject::tr(
"Spoke layer" ) ) );
63 QObject::tr(
"Spoke ID field" ), QVariant(), QStringLiteral(
"SPOKES" ) ) );
66 QObject::tr(
"Spoke layer fields to copy (leave empty to copy all fields)" ),
73 QString QgsJoinWithLinesAlgorithm::shortHelpString()
const 75 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" 76 "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" 77 "If input layers are not point layers, a point on the surface of the geometries will be taken as the connecting location." );
80 QgsJoinWithLinesAlgorithm *QgsJoinWithLinesAlgorithm::createInstance()
const 82 return new QgsJoinWithLinesAlgorithm();
87 if ( parameters.value( QStringLiteral(
"SPOKES" ) ) == parameters.value( QStringLiteral(
"HUBS" ) ) )
90 std::unique_ptr< QgsProcessingFeatureSource > hubSource( parameterAsSource( parameters, QStringLiteral(
"HUBS" ), context ) );
94 std::unique_ptr< QgsProcessingFeatureSource > spokeSource( parameterAsSource( parameters, QStringLiteral(
"SPOKES" ), context ) );
95 if ( !hubSource || !spokeSource )
98 QString fieldHubName = parameterAsString( parameters, QStringLiteral(
"HUB_FIELD" ), context );
99 int fieldHubIndex = hubSource->fields().lookupField( fieldHubName );
100 const QStringList hubFieldsToCopy = parameterAsFields( parameters, QStringLiteral(
"HUB_FIELDS" ), context );
102 QString fieldSpokeName = parameterAsString( parameters, QStringLiteral(
"SPOKE_FIELD" ), context );
103 int fieldSpokeIndex = spokeSource->fields().lookupField( fieldSpokeName );
104 const QStringList spokeFieldsToCopy = parameterAsFields( parameters, QStringLiteral(
"SPOKE_FIELDS" ), context );
106 if ( fieldHubIndex < 0 || fieldSpokeIndex < 0 )
111 if ( hubFieldsToCopy.empty() )
113 hubOutFields = hubSource->fields();
114 hubFieldIndices.reserve( hubOutFields.
count() );
115 for (
int i = 0; i < hubOutFields.
count(); ++i )
117 hubFieldIndices << i;
122 hubFieldIndices.reserve( hubOutFields.
count() );
123 for (
const QString &field : hubFieldsToCopy )
125 int index = hubSource->fields().
lookupField( field );
128 hubFieldIndices << index;
129 hubOutFields.
append( hubSource->fields().at( index ) );
135 hubFields2Fetch << fieldHubIndex;
139 if ( spokeFieldsToCopy.empty() )
141 spokeOutFields = spokeSource->fields();
142 for (
int i = 0; i < spokeOutFields.
count(); ++i )
144 spokeFieldIndices << i;
149 for (
const QString &field : spokeFieldsToCopy )
151 int index = spokeSource->fields().
lookupField( field );
154 spokeFieldIndices << index;
155 spokeOutFields.
append( spokeSource->fields().at( index ) );
161 spokeFields2Fetch << fieldSpokeIndex;
181 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
190 p = *static_cast< const QgsPoint *>( feature.geometry().constGet() );
192 p = *
static_cast< const QgsPoint *
>( feature.geometry().pointOnSurface().constGet() );
193 if ( hasZ && !p.
is3D() )
201 double step = hubSource->featureCount() > 0 ? 100.0 / hubSource->featureCount() : 1;
217 QgsPoint hubPoint = getPointFromFeature( hubFeature );
221 for (
int j = 0; j < hubFeature.
attributes().count(); ++j )
223 if ( !hubFieldIndices.contains( j ) )
225 hubAttributes << hubFeature.
attribute( j );
234 while ( spokeFeatures.
nextFeature( spokeFeature ) )
243 QgsPoint spokePoint = getPointFromFeature( spokeFeature );
251 for (
int j = 0; j < spokeFeature.
attributes().count(); ++j )
253 if ( !spokeFieldIndices.contains( j ) )
255 spokeAttributes << spokeFeature.
attribute( j );
258 outAttributes.append( spokeAttributes );
266 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
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.
bool isCanceled() const
Tells whether the operation has been canceled already.
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.
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 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.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
A feature sink output for processing algorithms.
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.
bool isMeasure() const
Returns true if the geometry contains m values.
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB)
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
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.
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) ...
int count() const
Returns number of items.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
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.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
An input feature source (such as vector layers) parameter for processing algorithms.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Line string geometry type, with support for z-dimension and m-values.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
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.
QList< int > QgsAttributeList
bool nextFeature(QgsFeature &f)
Contains information about the context in which a processing algorithm is executed.