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 ) );
91 std::unique_ptr< QgsProcessingFeatureSource > spokeSource( parameterAsSource( parameters, QStringLiteral(
"SPOKES" ), context ) );
92 if ( !hubSource || !spokeSource )
95 QString fieldHubName = parameterAsString( parameters, QStringLiteral(
"HUB_FIELD" ), context );
96 int fieldHubIndex = hubSource->fields().lookupField( fieldHubName );
97 const QStringList hubFieldsToCopy = parameterAsFields( parameters, QStringLiteral(
"HUB_FIELDS" ), context );
99 QString fieldSpokeName = parameterAsString( parameters, QStringLiteral(
"SPOKE_FIELD" ), context );
100 int fieldSpokeIndex = spokeSource->fields().lookupField( fieldSpokeName );
101 const QStringList spokeFieldsToCopy = parameterAsFields( parameters, QStringLiteral(
"SPOKE_FIELDS" ), context );
103 if ( fieldHubIndex < 0 || fieldSpokeIndex < 0 )
108 if ( hubFieldsToCopy.empty() )
110 hubOutFields = hubSource->fields();
111 for (
int i = 0; i < hubOutFields.
count(); ++i )
113 hubFieldIndices << i;
118 for (
const QString &field : hubFieldsToCopy )
120 int index = hubSource->fields().
lookupField( field );
123 hubFieldIndices << index;
124 hubOutFields.
append( hubSource->fields().at( index ) );
130 hubFields2Fetch << fieldHubIndex;
134 if ( spokeFieldsToCopy.empty() )
136 spokeOutFields = spokeSource->fields();
137 for (
int i = 0; i < spokeOutFields.
count(); ++i )
139 spokeFieldIndices << i;
144 for (
const QString &field : spokeFieldsToCopy )
146 int index = spokeSource->fields().
lookupField( field );
149 spokeFieldIndices << index;
150 spokeOutFields.
append( spokeSource->fields().at( index ) );
156 spokeFields2Fetch << fieldSpokeIndex;
176 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
177 outType, hubSource->sourceCrs() ) );
185 p = *static_cast< const QgsPoint *>( feature.geometry().constGet() );
187 p = *
static_cast< const QgsPoint *
>( feature.geometry().pointOnSurface().constGet() );
188 if ( hasZ && !p.
is3D() )
196 double step = hubSource->featureCount() > 0 ? 100.0 / hubSource->featureCount() : 1;
212 QgsPoint hubPoint = getPointFromFeature( hubFeature );
216 for (
int j = 0; j < hubFeature.
attributes().count(); ++j )
218 if ( !hubFieldIndices.contains( j ) )
220 hubAttributes << hubFeature.
attribute( j );
225 spokeRequest.
setFilterExpression( QgsExpression::createFieldEqualityExpression( fieldSpokeName, hubFeature.
attribute( fieldHubIndex ) ) );
229 while ( spokeFeatures.
nextFeature( spokeFeature ) )
238 QgsPoint spokePoint = getPointFromFeature( spokeFeature );
246 for (
int j = 0; j < spokeFeature.
attributes().count(); ++j )
248 if ( !spokeFieldIndices.contains( j ) )
250 spokeAttributes << spokeFeature.
attribute( j );
253 outAttributes.append( spokeAttributes );
261 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
Return 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) ...
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.