QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmtransectfixeddistance.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmtransectfixeddistance.cpp
3 -------------------------------------
4 begin : September 2025
5 copyright : (C) 2025 by Loïc Bartoletti
6 email : loic dot bartoletti at oslandia dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgslinestring.h"
21#include "qgsmultilinestring.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29QString QgsTransectFixedDistanceAlgorithm::name() const
30{
31 return u"transectfixeddistance"_s;
32}
33
34QString QgsTransectFixedDistanceAlgorithm::displayName() const
35{
36 return QObject::tr( "Transect (fixed distance)" );
37}
38
39QStringList QgsTransectFixedDistanceAlgorithm::tags() const
40{
41 return QObject::tr( "transect,station,lines,extend,fixed,interval,distance" ).split( ',' );
42}
43
44QString QgsTransectFixedDistanceAlgorithm::shortHelpString() const
45{
46 return QObject::tr( "This algorithm creates transects at fixed distance intervals along (multi)linestrings.\n" )
47 + QObject::tr( "A transect is a line oriented from an angle (by default perpendicular) to the input polylines at regular intervals." )
48 + u"\n\n"_s
49 + QObject::tr( "Field(s) from feature(s) are returned in the transect with these new fields:\n" )
50 + QObject::tr( "- TR_FID: ID of the original feature\n" )
51 + QObject::tr( "- TR_ID: ID of the transect. Each transect have an unique ID\n" )
52 + QObject::tr( "- TR_SEGMENT: ID of the segment of the linestring\n" )
53 + QObject::tr( "- TR_ANGLE: Angle in degrees from the original line at the vertex\n" )
54 + QObject::tr( "- TR_LENGTH: Total length of the transect returned\n" )
55 + QObject::tr( "- TR_ORIENT: Side of the transect (only on the left or right of the line, or both side)\n" );
56}
57
58QString QgsTransectFixedDistanceAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Creates transects at fixed distance intervals along (multi)linestrings." );
61}
62
63QgsTransectFixedDistanceAlgorithm *QgsTransectFixedDistanceAlgorithm::createInstance() const
64{
65 return new QgsTransectFixedDistanceAlgorithm();
66}
67
68void QgsTransectFixedDistanceAlgorithm::addAlgorithmParams()
69{
70 addParameter( new QgsProcessingParameterDistance( u"INTERVAL"_s, QObject::tr( "Fixed sampling interval" ), 10.0, u"INPUT"_s, false, 0.0001 ) );
71 addParameter( new QgsProcessingParameterBoolean( u"INCLUDE_START"_s, QObject::tr( "Include transect at start of line" ), true ) );
72}
73
74bool QgsTransectFixedDistanceAlgorithm::prepareAlgorithmTransectParameters( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
75{
76 mInterval = parameterAsDouble( parameters, u"INTERVAL"_s, context );
77 mIncludeStartPoint = parameterAsBool( parameters, u"INCLUDE_START"_s, context );
78 return true;
79}
80
81std::vector<QgsPoint> QgsTransectFixedDistanceAlgorithm::generateSamplingPoints( const QgsLineString &line, const QVariantMap &, QgsProcessingContext & )
82{
83 std::vector<QgsPoint> samplingPoints;
84
86 if ( line.is3D() )
87 pointType = Qgis::WkbType::PointZ;
88 if ( line.isMeasure() )
89 pointType = QgsWkbTypes::addM( pointType );
90
91 if ( mIncludeStartPoint )
92 {
93 samplingPoints.push_back( line.startPoint() );
94 }
95
96 line.visitPointsByRegularDistance( mInterval, [&]( double x, double y, double z, double m, double, double, double, double, double, double, double, double ) -> bool {
97 samplingPoints.emplace_back( pointType, x, y, z, m );
98 return true;
99 } );
100
101 return samplingPoints;
102}
103
104double QgsTransectFixedDistanceAlgorithm::calculateAzimuth( const QgsLineString &line, const QgsPoint &point, int )
105{
106 // For fixed distance sampling, find closest segment
107 QgsPoint segPt;
108 QgsVertexId vid;
109 const double sqrDist = line.closestSegment( point, segPt, vid, nullptr, Qgis::DEFAULT_SEGMENT_EPSILON );
110
111 // Validate closestSegment found a valid segment
112 if ( sqrDist < 0 || vid.vertex < 1 )
113 {
114 // Fallback: use azimuth from first to second vertex if line has at least 2 vertices
115 if ( line.numPoints() >= 2 )
116 {
117 return line.pointN( 0 ).azimuth( line.pointN( 1 ) ) * M_PI / 180.0;
118 }
119 return 0.0;
120 }
121
122 QgsVertexId prev( vid.part, vid.ring, vid.vertex - 1 );
123 return line.vertexAt( prev ).azimuth( line.vertexAt( vid ) ) * M_PI / 180.0;
124}
125
static const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition qgis.h:6558
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ Point
Point.
Definition qgis.h:282
@ PointZ
PointZ.
Definition qgis.h:299
bool isMeasure() const
Returns true if the geometry contains m values.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
QgsPoint vertexAt(QgsVertexId id) const override
Returns the point corresponding to a specified vertex id.
Definition qgscurve.cpp:199
Line string geometry type, with support for z-dimension and m-values.
QgsPoint startPoint() const override
Returns the starting point of the curve.
int numPoints() const override
Returns the number of points in the curve.
QgsPoint pointN(int i) const
Returns the specified point from inside the line string.
void visitPointsByRegularDistance(double distance, const std::function< bool(double x, double y, double z, double m, double startSegmentX, double startSegmentY, double startSegmentZ, double startSegmentM, double endSegmentX, double endSegmentY, double endSegmentZ, double endSegmentM) > &visitPoint) const
Visits regular points along the linestring, spaced by distance.
double closestSegment(const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf=nullptr, double epsilon=4 *std::numeric_limits< double >::epsilon()) const override
Searches for the closest segment of the geometry to a given point.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
double azimuth(const QgsPoint &other) const
Calculates Cartesian azimuth between this point and other one (clockwise in degree,...
Definition qgspoint.cpp:711
Contains information about the context in which a processing algorithm is executed.
Base class for providing feedback from a processing algorithm.
A boolean parameter for processing algorithms.
A double numeric parameter for distance values.
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:34
int vertex
Vertex number.
Definition qgsvertexid.h:98
int part
Part number.
Definition qgsvertexid.h:92
int ring
Ring number.
Definition qgsvertexid.h:95