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 for (
int i = 0; i < hubOutFields.
count(); ++i )
116 hubFieldIndices << i;
121 for (
const QString &field : hubFieldsToCopy )
123 int index = hubSource->fields().
lookupField( field );
126 hubFieldIndices << index;
127 hubOutFields.
append( hubSource->fields().at( index ) );
133 hubFields2Fetch << fieldHubIndex;
137 if ( spokeFieldsToCopy.empty() )
139 spokeOutFields = spokeSource->fields();
140 for (
int i = 0; i < spokeOutFields.
count(); ++i )
142 spokeFieldIndices << i;
147 for (
const QString &field : spokeFieldsToCopy )
149 int index = spokeSource->fields().
lookupField( field );
152 spokeFieldIndices << index;
153 spokeOutFields.
append( spokeSource->fields().at( index ) );
159 spokeFields2Fetch << fieldSpokeIndex;
179 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
180 outType, hubSource->sourceCrs() ) );
188 p = *static_cast< const QgsPoint *>( feature.geometry().constGet() );
190 p = *
static_cast< const QgsPoint *
>( feature.geometry().pointOnSurface().constGet() );
191 if ( hasZ && !p.
is3D() )
199 double step = hubSource->featureCount() > 0 ? 100.0 / hubSource->featureCount() : 1;
215 QgsPoint hubPoint = getPointFromFeature( hubFeature );
219 for (
int j = 0; j < hubFeature.
attributes().count(); ++j )
221 if ( !hubFieldIndices.contains( j ) )
223 hubAttributes << hubFeature.
attribute( j );
232 while ( spokeFeatures.
nextFeature( spokeFeature ) )
241 QgsPoint spokePoint = getPointFromFeature( spokeFeature );
249 for (
int j = 0; j < spokeFeature.
attributes().count(); ++j )
251 if ( !spokeFieldIndices.contains( j ) )
253 spokeAttributes << spokeFeature.
attribute( j );
256 outAttributes.append( spokeAttributes );
264 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
bool isMeasure() const
Returns true if the geometry contains m values.
int lookupField(const QString &fieldName) const
Look up field's index from the field name.
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.
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...
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.
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.
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.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
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.
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.
QList< int > QgsAttributeList
bool nextFeature(QgsFeature &f)
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.