Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add roundness method to curve polygon (#45154)
* add roundness function * add roundness function tests * add roundness function help * add roundness expression * add roundness expression tests * add roundness expression help * add roundness processing * add roundness processing tests * add roundness processing help * fix import and typo * Fix typo Co-authored-by: Harrissou Sant-anna <delazj@gmail.com> * Add tag Co-authored-by: Harrissou Sant-anna <delazj@gmail.com> * Fix typo Co-authored-by: Harrissou Sant-anna <delazj@gmail.com> * Modify the description Co-authored-by: Harrissou Sant-anna <delazj@gmail.com> * Replace equality condition by qgsDoubleNear Co-authored-by: Loïc Bartoletti <lbartoletti@users.noreply.github.com> * Change types for literals Co-authored-by: Loïc Bartoletti <lbartoletti@users.noreply.github.com> * add since version Co-authored-by: Loïc Bartoletti <lbartoletti@users.noreply.github.com> * add a const * add the \since adding in the sip file * Fix typo Co-authored-by: Loïc Bartoletti <lbartoletti@users.noreply.github.com> * Fix typo Co-authored-by: Loïc Bartoletti <lbartoletti@users.noreply.github.com> * Remove SIP factory annotation Co-authored-by: Nyall Dawson <nyall.dawson@gmail.com> * Remove double calculation of permimeter Co-authored-by: Nyall Dawson <nyall.dawson@gmail.com> * Always add an attribute Co-authored-by: Nyall Dawson <nyall.dawson@gmail.com> * Use QGSCOMPARENEAR in tests Co-authored-by: Nyall Dawson <nyall.dawson@gmail.com> * Shorten the short description Co-authored-by: Nyall Dawson <nyall.dawson@gmail.com> * Make a more complete help string Co-authored-by: Nyall Dawson <nyall.dawson@gmail.com> * Correct the description Co-authored-by: Nyall Dawson <nyall.dawson@gmail.com> * reformat bad code styles * sipify Co-authored-by: Harrissou Sant-anna <delazj@gmail.com> Co-authored-by: Loïc Bartoletti <lbartoletti@users.noreply.github.com> Co-authored-by: Nyall Dawson <nyall.dawson@gmail.com>
- Loading branch information
1 parent
209fd38
commit 97b288d
Showing
12 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "roundness", | ||
"type": "function", | ||
"groups": ["GeometryGroup"], | ||
"description": "Calculates how close a polygon shape is to a circle. The function returns 1 when the polygon shape is a perfect circle and 0 when it is completely flat.", | ||
"arguments": [ | ||
{"arg":"geometry","description":"a polygon"}], | ||
"examples": [ | ||
{ "expression":"round(roundness(geom_from_wkt('POLYGON(( 0 0, 0 1, 1 1, 1 0, 0 0))')), 3)", "returns":"0.785"}, | ||
{ "expression":"round(roundness(geom_from_wkt('POLYGON(( 0 0, 0 0.1, 1 0.1, 1 0, 0 0))')), 3)", "returns":"0.260"} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/*************************************************************************** | ||
qgsalgorithmroundness.cpp | ||
--------------------- | ||
begin : September 2021 | ||
copyright : (C) 2021 by Antoine Facchini | ||
email : antoine dot facchini @oslandia dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* 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 "qgsalgorithmroundness.h" | ||
#include "qgscurvepolygon.h" | ||
|
||
///@cond PRIVATE | ||
|
||
QString QgsRoundnessAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "roundness" ); | ||
} | ||
|
||
QString QgsRoundnessAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Roundness" ); | ||
} | ||
|
||
QStringList QgsRoundnessAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "roundness,circle" ).split( ',' ); | ||
} | ||
|
||
QString QgsRoundnessAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Vector geometry" ); | ||
} | ||
|
||
QString QgsRoundnessAlgorithm::groupId() const | ||
{ | ||
return QStringLiteral( "vectorgeometry" ); | ||
} | ||
|
||
QString QgsRoundnessAlgorithm::outputName() const | ||
{ | ||
return QObject::tr( "Roundness" ); | ||
} | ||
|
||
QString QgsRoundnessAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "Calculates the roundness of each feature and stores it as a new field. The input vector layer must contain polygons.\n\n" | ||
"The roundness of a polygon is defined as 4π × polygon area / perimeter². The roundness value varies between 0 and 1. A perfect circle has a roundness of 1, while a completely flat polygon has a roundness of 0." ); | ||
} | ||
|
||
QString QgsRoundnessAlgorithm::shortDescription() const | ||
{ | ||
return QObject::tr( "Calculates the roundness of polygon features." ); | ||
} | ||
|
||
QgsRoundnessAlgorithm *QgsRoundnessAlgorithm::createInstance() const | ||
{ | ||
return new QgsRoundnessAlgorithm(); | ||
} | ||
|
||
QList<int> QgsRoundnessAlgorithm::inputLayerTypes() const | ||
{ | ||
return QList<int>() << QgsProcessing::TypeVectorPolygon; | ||
} | ||
|
||
QgsProcessing::SourceType QgsRoundnessAlgorithm::outputLayerType() const | ||
{ | ||
return QgsProcessing::TypeVectorPolygon; | ||
} | ||
|
||
QgsFields QgsRoundnessAlgorithm::outputFields( const QgsFields &inputFields ) const | ||
{ | ||
QgsFields outputFields = inputFields; | ||
outputFields.append( QgsField( QStringLiteral( "roundness" ), QVariant::Double ) ); | ||
return outputFields; | ||
} | ||
|
||
QgsFeatureList QgsRoundnessAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * ) | ||
{ | ||
QgsFeature f = feature; | ||
QgsAttributes attributes = f.attributes(); | ||
if ( f.hasGeometry() ) | ||
{ | ||
QgsGeometry geom = f.geometry(); | ||
if ( const QgsCurvePolygon *poly = qgsgeometry_cast< const QgsCurvePolygon * >( geom.constGet()->simplifiedTypeRef() ) ) | ||
{ | ||
double roundness = poly->roundness(); | ||
attributes << QVariant( roundness ); | ||
} | ||
else | ||
{ | ||
attributes << QVariant(); | ||
} | ||
} | ||
else | ||
{ | ||
attributes << QVariant(); | ||
} | ||
f.setAttributes( attributes ); | ||
return QgsFeatureList() << f; | ||
} | ||
|
||
///@endcond | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/*************************************************************************** | ||
qgsalgorithmroundness.h | ||
--------------------- | ||
begin : September 2021 | ||
copyright : (C) 2021 by Antoine Facchini | ||
email : antoine dot facchini @oslandia dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* 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. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSALGORITHMROUNDNESS_H | ||
#define QGSALGORITHMROUNDNESS_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis_sip.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native roundness algorithm. | ||
*/ | ||
class QgsRoundnessAlgorithm : public QgsProcessingFeatureBasedAlgorithm | ||
{ | ||
|
||
public: | ||
|
||
QgsRoundnessAlgorithm() = default; | ||
QString name() const override; | ||
QString displayName() const override; | ||
QStringList tags() const override; | ||
QString group() const override; | ||
QString groupId() const override; | ||
QString shortHelpString() const override; | ||
QString shortDescription() const override; | ||
QgsRoundnessAlgorithm *createInstance() const override SIP_FACTORY; | ||
QList<int> inputLayerTypes() const override; | ||
|
||
protected: | ||
QString outputName() const override; | ||
QgsProcessing::SourceType outputLayerType() const override; | ||
QgsFields outputFields( const QgsFields &inputFields ) const override; | ||
|
||
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
}; | ||
|
||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMROUNDNESS_H | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters