Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #8906 from m-kuhn/native_polygons_to_lines
Add native polygonstolines algorithm
  • Loading branch information
m-kuhn committed Jan 21, 2019
2 parents eafedee + 1b7932a commit 8db14d8
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 136 deletions.
130 changes: 0 additions & 130 deletions python/plugins/processing/algs/qgis/PolygonsToLines.py

This file was deleted.

2 changes: 0 additions & 2 deletions python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py
Expand Up @@ -98,7 +98,6 @@
from .PointsToPaths import PointsToPaths
from .PoleOfInaccessibility import PoleOfInaccessibility
from .Polygonize import Polygonize
from .PolygonsToLines import PolygonsToLines
from .PostGISExecuteSQL import PostGISExecuteSQL
from .PostGISExecuteAndLoadSQL import PostGISExecuteAndLoadSQL
from .RandomExtract import RandomExtract
Expand Down Expand Up @@ -210,7 +209,6 @@ def getAlgs(self):
PointsToPaths(),
PoleOfInaccessibility(),
Polygonize(),
PolygonsToLines(),
PostGISExecuteSQL(),
PostGISExecuteAndLoadSQL(),
RandomExtract(),
Expand Down
1 change: 1 addition & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -73,6 +73,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmorientedminimumboundingbox.cpp
processing/qgsalgorithmpackage.cpp
processing/qgsalgorithmarrayoffsetlines.cpp
processing/qgsalgorithmpolygonstolines.cpp
processing/qgsalgorithmpointonsurface.cpp
processing/qgsalgorithmprojectpointcartesian.cpp
processing/qgsalgorithmpromotetomultipart.cpp
Expand Down
157 changes: 157 additions & 0 deletions src/analysis/processing/qgsalgorithmpolygonstolines.cpp
@@ -0,0 +1,157 @@
/***************************************************************************
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" );
}

QgsProcessing::SourceType QgsPolygonsToLinesAlgorithm::outputLayerType() const
{
return QgsProcessing::TypeVectorLine;
}

QgsWkbTypes::Type QgsPolygonsToLinesAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
{
QgsWkbTypes::Type wkbType = QgsWkbTypes::Unknown;

if ( QgsWkbTypes::singleType( QgsWkbTypes::flatType( inputWkbType ) ) == QgsWkbTypes::Polygon )
wkbType = QgsWkbTypes::MultiLineString;
else if ( QgsWkbTypes::singleType( QgsWkbTypes::flatType( inputWkbType ) ) == QgsWkbTypes::CurvePolygon )
wkbType = QgsWkbTypes::MultiCurve;

if ( QgsWkbTypes::hasM( inputWkbType ) )
wkbType = QgsWkbTypes::addM( wkbType );
if ( QgsWkbTypes::hasZ( inputWkbType ) )
wkbType = QgsWkbTypes::addZ( wkbType );

return wkbType;
}

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 * )
{
Q_UNUSED( context )

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 = outputWkbType( 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() );
}

QList<QgsCurve *> QgsPolygonsToLinesAlgorithm::extractRings( const QgsAbstractGeometry *geom ) const
{
QList<QgsCurve *> rings;

if ( QgsGeometryCollection *collection = qgsgeometry_cast<QgsGeometryCollection *>( geom ) )
{
QgsGeometryPartIterator parts = collection->parts();
while ( parts.hasNext() )
rings.append( extractRings( parts.next() ) );
}
else if ( QgsCurvePolygon *polygon = qgsgeometry_cast<QgsCurvePolygon *>( geom ) )
{
if ( auto exteriorRing = polygon->exteriorRing() )
rings.append( exteriorRing->clone() );
for ( int i = 0; i < polygon->numInteriorRings(); ++i )
{
rings.append( polygon->interiorRing( i )->clone() );
}
}

return rings;
}



///@endcond


62 changes: 62 additions & 0 deletions src/analysis/processing/qgsalgorithmpolygonstolines.h
@@ -0,0 +1,62 @@
/***************************************************************************
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;
QgsProcessing::SourceType outputLayerType() const override;
QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) 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;
};

///@endcond PRIVATE

#endif // QGSALGORITHMPOLYGONSTOLINES_H


3 changes: 2 additions & 1 deletion src/analysis/processing/qgsnativealgorithms.cpp
Expand Up @@ -106,7 +106,7 @@
#include "qgsalgorithmvectorize.h"
#include "qgsalgorithmwedgebuffers.h"
#include "qgsalgorithmzonalhistogram.h"

#include "qgsalgorithmpolygonstolines.h"

///@cond PRIVATE

Expand Down Expand Up @@ -245,6 +245,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsVariableWidthBufferByMAlgorithm() );
addAlgorithm( new QgsWedgeBuffersAlgorithm() );
addAlgorithm( new QgsZonalHistogramAlgorithm() );
addAlgorithm( new QgsPolygonsToLinesAlgorithm() );
}


Expand Down

0 comments on commit 8db14d8

Please sign in to comment.