QGIS API Documentation 4.3.0-Master (45633be667c)
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 QGS_MARK_ALGORITHM_SOURCE
84
85 std::vector<QgsPoint> samplingPoints;
86
88 if ( line.is3D() )
89 pointType = Qgis::WkbType::PointZ;
90 if ( line.isMeasure() )
91 pointType = QgsWkbTypes::addM( pointType );
92
93 if ( mIncludeStartPoint )
94 {
95 samplingPoints.push_back( line.startPoint() );
96 }
97
98 line.visitPointsByRegularDistance( mInterval, [&]( double x, double y, double z, double m, double, double, double, double, double, double, double, double ) -> bool {
99 samplingPoints.emplace_back( pointType, x, y, z, m );
100 return true;
101 } );
102
103 return samplingPoints;
104}
105
106double QgsTransectFixedDistanceAlgorithm::calculateAzimuth( const QgsLineString &line, const QgsPoint &point, int )
107{
108 // For fixed distance sampling, find closest segment
109 QgsPoint segPt;
110 QgsVertexId vid;
111 const double sqrDist = line.closestSegment( point, segPt, vid, nullptr, Qgis::DEFAULT_SEGMENT_EPSILON );
112
113 // Validate closestSegment found a valid segment
114 if ( sqrDist < 0 || vid.vertex < 1 )
115 {
116 // Fallback: use azimuth from first to second vertex if line has at least 2 vertices
117 if ( line.numPoints() >= 2 )
118 {
119 return line.pointN( 0 ).azimuth( line.pointN( 1 ) ) * M_PI / 180.0;
120 }
121 return 0.0;
122 }
123
124 QgsVertexId prev( vid.part, vid.ring, vid.vertex - 1 );
125 return line.vertexAt( prev ).azimuth( line.vertexAt( vid ) ) * M_PI / 180.0;
126}
127
static const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition qgis.h:7312
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ Point
Point.
Definition qgis.h:296
@ PointZ
PointZ.
Definition qgis.h:313
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:198
Line string geometry type, with support for z-dimension and m-values.
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:732
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.
int numPoints() const override
Returns the number of points in the curve.
QgsPoint startPoint() const override
Returns the starting point of the curve.
QgsPoint pointN(int i) const
Returns the specified point from inside the simple curve.
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:35
int vertex
Vertex number.
int part
Part number.
Definition qgsvertexid.h:96
int ring
Ring number.
Definition qgsvertexid.h:99