QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsalgorithmorthogonalize.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmorthogonalize.cpp
3  ---------------------
4  begin : November 2019
5  copyright : (C) 2019 by Alexander Bruy
6  email : alexander dot bruy 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 
19 #include "qgsprocessing.h"
20 
22 
23 QString QgsOrthogonalizeAlgorithm::name() const
24 {
25  return QStringLiteral( "orthogonalize" );
26 }
27 
28 QString QgsOrthogonalizeAlgorithm::displayName() const
29 {
30  return QObject::tr( "Orthogonalize" );
31 }
32 
33 QStringList QgsOrthogonalizeAlgorithm::tags() const
34 {
35  return QObject::tr( "rectangle,perpendicular,right,angles,square,quadrilateralise" ).split( ',' );
36 }
37 
38 QString QgsOrthogonalizeAlgorithm::group() const
39 {
40  return QObject::tr( "Vector geometry" );
41 }
42 
43 QString QgsOrthogonalizeAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeometry" );
46 }
47 
48 QString QgsOrthogonalizeAlgorithm::shortHelpString() const
49 {
50  return QObject::tr( "Takes a line or polygon layer and attempts to orthogonalize "
51  "all the geometries in the layer. This process shifts the nodes "
52  "in the geometries to try to make every angle in the geometry "
53  "either a right angle or a straight line.\n\n"
54  "The angle tolerance parameter is used to specify the maximum "
55  "deviation from a right angle or straight line a node can have "
56  "for it to be adjusted. Smaller tolerances mean that only nodes "
57  "which are already closer to right angles will be adjusted, and "
58  "larger tolerances mean that nodes which deviate further from "
59  "right angles will also be adjusted.\n\n"
60  "The algorithm is iterative. Setting a larger number for the maximum "
61  "iterations will result in a more orthogonal geometry at the cost of "
62  "extra processing time." );
63 }
64 
65 QString QgsOrthogonalizeAlgorithm::outputName() const
66 {
67  return QObject::tr( "Orthogonalized" );
68 }
69 
70 QList<int> QgsOrthogonalizeAlgorithm::inputLayerTypes() const
71 {
73 }
74 
75 QgsOrthogonalizeAlgorithm *QgsOrthogonalizeAlgorithm::createInstance() const
76 {
77  return new QgsOrthogonalizeAlgorithm();
78 }
79 
80 void QgsOrthogonalizeAlgorithm::initParameters( const QVariantMap & )
81 {
82  auto angleToleranceParam = std::make_unique < QgsProcessingParameterNumber >( QStringLiteral( "ANGLE_TOLERANCE" ), QObject::tr( "Maximum angle tolerance (degrees)" ),
83  QgsProcessingParameterNumber::Double, 15.0, false, 0.0, 45.0 );
84  angleToleranceParam->setIsDynamic( true );
85  angleToleranceParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Angle tolerance" ), QObject::tr( "Maximum angle tolerance (degrees)" ), QgsPropertyDefinition::Double ) );
86  angleToleranceParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
87  addParameter( angleToleranceParam.release() );
88 
89  std::unique_ptr< QgsProcessingParameterNumber> maxIterations = std::make_unique< QgsProcessingParameterNumber >(
90  QStringLiteral( "MAX_ITERATIONS" ),
91  QObject::tr( "Maximum algorithm iterations" ),
93  1000, false, 1, 10000 );
94  maxIterations->setFlags( maxIterations->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
95  addParameter( maxIterations.release() );
96 }
97 
98 bool QgsOrthogonalizeAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
99 {
100  mAngleTolerance = parameterAsDouble( parameters, QStringLiteral( "ANGLE_TOLERANCE" ), context );
101  mDynamicAngleTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ANGLE_TOLERANCE" ) );
102  if ( mDynamicAngleTolerance )
103  mAngleToleranceProperty = parameters.value( QStringLiteral( "ANGLE_TOLERANCE" ) ).value< QgsProperty >();
104 
105  mMaxIterations = parameterAsDouble( parameters, QStringLiteral( "MAX_ITERATIONS" ), context );
106 
107  return true;
108 }
109 
110 QgsFeatureList QgsOrthogonalizeAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
111 {
112  QgsFeature f = feature;
113 
114  if ( f.hasGeometry() )
115  {
116  double angleTolerance = mAngleTolerance;
117  if ( mDynamicAngleTolerance )
118  angleTolerance = mAngleToleranceProperty.valueAsDouble( context.expressionContext(), angleTolerance );
119 
120  const QgsGeometry outputGeometry = f.geometry().orthogonalize( 1.0e-8, mMaxIterations, angleTolerance );
121  if ( outputGeometry.isNull() )
122  throw QgsProcessingException( QObject::tr( "Error orthogonalizing geometry" ) );
123 
124  f.setGeometry( outputGeometry );
125  }
126 
127  return QgsFeatureList() << f;
128 }
129 
QgsProcessingParameterNumber::Double
@ Double
Double/float values.
Definition: qgsprocessingparameters.h:2187
QgsProperty
A store for object properties.
Definition: qgsproperty.h:230
QgsProcessingParameters::isDynamic
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...
Definition: qgsprocessingparameters.cpp:114
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
qgsalgorithmorthogonalize.h
QgsProcessing::TypeVectorPolygon
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:51
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessingParameterDefinition::FlagAdvanced
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition: qgsprocessingparameters.h:451
QgsPropertyDefinition::Double
@ Double
Double value (including negative values)
Definition: qgsproperty.h:57
QgsGeometry::orthogonalize
QgsGeometry orthogonalize(double tolerance=1.0E-8, int maxIterations=1000, double angleThreshold=15.0) const
Attempts to orthogonalize a line or polygon geometry by shifting vertices to make the geometries angl...
Definition: qgsgeometry.cpp:1188
QgsProcessing::TypeVectorLine
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:50
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:46
QgsGeometry::isNull
bool isNull
Definition: qgsgeometry.h:127
QgsProcessingParameterNumber::Integer
@ Integer
Integer values.
Definition: qgsprocessingparameters.h:2186
qgsprocessing.h
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:149
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82