QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsalgorithmsegmentize.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmsegmentize.cpp
3  ---------------------
4  begin : March 2018
5  copyright : (C) 2018 by Nyall Dawson
6  email : nyall dot dawson at gmail 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 
18 #include "qgsalgorithmsegmentize.h"
19 
21 
22 QString QgsSegmentizeByMaximumDistanceAlgorithm::name() const
23 {
24  return QStringLiteral( "segmentizebymaxdistance" );
25 }
26 
27 QString QgsSegmentizeByMaximumDistanceAlgorithm::displayName() const
28 {
29  return QObject::tr( "Segmentize by maximum distance" );
30 }
31 
32 QStringList QgsSegmentizeByMaximumDistanceAlgorithm::tags() const
33 {
34  return QObject::tr( "straighten,linearize,densify,curves,curved,circular" ).split( ',' );
35 }
36 
37 QString QgsSegmentizeByMaximumDistanceAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsSegmentizeByMaximumDistanceAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsSegmentizeByMaximumDistanceAlgorithm::outputName() const
48 {
49  return QObject::tr( "Segmentized" );
50 }
51 
52 QString QgsSegmentizeByMaximumDistanceAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm segmentizes a geometry by converting curved sections to linear sections.\n\n"
55  "The segmentization is performed by specifying the maximum allowed offset distance between the original"
56  "curve and the segmentized representation.\n\n"
57  "Non-curved geometries will be retained without change." );
58 }
59 
60 QgsSegmentizeByMaximumDistanceAlgorithm *QgsSegmentizeByMaximumDistanceAlgorithm::createInstance() const
61 {
62  return new QgsSegmentizeByMaximumDistanceAlgorithm();
63 }
64 
65 QList<int> QgsSegmentizeByMaximumDistanceAlgorithm::inputLayerTypes() const
66 {
68 }
69 
70 void QgsSegmentizeByMaximumDistanceAlgorithm::initParameters( const QVariantMap & )
71 {
72  std::unique_ptr< QgsProcessingParameterDistance > tolerance = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "DISTANCE" ),
73  QObject::tr( "Maximum offset distance" ),
74  1.0, QStringLiteral( "INPUT" ), false, 0, 10000000.0 );
75  tolerance->setIsDynamic( true );
76  tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Maximum offset distance" ), QgsPropertyDefinition::DoublePositive ) );
77  tolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
78  addParameter( tolerance.release() );
79 }
80 
81 bool QgsSegmentizeByMaximumDistanceAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
82 {
83  mTolerance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
84  mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
85  if ( mDynamicTolerance )
86  mToleranceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >();
87 
88  return true;
89 }
90 
91 QgsFeatureList QgsSegmentizeByMaximumDistanceAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
92 {
93  QgsFeature f = feature;
94  if ( f.hasGeometry() )
95  {
96  QgsGeometry geometry = f.geometry();
97  double tolerance = mTolerance;
98  if ( mDynamicTolerance )
99  tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
101  f.setGeometry( geometry );
102  }
103  return QgsFeatureList() << f;
104 }
105 
106 
107 
108 
109 
110 QString QgsSegmentizeByMaximumAngleAlgorithm::name() const
111 {
112  return QStringLiteral( "segmentizebymaxangle" );
113 }
114 
115 QString QgsSegmentizeByMaximumAngleAlgorithm::displayName() const
116 {
117  return QObject::tr( "Segmentize by maximum angle" );
118 }
119 
120 QStringList QgsSegmentizeByMaximumAngleAlgorithm::tags() const
121 {
122  return QObject::tr( "straighten,linearize,densify,curves,curved,circular,angle" ).split( ',' );
123 }
124 
125 QString QgsSegmentizeByMaximumAngleAlgorithm::group() const
126 {
127  return QObject::tr( "Vector geometry" );
128 }
129 
130 QString QgsSegmentizeByMaximumAngleAlgorithm::groupId() const
131 {
132  return QStringLiteral( "vectorgeometry" );
133 }
134 
135 QString QgsSegmentizeByMaximumAngleAlgorithm::outputName() const
136 {
137  return QObject::tr( "Segmentized" );
138 }
139 
140 QString QgsSegmentizeByMaximumAngleAlgorithm::shortHelpString() const
141 {
142  return QObject::tr( "This algorithm segmentizes a geometry by converting curved sections to linear sections.\n\n"
143  "The segmentization is performed by specifying the maximum allowed radius angle between vertices "
144  "on the straightened geometry (e.g the angle of the arc created from the original arc center to consecutive "
145  "output vertices on the linearized geometry).\n\n"
146  "Non-curved geometries will be retained without change." );
147 }
148 
149 QgsSegmentizeByMaximumAngleAlgorithm *QgsSegmentizeByMaximumAngleAlgorithm::createInstance() const
150 {
151  return new QgsSegmentizeByMaximumAngleAlgorithm();
152 }
153 
154 QList<int> QgsSegmentizeByMaximumAngleAlgorithm::inputLayerTypes() const
155 {
157 }
158 
159 void QgsSegmentizeByMaximumAngleAlgorithm::initParameters( const QVariantMap & )
160 {
161  std::unique_ptr< QgsProcessingParameterNumber > tolerance = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "ANGLE" ),
162  QObject::tr( "Maximum angle between vertices (degrees)" ), QgsProcessingParameterNumber::Double,
163  5.0, false, 0, 360.0 );
164  tolerance->setIsDynamic( true );
165  tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "ANGLE" ), QObject::tr( "Maximum angle between vertices (degrees)" ), QgsPropertyDefinition::DoublePositive ) );
166  tolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
167  addParameter( tolerance.release() );
168 }
169 
170 bool QgsSegmentizeByMaximumAngleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
171 {
172  mTolerance = parameterAsDouble( parameters, QStringLiteral( "ANGLE" ), context );
173  mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ANGLE" ) );
174  if ( mDynamicTolerance )
175  mToleranceProperty = parameters.value( QStringLiteral( "ANGLE" ) ).value< QgsProperty >();
176 
177  return true;
178 }
179 
180 QgsFeatureList QgsSegmentizeByMaximumAngleAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
181 {
182  QgsFeature f = feature;
183  if ( f.hasGeometry() )
184  {
185  QgsGeometry geometry = f.geometry();
186  double tolerance = mTolerance;
187  if ( mDynamicTolerance )
188  tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
189  geometry.convertToStraightSegment( M_PI * tolerance / 180.0, QgsAbstractGeometry::MaximumAngle );
190  f.setGeometry( geometry );
191  }
192  return QgsFeatureList() << f;
193 }
194 
196 
197 
Maximum distance between an arbitrary point on the original curve and closest point on its approximat...
Base class for providing feedback from a processing algorithm.
Maximum angle between generating radii (lines from arc center to output vertices) ...
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
Positive double value (including 0)
Definition: qgsproperty.h:58
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
void convertToStraightSegment(double tolerance=M_PI/180., QgsAbstractGeometry::SegmentationToleranceType toleranceType=QgsAbstractGeometry::MaximumAngle)
Converts the geometry to straight line segments, if it is a curved geometry type. ...
Vector polygon layers.
Definition: qgsprocessing.h:50
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
A store for object properties.
Definition: qgsproperty.h:229
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition for a property.
Definition: qgsproperty.h:46
Vector line layers.
Definition: qgsprocessing.h:49
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
Contains information about the context in which a processing algorithm is executed.
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...