24QString QgsTransectAlgorithm::name()
const
26 return QStringLiteral(
"transect" );
29QString QgsTransectAlgorithm::displayName()
const
31 return QObject::tr(
"Transect" );
34QStringList QgsTransectAlgorithm::tags()
const
36 return QObject::tr(
"transect,station,lines,extend," ).split(
',' );
39QString QgsTransectAlgorithm::group()
const
41 return QObject::tr(
"Vector geometry" );
44QString QgsTransectAlgorithm::groupId()
const
46 return QStringLiteral(
"vectorgeometry" );
49void QgsTransectAlgorithm::initAlgorithm(
const QVariantMap & )
52 std::unique_ptr<QgsProcessingParameterDistance> length = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral(
"LENGTH" ), QObject::tr(
"Length of the transect" ), 5.0, QStringLiteral(
"INPUT" ),
false, 0 );
53 length->setIsDynamic(
true );
55 length->setDynamicLayerParameterName( QStringLiteral(
"INPUT" ) );
56 addParameter( length.release() );
58 std::unique_ptr<QgsProcessingParameterNumber>
angle = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"ANGLE" ), QObject::tr(
"Angle in degrees from the original line at the vertices" ),
Qgis::ProcessingNumberParameterType::Double, 90.0,
false, 0, 360 );
59 angle->setIsDynamic(
true );
61 angle->setDynamicLayerParameterName( QStringLiteral(
"INPUT" ) );
62 addParameter(
angle.release() );
64 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"SIDE" ), QObject::tr(
"Side to create the transects" ), QStringList() << QObject::tr(
"Left" ) << QObject::tr(
"Right" ) << QObject::tr(
"Both" ),
false ) );
68QString QgsTransectAlgorithm::shortHelpString()
const
70 return QObject::tr(
"This algorithm creates transects on vertices for (multi)linestring.\n" ) + QObject::tr(
"A transect is a line oriented from an angle (by default perpendicular) to the input polylines (at vertices)." ) + QStringLiteral(
"\n\n" ) + QObject::tr(
"Field(s) from feature(s) are returned in the transect with these new fields:\n" ) + QObject::tr(
"- TR_FID: ID of the original feature\n" ) + QObject::tr(
"- TR_ID: ID of the transect. Each transect have an unique ID\n" ) + QObject::tr(
"- TR_SEGMENT: ID of the segment of the linestring\n" ) + QObject::tr(
"- TR_ANGLE: Angle in degrees from the original line at the vertex\n" ) + QObject::tr(
"- TR_LENGTH: Total length of the transect returned\n" ) + QObject::tr(
"- TR_ORIENT: Side of the transect (only on the left or right of the line, or both side)\n" );
78QgsTransectAlgorithm *QgsTransectAlgorithm::createInstance()
const
80 return new QgsTransectAlgorithm();
85 const Side orientation =
static_cast<QgsTransectAlgorithm::Side
>( parameterAsInt( parameters, QStringLiteral(
"SIDE" ), context ) );
86 const double angle = fabs( parameterAsDouble( parameters, QStringLiteral(
"ANGLE" ), context ) );
90 angleProperty = parameters.
value( QStringLiteral(
"ANGLE" ) ).value<
QgsProperty>();
92 double length = parameterAsDouble( parameters, QStringLiteral(
"LENGTH" ), context );
96 lengthProperty = parameters.
value( QStringLiteral(
"LENGTH" ) ).value<
QgsProperty>();
98 if ( orientation == QgsTransectAlgorithm::Both )
101 std::unique_ptr<QgsFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
109 fields.
append(
QgsField( QStringLiteral(
"TR_FID" ), QMetaType::Type::Int, QString(), 20 ) );
110 fields.
append(
QgsField( QStringLiteral(
"TR_ID" ), QMetaType::Type::Int, QString(), 20 ) );
111 fields.
append(
QgsField( QStringLiteral(
"TR_SEGMENT" ), QMetaType::Type::Int, QString(), 20 ) );
112 fields.
append(
QgsField( QStringLiteral(
"TR_ANGLE" ), QMetaType::Type::Double, QString(), 5, 2 ) );
113 fields.
append(
QgsField( QStringLiteral(
"TR_LENGTH" ), QMetaType::Type::Double, QString(), 20, 6 ) );
114 fields.
append(
QgsField( QStringLiteral(
"TR_ORIENT" ), QMetaType::Type::Int, QString(), 1 ) );
123 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields, outputWkb, source->sourceCrs(),
QgsFeatureSink::RegeneratePrimaryKey ) );
131 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 1;
149 if ( dynamicLength || dynamicAngle )
154 double evaluatedLength = length;
157 double evaluatedAngle =
angle;
170 const int i = vertexId.
vertex;
173 attrs << current << number << i + 1 << evaluatedAngle << ( ( orientation == QgsTransectAlgorithm::Both ) ? evaluatedLength * 2 : evaluatedLength ) << orientation;
175 const double angleAtVertex = line->
vertexAngle( vertexId );
176 outFeat.
setGeometry( calcTransect( *it, angleAtVertex, evaluatedLength, orientation, evaluatedAngle ) );
188 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
193QgsGeometry QgsTransectAlgorithm::calcTransect(
const QgsPoint &point,
const double angleAtVertex,
const double length,
const QgsTransectAlgorithm::Side orientation,
const double angle )
200 if ( ( orientation == QgsTransectAlgorithm::Right ) || ( orientation == QgsTransectAlgorithm::Both ) )
202 pLeft = point.
project( length, angle + 180.0 / M_PI * angleAtVertex );
203 if ( orientation != QgsTransectAlgorithm::Both )
207 if ( ( orientation == QgsTransectAlgorithm::Left ) || ( orientation == QgsTransectAlgorithm::Both ) )
209 pRight = point.
project( -length, angle + 180.0 / M_PI * angleAtVertex );
210 if ( orientation != QgsTransectAlgorithm::Both )
214 line.append( pLeft );
215 line.append( pRight );
@ VectorLine
Vector line layers.
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
WkbType
The WKB type describes the number of dimensions a geometry has.
@ Double
Double/float values.
The vertex_iterator class provides STL-style iterator for vertices.
QgsVertexId vertexId() const
Returns vertex ID of the current item.
vertex_iterator vertices_end() const
Returns STL-style iterator pointing to the imaginary vertex after the last vertex of the geometry.
vertex_iterator vertices_begin() const
Returns STL-style iterator pointing to the first vertex of the geometry.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
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.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
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.
int numGeometries() const
Returns the number of geometries within the collection.
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.
static QgsGeometry fromPolyline(const QgsPolyline &polyline)
Creates a new LineString geometry from a list of QgsPoint points.
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
Line string geometry type, with support for z-dimension and m-values.
double vertexAngle(QgsVertexId vertex) const override
Returns approximate angle at a vertex.
Multi line string geometry collection.
QgsLineString * lineStringN(int index)
Returns the line string with the specified index.
Point geometry type, with support for z-dimension and m-values.
QgsPoint project(double distance, double azimuth, double inclination=90.0) const
Returns a new point which corresponds to this point projected by a specified distance with specified ...
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Custom exception class for processing related exceptions.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
Base class for providing feedback from a processing algorithm.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
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...
Definition for a property.
@ Double
Double value (including negative values)
@ DoublePositive
Positive double value (including 0)
A store for object properties.
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...
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
static bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
QgsPointSequence QgsPolyline
Polyline as represented as a vector of points.
Utility class for identifying a unique vertex within a geometry.