24 QString QgsTransectAlgorithm::name()
const
26 return QStringLiteral(
"transect" );
29 QString QgsTransectAlgorithm::displayName()
const
31 return QObject::tr(
"Transect" );
34 QStringList QgsTransectAlgorithm::tags()
const
36 return QObject::tr(
"transect,station,lines,extend," ).split(
',' );
39 QString QgsTransectAlgorithm::group()
const
41 return QObject::tr(
"Vector geometry" );
44 QString QgsTransectAlgorithm::groupId()
const
46 return QStringLiteral(
"vectorgeometry" );
49 void QgsTransectAlgorithm::initAlgorithm(
const QVariantMap & )
53 std::unique_ptr< QgsProcessingParameterDistance > length = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral(
"LENGTH" ), QObject::tr(
"Length of the transect" ),
54 5.0, QStringLiteral(
"INPUT" ),
false, 0 );
55 length->setIsDynamic(
true );
57 length->setDynamicLayerParameterName( QStringLiteral(
"INPUT" ) );
58 addParameter( length.release() );
60 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,
61 90.0,
false, 0, 360 );
62 angle->setIsDynamic(
true );
64 angle->setDynamicLayerParameterName( QStringLiteral(
"INPUT" ) );
65 addParameter(
angle.release() );
67 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"SIDE" ), QObject::tr(
"Side to create the transects" ), QStringList() << QObject::tr(
"Left" ) << QObject::tr(
"Right" ) << QObject::tr(
"Both" ),
false ) );
72 QString QgsTransectAlgorithm::shortHelpString()
const
75 return QObject::tr(
"This algorithm creates transects on vertices for (multi)linestring.\n" ) +
76 QObject::tr(
"A transect is a line oriented from an angle (by default perpendicular) to the input polylines (at vertices)." ) +
77 QStringLiteral(
"\n\n" ) +
78 QObject::tr(
"Field(s) from feature(s) are returned in the transect with these new fields:\n" ) +
79 QObject::tr(
"- TR_FID: ID of the original feature\n" ) +
80 QObject::tr(
"- TR_ID: ID of the transect. Each transect have an unique ID\n" ) +
81 QObject::tr(
"- TR_SEGMENT: ID of the segment of the linestring\n" ) +
82 QObject::tr(
"- TR_ANGLE: Angle in degrees from the original line at the vertex\n" ) +
83 QObject::tr(
"- TR_LENGTH: Total length of the transect returned\n" ) +
84 QObject::tr(
"- TR_ORIENT: Side of the transect (only on the left or right of the line, or both side)\n" );
88 QgsTransectAlgorithm *QgsTransectAlgorithm::createInstance()
const
90 return new QgsTransectAlgorithm();
95 Side orientation =
static_cast< QgsTransectAlgorithm::Side
>( parameterAsInt( parameters, QStringLiteral(
"SIDE" ), context ) );
96 double angle = fabs( parameterAsDouble( parameters, QStringLiteral(
"ANGLE" ), context ) );
100 angleProperty = parameters.
value( QStringLiteral(
"ANGLE" ) ).value<
QgsProperty >();
102 double length = parameterAsDouble( parameters, QStringLiteral(
"LENGTH" ), context );
106 lengthProperty = parameters.
value( QStringLiteral(
"LENGTH" ) ).value<
QgsProperty >();
108 if ( orientation == QgsTransectAlgorithm::Both )
111 std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
119 fields.
append(
QgsField( QStringLiteral(
"TR_FID" ), QVariant::Int, QString(), 20 ) );
120 fields.
append(
QgsField( QStringLiteral(
"TR_ID" ), QVariant::Int, QString(), 20 ) );
121 fields.
append(
QgsField( QStringLiteral(
"TR_SEGMENT" ), QVariant::Int, QString(), 20 ) );
122 fields.
append(
QgsField( QStringLiteral(
"TR_ANGLE" ), QVariant::Double, QString(), 5, 2 ) );
123 fields.
append(
QgsField( QStringLiteral(
"TR_LENGTH" ), QVariant::Double, QString(), 20, 6 ) );
124 fields.
append(
QgsField( QStringLiteral(
"TR_ORIENT" ), QVariant::Int, QString(), 1 ) );
133 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
142 double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 1;
160 if ( dynamicLength || dynamicAngle )
165 double evaluatedLength = length;
168 double evaluatedAngle =
angle;
184 attrs << current << number << i + 1 << evaluatedAngle <<
185 ( ( orientation == QgsTransectAlgorithm::Both ) ? evaluatedLength * 2 : evaluatedLength ) <<
188 double angleAtVertex = line->
vertexAngle( vertexId );
189 outFeat.
setGeometry( calcTransect( *it, angleAtVertex, evaluatedLength, orientation, evaluatedAngle ) );
198 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
203 QgsGeometry QgsTransectAlgorithm::calcTransect(
const QgsPoint &point,
const double angleAtVertex,
const double length,
const QgsTransectAlgorithm::Side orientation,
const double angle )
210 if ( ( orientation == QgsTransectAlgorithm::Right ) || ( orientation == QgsTransectAlgorithm::Both ) )
212 pLeft = point.
project( length,
angle + 180.0 / M_PI * angleAtVertex );
213 if ( orientation != QgsTransectAlgorithm::Both )
217 if ( ( orientation == QgsTransectAlgorithm::Left ) || ( orientation == QgsTransectAlgorithm::Both ) )
219 pRight = point.
project( -length,
angle + 180.0 / M_PI * angleAtVertex );
220 if ( orientation != QgsTransectAlgorithm::Both )
224 line.append( pLeft );
225 line.append( pRight );