25using namespace Qt::StringLiterals;
29QString QgsTransectAlgorithmBase::group()
const
31 return QObject::tr(
"Vector geometry" );
34QString QgsTransectAlgorithmBase::groupId()
const
36 return u
"vectorgeometry"_s;
39QStringList QgsTransectAlgorithmBase::tags()
const
41 return QObject::tr(
"transect,station,lines,extend" ).split(
',' );
44QString QgsTransectAlgorithmBase::shortDescription()
const
46 return QObject::tr(
"Creates transects for (multi)linestrings." );
54void QgsTransectAlgorithmBase::initAlgorithm(
const QVariantMap & )
60 auto length = std::make_unique<QgsProcessingParameterDistance>( u
"LENGTH"_s, QObject::tr(
"Length of the transect" ), 5.0, u
"INPUT"_s,
false, 0 );
61 length->setIsDynamic(
true );
63 length->setDynamicLayerParameterName( u
"INPUT"_s );
64 addParameter( length.release() );
68 angle->setIsDynamic(
true );
70 angle->setDynamicLayerParameterName( u
"INPUT"_s );
71 addParameter(
angle.release() );
74 new QgsProcessingParameterEnum( u
"SIDE"_s, QObject::tr(
"Side to create the transects" ), QStringList() << QObject::tr(
"Left" ) << QObject::tr(
"Right" ) << QObject::tr(
"Both" ),
false, 2 )
78 = std::make_unique<QgsProcessingParameterEnum>( u
"DIRECTION"_s, QObject::tr(
"Direction" ), QStringList() << QObject::tr(
"Right to Left" ) << QObject::tr(
"Left to Right" ),
false, 0,
true );
79 direction->setGuiDefaultValueOverride( 1 );
80 addParameter( direction.release() );
87 mOrientation =
static_cast<QgsTransectAlgorithmBase::Side
>( parameterAsInt( parameters, u
"SIDE"_s, context ) );
88 mAngle = fabs( parameterAsDouble( parameters, u
"ANGLE"_s, context ) );
91 mAngleProperty = parameters.value( u
"ANGLE"_s ).value<
QgsProperty>();
93 mLength = parameterAsDouble( parameters, u
"LENGTH"_s, context );
96 mLengthProperty = parameters.value( u
"LENGTH"_s ).value<
QgsProperty>();
98 if ( mOrientation == QgsTransectAlgorithmBase::Both )
101 mDirection =
static_cast<QgsTransectAlgorithmBase::Direction
>( parameterAsInt( parameters, u
"DIRECTION"_s, context ) );
104 if ( !prepareAlgorithmTransectParameters( parameters, context, feedback ) )
105 return QVariantMap();
107 std::unique_ptr<QgsFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
114 newFields.
append(
QgsField( u
"TR_FID"_s, QMetaType::Type::Int, QString(), 20 ) );
115 newFields.
append(
QgsField( u
"TR_ID"_s, QMetaType::Type::Int, QString(), 20 ) );
116 newFields.
append(
QgsField( u
"TR_SEGMENT"_s, QMetaType::Type::Int, QString(), 20 ) );
117 newFields.
append(
QgsField( u
"TR_ANGLE"_s, QMetaType::Type::Double, QString(), 5, 2 ) );
118 newFields.
append(
QgsField( u
"TR_LENGTH"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
119 newFields.
append(
QgsField( u
"TR_ORIENT"_s, QMetaType::Type::Int, QString(), 1 ) );
129 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields, outputWkb, source->sourceCrs(),
QgsFeatureSink::RegeneratePrimaryKey ) );
137 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 1;
154 if ( mDynamicLength || mDynamicAngle )
159 double evaluatedLength = mLength;
160 if ( mDynamicLength )
162 double evaluatedAngle = mAngle;
164 evaluatedAngle = mAngleProperty.valueAsDouble( context.
expressionContext(), mAngle );
175 for (
int part = 0; part < multiLine->
numGeometries(); ++part )
182 std::vector<QgsPoint> samplingPoints = generateSamplingPoints( *lineString, parameters, context );
184 for (
int i = 0; i < static_cast<int>( samplingPoints.size() ); ++i )
186 const QgsPoint &pt = samplingPoints[i];
189 double azimuth = calculateAzimuth( *lineString, pt, i );
193 attrs << current << number << i + 1 << evaluatedAngle << ( ( mOrientation == QgsTransectAlgorithmBase::Both ) ? evaluatedLength * 2 : evaluatedLength ) << static_cast<int>( mOrientation );
195 outFeat.
setGeometry( calcTransect( pt, azimuth, evaluatedLength, mOrientation, evaluatedAngle, mDirection ) );
206 outputs.insert( u
"OUTPUT"_s, dest );
211 const QgsPoint &point,
const double angleAtVertex,
const double length,
const QgsTransectAlgorithmBase::Side orientation,
const double angle,
const QgsTransectAlgorithmBase::Direction direction
220 switch ( orientation )
222 case QgsTransectAlgorithmBase::Right:
223 pStart = point.
project( length, angle + 180.0 / M_PI * angleAtVertex );
227 case QgsTransectAlgorithmBase::Left:
228 pEnd = point.
project( -length, angle + 180.0 / M_PI * angleAtVertex );
232 case QgsTransectAlgorithmBase::Both:
233 pStart = point.
project( length, angle + 180.0 / M_PI * angleAtVertex );
234 pEnd = point.
project( -length, angle + 180.0 / M_PI * angleAtVertex );
243 if ( direction == QgsTransectAlgorithmBase::LeftToRight )
245 transect.append( pEnd );
246 transect.append( pStart );
250 transect.append( pStart );
251 transect.append( pEnd );
@ 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.
virtual QgsAbstractGeometry * segmentize(double tolerance=M_PI/180., SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a version of the geometry without curves.
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.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
Line string geometry type, with support for z-dimension and m-values.
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...
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
Definition for a property.
@ Double
Double value (including negative values).
@ DoublePositive
Positive double value (including 0).
A store for object properties.
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 Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
static Q_INVOKABLE bool isCurvedType(Qgis::WkbType type)
Returns true if the WKB type is a curved type or can contain curved geometries.
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).
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QgsPointSequence QgsPolyline
Polyline as represented as a vector of points.