29using namespace Qt::StringLiterals;
36 SetMarkerRotationVisitor(
const QString &rotationField )
37 : mRotationField( rotationField )
40 bool visit(
const QgsStyleEntityVisitorInterface::StyleLeaf &entity )
override
42 if (
const QgsStyleSymbolEntity *symbolEntity =
dynamic_cast<const QgsStyleSymbolEntity *
>( entity.
entity ) )
44 if ( QgsMarkerSymbol *marker =
dynamic_cast<QgsMarkerSymbol *
>( symbolEntity->symbol() ) )
53 QString mRotationField;
59 SetMarkerRotationPostProcessor( std::unique_ptr<QgsFeatureRenderer> renderer,
const QString &rotationField )
60 : mRenderer( std::move( renderer ) )
61 , mRotationField( rotationField )
64 void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &, QgsProcessingFeedback * )
override
66 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
68 SetMarkerRotationVisitor visitor( mRotationField );
69 mRenderer->accept( &visitor );
70 vl->setRenderer( mRenderer.release() );
76 std::unique_ptr<QgsFeatureRenderer> mRenderer;
77 QString mRotationField;
80QString QgsAngleToNearestAlgorithm::name()
const
82 return u
"angletonearest"_s;
85QString QgsAngleToNearestAlgorithm::displayName()
const
87 return QObject::tr(
"Align points to features" );
90QStringList QgsAngleToNearestAlgorithm::tags()
const
92 return QObject::tr(
"align,marker,stroke,fill,orient,points,lines,angles,rotation,rotate" ).split(
',' );
95QString QgsAngleToNearestAlgorithm::group()
const
97 return QObject::tr(
"Cartography" );
100QString QgsAngleToNearestAlgorithm::groupId()
const
102 return u
"cartography"_s;
105QgsAngleToNearestAlgorithm::~QgsAngleToNearestAlgorithm() =
default;
107void QgsAngleToNearestAlgorithm::initAlgorithm(
const QVariantMap &configuration )
109 mIsInPlace = configuration.value( u
"IN_PLACE"_s ).toBool();
114 addParameter(
new QgsProcessingParameterDistance( u
"MAX_DISTANCE"_s, QObject::tr(
"Maximum distance to consider" ), QVariant(), u
"INPUT"_s,
true, 0 ) );
133QString QgsAngleToNearestAlgorithm::shortHelpString()
const
135 return QObject::tr(
"This algorithm calculates the rotation required to align point features with their nearest "
136 "feature from another reference layer. A new field is added to the output layer which is filled with the angle "
137 "(in degrees, clockwise) to the nearest reference feature.\n\n"
138 "Optionally, the output layer's symbology can be set to automatically use the calculated rotation "
139 "field to rotate marker symbols.\n\n"
140 "If desired, a maximum distance to use when aligning points can be set, to avoid aligning isolated points "
141 "to distant features." );
144QString QgsAngleToNearestAlgorithm::shortDescription()
const
146 return QObject::tr(
"Rotates point features to align them to nearby features." );
149QgsAngleToNearestAlgorithm *QgsAngleToNearestAlgorithm::createInstance()
const
151 return new QgsAngleToNearestAlgorithm();
154bool QgsAngleToNearestAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
156 if (
const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
167 if (
QgsVectorLayer *sourceLayer = parameterAsVectorLayer( parameters, u
"INPUT"_s, context ) )
169 mSourceRenderer.reset( sourceLayer->renderer()->clone() );
178 const double maxDistance = parameters.value( u
"MAX_DISTANCE"_s ).isValid() ? parameterAsDouble( parameters, u
"MAX_DISTANCE"_s, context ) : std::numeric_limits<double>::quiet_NaN();
179 std::unique_ptr<QgsProcessingFeatureSource> input( parameterAsSource( parameters, u
"INPUT"_s, context ) );
183 std::unique_ptr<QgsProcessingFeatureSource> referenceSource( parameterAsSource( parameters, u
"REFERENCE_LAYER"_s, context ) );
184 if ( !referenceSource )
187 const QString fieldName = parameterAsString( parameters, u
"FIELD_NAME"_s, context );
199 throw QgsProcessingException( QObject::tr(
"A field with the same name (%1) already exists" ).arg( fieldName ) );
201 outFields.
append(
QgsField( fieldName, QMetaType::Type::Double ) );
205 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, outFields, input->wkbType(), input->sourceCrs() ) );
206 if ( parameters.value( u
"OUTPUT"_s ).isValid() && !sink )
211 double step = referenceSource->featureCount() > 0 ? 50.0 / referenceSource->featureCount() : 1;
225 step = input->featureCount() > 0 ? 50.0 / input->featureCount() : 1;
240 if ( !f.hasGeometry() )
243 attributes.append( QVariant() );
245 attributes[fieldIndex] = QVariant();
246 f.setAttributes( attributes );
252 const QList<QgsFeatureId> nearest = index.nearestNeighbor( f.geometry(), 1, std::isnan( maxDistance ) ? 0 : maxDistance );
253 if ( nearest.empty() )
255 feedback->
pushInfo( QObject::tr(
"No matching features found within search distance" ) );
257 attributes.append( QVariant() );
259 attributes[fieldIndex] = QVariant();
260 f.setAttributes( attributes );
266 if ( nearest.count() > 1 )
268 feedback->
pushInfo( QObject::tr(
"Multiple matching features found at same distance from search feature, found %n feature(s)",
nullptr, nearest.count() ) );
275 attributes.append( line->startPoint().azimuth( line->endPoint() ) );
277 attributes[fieldIndex] = line->startPoint().azimuth( line->endPoint() );
282 attributes.append( QVariant() );
284 attributes[fieldIndex] = QVariant();
286 f.setAttributes( attributes );
296 const bool applySymbology = parameterAsBool( parameters, u
"APPLY_SYMBOLOGY"_s, context );
297 if ( applySymbology )
303 QVariantMap inPlaceParams = parameters;
305 if (
QgsVectorLayer *sourceLayer = parameterAsVectorLayer( inPlaceParams, u
"INPUT"_s, context ) )
307 std::unique_ptr<QgsFeatureRenderer> sourceRenderer( sourceLayer->renderer()->clone() );
308 SetMarkerRotationPostProcessor processor( std::move( sourceRenderer ), fieldName );
309 processor.postProcessLayer( sourceLayer, context, feedback );
319 outputs.insert( u
"OUTPUT"_s, dest );
@ VectorPoint
Vector point layers.
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ SupportsInPlaceEdits
Algorithm supports in-place editing.
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 & setNoAttributes()
Set that no attributes will be fetched.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsGeometry shortestLine(const QgsGeometry &other) const
Returns the shortest line joining this geometry to another geometry.
Line string geometry type, with support for z-dimension and m-values.
Base class for all map layer types.
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
void setPostProcessor(QgsProcessingLayerPostProcessorInterface *processor)
Sets the layer post-processor.
Contains information about the context in which a processing algorithm is executed.
QgsProcessingContext::LayerDetails & layerToLoadOnCompletionDetails(const QString &layer)
Returns a reference to the details for a given layer which is loaded on completion of the algorithm o...
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
bool willLoadLayerOnCompletion(const QString &layer) const
Returns true if the given layer (by ID or datasource) will be loaded into the current project upon co...
Custom exception class for processing related exceptions.
Encapsulates settings relating to a feature source input to a processing algorithm.
QgsProperty source
Source definition.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
An interface for layer post-processing handlers for execution following a processing algorithm operat...
virtual void postProcessLayer(QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback)=0
Post-processes the specified layer, following successful execution of a processing algorithm.
A boolean parameter for processing algorithms.
A double numeric parameter for distance values.
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.
A string parameter for processing algorithms.
static QgsProperty fromField(const QString &fieldName, bool isActive=true)
Returns a new FieldBasedProperty created from the specified field name.
A spatial index for QgsFeature objects.
@ FlagStoreFeatureGeometries
Indicates that the spatial index should also store feature geometries. This requires more memory,...
An interface for classes which can visit style entity (e.g.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
Represents a vector layer which manages a vector based dataset.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
const QgsStyleEntityInterface * entity
Reference to style entity being visited.