QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
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
20#include "qgsprocessing.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsOrthogonalizeAlgorithm::name() const
29{
30 return u"orthogonalize"_s;
31}
32
33QString QgsOrthogonalizeAlgorithm::displayName() const
34{
35 return QObject::tr( "Orthogonalize" );
36}
37
38QStringList QgsOrthogonalizeAlgorithm::tags() const
39{
40 return QObject::tr( "rectangle,perpendicular,right,angles,square,quadrilateralise" ).split( ',' );
41}
42
43QString QgsOrthogonalizeAlgorithm::group() const
44{
45 return QObject::tr( "Vector geometry" );
46}
47
48QString QgsOrthogonalizeAlgorithm::groupId() const
49{
50 return u"vectorgeometry"_s;
51}
52
53QString QgsOrthogonalizeAlgorithm::shortHelpString() const
54{
55 return QObject::tr(
56 "Takes a line or polygon layer and attempts to orthogonalize "
57 "all the geometries in the layer. This process shifts the nodes "
58 "in the geometries to try to make every angle in the geometry "
59 "either a right angle or a straight line.\n\n"
60 "The angle tolerance parameter is used to specify the maximum "
61 "deviation from a right angle or straight line a node can have "
62 "for it to be adjusted. Smaller tolerances mean that only nodes "
63 "which are already closer to right angles will be adjusted, and "
64 "larger tolerances mean that nodes which deviate further from "
65 "right angles will also be adjusted.\n\n"
66 "The algorithm is iterative. Setting a larger number for the maximum "
67 "iterations will result in a more orthogonal geometry at the cost of "
68 "extra processing time."
69 );
70}
71
72QString QgsOrthogonalizeAlgorithm::shortDescription() const
73{
74 return QObject::tr(
75 "Takes a line or polygon layer and attempts to orthogonalize "
76 "all the geometries in the layer."
77 );
78}
79
80QString QgsOrthogonalizeAlgorithm::outputName() const
81{
82 return QObject::tr( "Orthogonalized" );
83}
84
85QList<int> QgsOrthogonalizeAlgorithm::inputLayerTypes() const
86{
87 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon ) << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
88}
89
90QgsOrthogonalizeAlgorithm *QgsOrthogonalizeAlgorithm::createInstance() const
91{
92 return new QgsOrthogonalizeAlgorithm();
93}
94
95void QgsOrthogonalizeAlgorithm::initParameters( const QVariantMap & )
96{
97 auto angleToleranceParam
98 = std::make_unique<QgsProcessingParameterNumber>( u"ANGLE_TOLERANCE"_s, QObject::tr( "Maximum angle tolerance (degrees)" ), Qgis::ProcessingNumberParameterType::Double, 15.0, false, 0.0, 45.0 );
99 angleToleranceParam->setIsDynamic( true );
100 angleToleranceParam->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Angle tolerance"_s, QObject::tr( "Maximum angle tolerance (degrees)" ), QgsPropertyDefinition::Double ) );
101 angleToleranceParam->setDynamicLayerParameterName( u"INPUT"_s );
102 addParameter( angleToleranceParam.release() );
103
104 auto maxIterations
105 = std::make_unique<QgsProcessingParameterNumber>( u"MAX_ITERATIONS"_s, QObject::tr( "Maximum algorithm iterations" ), Qgis::ProcessingNumberParameterType::Integer, 1000, false, 1, 10000 );
106 maxIterations->setFlags( maxIterations->flags() | Qgis::ProcessingParameterFlag::Advanced );
107 addParameter( maxIterations.release() );
108}
109
110bool QgsOrthogonalizeAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
111{
112 mAngleTolerance = parameterAsDouble( parameters, u"ANGLE_TOLERANCE"_s, context );
113 mDynamicAngleTolerance = QgsProcessingParameters::isDynamic( parameters, u"ANGLE_TOLERANCE"_s );
114 if ( mDynamicAngleTolerance )
115 mAngleToleranceProperty = parameters.value( u"ANGLE_TOLERANCE"_s ).value<QgsProperty>();
116
117 mMaxIterations = parameterAsDouble( parameters, u"MAX_ITERATIONS"_s, context );
118
119 return true;
120}
121
122QgsFeatureList QgsOrthogonalizeAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
123{
124 QgsFeature f = feature;
125
126 if ( f.hasGeometry() )
127 {
128 double angleTolerance = mAngleTolerance;
129 if ( mDynamicAngleTolerance )
130 angleTolerance = mAngleToleranceProperty.valueAsDouble( context.expressionContext(), angleTolerance );
131
132 const QgsGeometry outputGeometry = f.geometry().orthogonalize( 1.0e-8, mMaxIterations, angleTolerance );
133 if ( outputGeometry.isNull() )
134 throw QgsProcessingException( QObject::tr( "Error orthogonalizing geometry" ) );
135
136 f.setGeometry( outputGeometry );
137 }
138
139 return QgsFeatureList() << f;
140}
141
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
@ VectorLine
Vector line layers.
Definition qgis.h:3649
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3880
@ Double
Double/float values.
Definition qgis.h:3921
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
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...
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
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 for a property.
Definition qgsproperty.h:47
@ Double
Double value (including negative values).
Definition qgsproperty.h:56
A store for object properties.
QList< QgsFeature > QgsFeatureList