/
qgsalgorithmtransect.cpp
230 lines (187 loc) · 9.87 KB
/
qgsalgorithmtransect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/***************************************************************************
qgsalgorithmtransect.cpp
-------------------------
begin : October 2017
copyright : (C) 2017 by Loïc Bartoletti
email : lbartoletti at tuxfamily dot org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsalgorithmtransect.h"
#include "qgsmultilinestring.h"
#include "qgslinestring.h"
///@cond PRIVATE
QString QgsTransectAlgorithm::name() const
{
return QStringLiteral( "transect" );
}
QString QgsTransectAlgorithm::displayName() const
{
return QObject::tr( "Transect" );
}
QStringList QgsTransectAlgorithm::tags() const
{
return QObject::tr( "transect,station,lines,extend," ).split( ',' );
}
QString QgsTransectAlgorithm::group() const
{
return QObject::tr( "Vector geometry" );
}
QString QgsTransectAlgorithm::groupId() const
{
return QStringLiteral( "vectorgeometry" );
}
void QgsTransectAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ),
QObject::tr( "Input layer" ), QList< int >() << QgsProcessing::TypeVectorLine ) );
std::unique_ptr< QgsProcessingParameterDistance > length = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "LENGTH" ), QObject::tr( "Length of the transect" ),
5.0, QStringLiteral( "INPUT" ), false, 0 );
length->setIsDynamic( true );
length->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "LENGTH" ), QObject::tr( "Length of the transect" ), QgsPropertyDefinition::DoublePositive ) );
length->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
addParameter( length.release() );
std::unique_ptr< QgsProcessingParameterNumber > angle = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "ANGLE" ), QObject::tr( "Angle in degrees from the original line at the vertices" ), QgsProcessingParameterNumber::Double,
90.0, false, 0, 360 );
angle->setIsDynamic( true );
angle->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "ANGLE" ), QObject::tr( "Angle in degrees" ), QgsPropertyDefinition::Double ) );
angle->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
addParameter( angle.release() );
addParameter( new QgsProcessingParameterEnum( QStringLiteral( "SIDE" ), QObject::tr( "Side to create the transects" ), QStringList() << QObject::tr( "Left" ) << QObject::tr( "Right" ) << QObject::tr( "Both" ), false ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Transect" ), QgsProcessing::TypeVectorLine ) );
}
QString QgsTransectAlgorithm::shortHelpString() const
{
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" );
}
QgsTransectAlgorithm *QgsTransectAlgorithm::createInstance() const
{
return new QgsTransectAlgorithm();
}
QVariantMap QgsTransectAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
Side orientation = static_cast< QgsTransectAlgorithm::Side >( parameterAsInt( parameters, QStringLiteral( "SIDE" ), context ) );
double angle = fabs( parameterAsDouble( parameters, QStringLiteral( "ANGLE" ), context ) );
bool dynamicAngle = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ANGLE" ) );
QgsProperty angleProperty;
if ( dynamicAngle )
angleProperty = parameters.value( QStringLiteral( "ANGLE" ) ).value< QgsProperty >();
double length = parameterAsDouble( parameters, QStringLiteral( "LENGTH" ), context );
bool dynamicLength = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "LENGTH" ) );
QgsProperty lengthProperty;
if ( dynamicLength )
lengthProperty = parameters.value( QStringLiteral( "LENGTH" ) ).value< QgsProperty >();
if ( orientation == QgsTransectAlgorithm::Both )
length /= 2.0;
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
QgsExpressionContext expressionContext = createExpressionContext( parameters, context, dynamic_cast< QgsProcessingFeatureSource * >( source.get() ) );
QgsFields fields = source->fields();
fields.append( QgsField( QStringLiteral( "TR_FID" ), QVariant::Int, QString(), 20 ) );
fields.append( QgsField( QStringLiteral( "TR_ID" ), QVariant::Int, QString(), 20 ) );
fields.append( QgsField( QStringLiteral( "TR_SEGMENT" ), QVariant::Int, QString(), 20 ) );
fields.append( QgsField( QStringLiteral( "TR_ANGLE" ), QVariant::Double, QString(), 5, 2 ) );
fields.append( QgsField( QStringLiteral( "TR_LENGTH" ), QVariant::Double, QString(), 20, 6 ) );
fields.append( QgsField( QStringLiteral( "TR_ORIENT" ), QVariant::Int, QString(), 1 ) );
QgsWkbTypes::Type outputWkb = QgsWkbTypes::LineString;
if ( QgsWkbTypes::hasZ( source->wkbType() ) )
outputWkb = QgsWkbTypes::addZ( outputWkb );
if ( QgsWkbTypes::hasM( source->wkbType() ) )
outputWkb = QgsWkbTypes::addM( outputWkb );
QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields,
outputWkb, source->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
if ( !sink )
throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
QgsFeatureIterator features = source->getFeatures( );
int current = -1;
int number = 0;
double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 1;
QgsFeature feat;
while ( features.nextFeature( feat ) )
{
current++;
if ( feedback->isCanceled() )
{
break;
}
feedback->setProgress( current * step );
if ( !feat.hasGeometry() )
continue;
QgsGeometry inputGeometry = feat.geometry();
if ( dynamicLength || dynamicAngle )
{
expressionContext.setFeature( feat );
}
double evaluatedLength = length;
if ( dynamicLength )
evaluatedLength = lengthProperty.valueAsDouble( context.expressionContext(), length );
double evaluatedAngle = angle;
if ( dynamicAngle )
evaluatedAngle = angleProperty.valueAsDouble( context.expressionContext(), angle );
inputGeometry.convertToMultiType();
const QgsMultiLineString *multiLine = static_cast< const QgsMultiLineString * >( inputGeometry.constGet() );
for ( int id = 0; id < multiLine->numGeometries(); ++id )
{
const QgsLineString *line = static_cast< const QgsLineString * >( multiLine->geometryN( id ) );
QgsAbstractGeometry::vertex_iterator it = line->vertices_begin();
while ( it != line->vertices_end() )
{
QgsVertexId vertexId = it.vertexId();
int i = vertexId.vertex;
QgsFeature outFeat;
QgsAttributes attrs = feat.attributes();
attrs << current << number << i + 1 << evaluatedAngle <<
( ( orientation == QgsTransectAlgorithm::Both ) ? evaluatedLength * 2 : evaluatedLength ) <<
orientation;
outFeat.setAttributes( attrs );
double angleAtVertex = line->vertexAngle( vertexId );
outFeat.setGeometry( calcTransect( *it, angleAtVertex, evaluatedLength, orientation, evaluatedAngle ) );
sink->addFeature( outFeat, QgsFeatureSink::FastInsert );
number++;
it++;
}
}
}
QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
return outputs;
}
QgsGeometry QgsTransectAlgorithm::calcTransect( const QgsPoint &point, const double angleAtVertex, const double length, const QgsTransectAlgorithm::Side orientation, const double angle )
{
QgsPoint pLeft; // left point of the line
QgsPoint pRight; // right point of the line
QgsPolyline line;
if ( ( orientation == QgsTransectAlgorithm::Right ) || ( orientation == QgsTransectAlgorithm::Both ) )
{
pLeft = point.project( length, angle + 180.0 / M_PI * angleAtVertex );
if ( orientation != QgsTransectAlgorithm::Both )
pRight = point;
}
if ( ( orientation == QgsTransectAlgorithm::Left ) || ( orientation == QgsTransectAlgorithm::Both ) )
{
pRight = point.project( -length, angle + 180.0 / M_PI * angleAtVertex );
if ( orientation != QgsTransectAlgorithm::Both )
pLeft = point;
}
line.append( pLeft );
line.append( pRight );
return QgsGeometry::fromPolyline( line );
}
///@endcond