-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add native polygonstolines algorithm
- Loading branch information
Showing
6 changed files
with
216 additions
and
133 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
150 changes: 150 additions & 0 deletions
150
src/analysis/processing/qgsalgorithmpolygonstolines.cpp
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,150 @@ | ||
/*************************************************************************** | ||
qgsalgorithmpolygonstolines.cpp | ||
--------------------- | ||
begin : January 2019 | ||
copyright : (C) 2019 by Matthias Kuhn | ||
email : matthias@opengis.ch | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* 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 "qgsalgorithmpolygonstolines.h" | ||
#include "qgsgeometrycollection.h" | ||
#include "qgscurvepolygon.h" | ||
#include "qgscurve.h" | ||
#include "qgsmultilinestring.h" | ||
|
||
///@cond PRIVATE | ||
|
||
QString QgsPolygonsToLinesAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "polygonstolines" ); | ||
} | ||
|
||
QString QgsPolygonsToLinesAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Polygons to lines" ); | ||
} | ||
|
||
QStringList QgsPolygonsToLinesAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "line,polygon,convert" ).split( ',' ); | ||
} | ||
|
||
QString QgsPolygonsToLinesAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Vector creation" ); | ||
} | ||
|
||
QString QgsPolygonsToLinesAlgorithm::groupId() const | ||
{ | ||
return QStringLiteral( "vectorgeometry" ); | ||
} | ||
|
||
QString QgsPolygonsToLinesAlgorithm::outputName() const | ||
{ | ||
return QObject::tr( "Lines" ); | ||
} | ||
|
||
QString QgsPolygonsToLinesAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "Converts polygons to lines" ); | ||
} | ||
|
||
QString QgsPolygonsToLinesAlgorithm::shortDescription() const | ||
{ | ||
return QObject::tr( "Converts polygons to lines." ); | ||
} | ||
|
||
QgsPolygonsToLinesAlgorithm *QgsPolygonsToLinesAlgorithm::createInstance() const | ||
{ | ||
return new QgsPolygonsToLinesAlgorithm(); | ||
} | ||
|
||
QList<int> QgsPolygonsToLinesAlgorithm::inputLayerTypes() const | ||
{ | ||
return QList< int >() << QgsProcessing::TypeVectorPolygon; | ||
} | ||
|
||
QgsFeatureList QgsPolygonsToLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * ) | ||
{ | ||
QgsFeatureList result; | ||
QgsFeature feat = feature; | ||
if ( feat.hasGeometry() ) | ||
feat.setGeometry( convertToLines( feat.geometry() ) ); | ||
|
||
result << feat; | ||
return result; | ||
} | ||
|
||
QgsGeometry QgsPolygonsToLinesAlgorithm::convertToLines( const QgsGeometry &geometry ) const | ||
{ | ||
auto rings = extractRings( geometry.constGet() ); | ||
|
||
QgsWkbTypes::Type resultType = outWkbType( geometry.wkbType() ); | ||
|
||
std::unique_ptr<QgsMultiCurve> lineGeometry; | ||
|
||
if ( QgsWkbTypes::flatType( resultType ) == QgsWkbTypes::MultiLineString ) | ||
lineGeometry = qgis::make_unique<QgsMultiLineString>(); | ||
else | ||
lineGeometry = qgis::make_unique<QgsMultiCurve>(); | ||
|
||
for ( auto ring : qgis::as_const( rings ) ) | ||
lineGeometry->addGeometry( ring ); | ||
|
||
return QgsGeometry( lineGeometry.release() ); | ||
} | ||
|
||
QgsWkbTypes::Type QgsPolygonsToLinesAlgorithm::outWkbType( QgsWkbTypes::Type polygonWkbType ) const | ||
{ | ||
QgsWkbTypes::Type wkbType = QgsWkbTypes::NoGeometry; | ||
|
||
if ( QgsWkbTypes::singleType( QgsWkbTypes::flatType( polygonWkbType ) ) == QgsWkbTypes::Polygon ) | ||
wkbType = QgsWkbTypes::MultiLineString; | ||
else if ( QgsWkbTypes::singleType( QgsWkbTypes::flatType( polygonWkbType ) ) == QgsWkbTypes::CurvePolygon ) | ||
wkbType = QgsWkbTypes::MultiCurve; | ||
|
||
if ( QgsWkbTypes::hasM( polygonWkbType ) ) | ||
wkbType = QgsWkbTypes::addM( wkbType ); | ||
if ( QgsWkbTypes::hasZ( polygonWkbType ) ) | ||
wkbType = QgsWkbTypes::addZ( wkbType ); | ||
|
||
return wkbType; | ||
} | ||
|
||
QList<QgsCurve *> QgsPolygonsToLinesAlgorithm::extractRings( const QgsAbstractGeometry *geom ) const | ||
{ | ||
QList<QgsCurve *> rings; | ||
|
||
if ( QgsGeometryCollection *collection = qgsgeometry_cast<QgsGeometryCollection *>( geom ) ) | ||
{ | ||
for ( int i = 0; i < collection->numGeometries(); ++i ) | ||
{ | ||
rings.append( extractRings( collection->geometryN( i ) ) ); | ||
} | ||
} | ||
else if ( QgsCurvePolygon *polygon = qgsgeometry_cast<QgsCurvePolygon *>( geom ) ) | ||
{ | ||
rings.append( polygon->exteriorRing()->clone() ); | ||
for ( int i = 0; i < polygon->numInteriorRings(); ++i ) | ||
{ | ||
rings.append( polygon->interiorRing( i )->clone() ); | ||
} | ||
} | ||
|
||
return rings; | ||
} | ||
|
||
|
||
|
||
///@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,63 @@ | ||
/*************************************************************************** | ||
qgsalgorithmpolygontolines.h | ||
--------------------- | ||
begin : January 2019 | ||
copyright : (C) 2019 by Matthias Kuhn | ||
email : matthias@opengis.ch | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* 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 QGSALGORITHMPOLYGONSTOLINES_H | ||
#define QGSALGORITHMPOLYGONSTOLINES_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native convert polygons to lines algorithm | ||
*/ | ||
class QgsPolygonsToLinesAlgorithm : public QgsProcessingFeatureBasedAlgorithm | ||
{ | ||
|
||
public: | ||
|
||
QgsPolygonsToLinesAlgorithm() = 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; | ||
QgsPolygonsToLinesAlgorithm *createInstance() const override SIP_FACTORY; | ||
QList<int> inputLayerTypes() const override; | ||
|
||
protected: | ||
QString outputName() const override; | ||
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
|
||
private: | ||
QgsGeometry convertToLines( const QgsGeometry &geometry ) const; | ||
QList<QgsCurve *> extractRings( const QgsAbstractGeometry *geom ) const; | ||
QgsWkbTypes::Type outWkbType( QgsWkbTypes::Type polygonWkbType ) const; | ||
|
||
friend class TestQgsProcessingAlgs; | ||
}; | ||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMPOLYGONSTOLINES_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