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() );
67 angle->setIsDynamic(
true );
69 angle->setDynamicLayerParameterName( u
"INPUT"_s );
70 addParameter(
angle.release() );
72 addParameter(
new QgsProcessingParameterEnum( u
"SIDE"_s, QObject::tr(
"Side to create the transects" ), QStringList() << QObject::tr(
"Left" ) << QObject::tr(
"Right" ) << QObject::tr(
"Both" ),
false, 2 ) );
74 auto direction = std::make_unique<QgsProcessingParameterEnum>( u
"DIRECTION"_s, QObject::tr(
"Direction" ), QStringList() << QObject::tr(
"Right to Left" ) << QObject::tr(
"Left to Right" ),
false, 0,
true );
75 direction->setGuiDefaultValueOverride( 1 );
76 addParameter( direction.release() );
83 mOrientation =
static_cast<QgsTransectAlgorithmBase::Side
>( parameterAsInt( parameters, u
"SIDE"_s, context ) );
84 mAngle = fabs( parameterAsDouble( parameters, u
"ANGLE"_s, context ) );
87 mAngleProperty = parameters.value( u
"ANGLE"_s ).value<
QgsProperty>();
89 mLength = parameterAsDouble( parameters, u
"LENGTH"_s, context );
92 mLengthProperty = parameters.value( u
"LENGTH"_s ).value<
QgsProperty>();
94 if ( mOrientation == QgsTransectAlgorithmBase::Both )
97 mDirection =
static_cast<QgsTransectAlgorithmBase::Direction
>( parameterAsInt( parameters, u
"DIRECTION"_s, context ) );
100 if ( !prepareAlgorithmTransectParameters( parameters, context, feedback ) )
101 return QVariantMap();
103 std::unique_ptr<QgsFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
110 newFields.
append(
QgsField( u
"TR_FID"_s, QMetaType::Type::Int, QString(), 20 ) );
111 newFields.
append(
QgsField( u
"TR_ID"_s, QMetaType::Type::Int, QString(), 20 ) );
112 newFields.
append(
QgsField( u
"TR_SEGMENT"_s, QMetaType::Type::Int, QString(), 20 ) );
113 newFields.
append(
QgsField( u
"TR_ANGLE"_s, QMetaType::Type::Double, QString(), 5, 2 ) );
114 newFields.
append(
QgsField( u
"TR_LENGTH"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
115 newFields.
append(
QgsField( u
"TR_ORIENT"_s, QMetaType::Type::Int, QString(), 1 ) );
125 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields, outputWkb, source->sourceCrs(),
QgsFeatureSink::RegeneratePrimaryKey ) );
133 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 1;
150 if ( mDynamicLength || mDynamicAngle )
155 double evaluatedLength = mLength;
156 if ( mDynamicLength )
158 double evaluatedAngle = mAngle;
160 evaluatedAngle = mAngleProperty.valueAsDouble( context.
expressionContext(), mAngle );
171 for (
int part = 0; part < multiLine->
numGeometries(); ++part )
178 std::vector<QgsPoint> samplingPoints = generateSamplingPoints( *lineString, parameters, context );
180 for (
int i = 0; i < static_cast<int>( samplingPoints.size() ); ++i )
182 const QgsPoint &pt = samplingPoints[i];
185 double azimuth = calculateAzimuth( *lineString, pt, i );
189 attrs << current << number << i + 1 << evaluatedAngle
190 << ( ( mOrientation == QgsTransectAlgorithmBase::Both ) ? evaluatedLength * 2 : evaluatedLength )
191 <<
static_cast<int>( mOrientation );
193 outFeat.
setGeometry( calcTransect( pt, azimuth, evaluatedLength, mOrientation, evaluatedAngle, mDirection ) );
204 outputs.insert( u
"OUTPUT"_s, dest );
208QgsGeometry QgsTransectAlgorithmBase::calcTransect(
const QgsPoint &point,
const double angleAtVertex,
const double length,
const QgsTransectAlgorithmBase::Side orientation,
const double angle,
const QgsTransectAlgorithmBase::Direction direction )
216 switch ( orientation )
218 case QgsTransectAlgorithmBase::Right:
219 pStart = point.
project( length, angle + 180.0 / M_PI * angleAtVertex );
223 case QgsTransectAlgorithmBase::Left:
224 pEnd = point.
project( -length, angle + 180.0 / M_PI * angleAtVertex );
228 case QgsTransectAlgorithmBase::Both:
229 pStart = point.
project( length, angle + 180.0 / M_PI * angleAtVertex );
230 pEnd = point.
project( -length, angle + 180.0 / M_PI * angleAtVertex );
239 if ( direction == QgsTransectAlgorithmBase::LeftToRight )
241 transect.append( pEnd );
242 transect.append( pStart );
246 transect.append( pStart );
247 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.