|
| 1 | +/*************************************************************************** |
| 2 | + qgsalgorithmpolygonstolines.cpp |
| 3 | + --------------------- |
| 4 | + begin : January 2019 |
| 5 | + copyright : (C) 2019 by Matthias Kuhn |
| 6 | + email : matthias@opengis.ch |
| 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 | + |
| 18 | +#include "qgsalgorithmpolygonstolines.h" |
| 19 | +#include "qgsgeometrycollection.h" |
| 20 | +#include "qgscurvepolygon.h" |
| 21 | +#include "qgscurve.h" |
| 22 | +#include "qgsmultilinestring.h" |
| 23 | + |
| 24 | +///@cond PRIVATE |
| 25 | + |
| 26 | +QString QgsPolygonsToLinesAlgorithm::name() const |
| 27 | +{ |
| 28 | + return QStringLiteral( "polygonstolines" ); |
| 29 | +} |
| 30 | + |
| 31 | +QString QgsPolygonsToLinesAlgorithm::displayName() const |
| 32 | +{ |
| 33 | + return QObject::tr( "Polygons to lines" ); |
| 34 | +} |
| 35 | + |
| 36 | +QStringList QgsPolygonsToLinesAlgorithm::tags() const |
| 37 | +{ |
| 38 | + return QObject::tr( "line,polygon,convert" ).split( ',' ); |
| 39 | +} |
| 40 | + |
| 41 | +QString QgsPolygonsToLinesAlgorithm::group() const |
| 42 | +{ |
| 43 | + return QObject::tr( "Vector creation" ); |
| 44 | +} |
| 45 | + |
| 46 | +QString QgsPolygonsToLinesAlgorithm::groupId() const |
| 47 | +{ |
| 48 | + return QStringLiteral( "vectorgeometry" ); |
| 49 | +} |
| 50 | + |
| 51 | +QString QgsPolygonsToLinesAlgorithm::outputName() const |
| 52 | +{ |
| 53 | + return QObject::tr( "Lines" ); |
| 54 | +} |
| 55 | + |
| 56 | +QString QgsPolygonsToLinesAlgorithm::shortHelpString() const |
| 57 | +{ |
| 58 | + return QObject::tr( "Converts polygons to lines" ); |
| 59 | +} |
| 60 | + |
| 61 | +QString QgsPolygonsToLinesAlgorithm::shortDescription() const |
| 62 | +{ |
| 63 | + return QObject::tr( "Converts polygons to lines." ); |
| 64 | +} |
| 65 | + |
| 66 | +QgsPolygonsToLinesAlgorithm *QgsPolygonsToLinesAlgorithm::createInstance() const |
| 67 | +{ |
| 68 | + return new QgsPolygonsToLinesAlgorithm(); |
| 69 | +} |
| 70 | + |
| 71 | +QList<int> QgsPolygonsToLinesAlgorithm::inputLayerTypes() const |
| 72 | +{ |
| 73 | + return QList< int >() << QgsProcessing::TypeVectorPolygon; |
| 74 | +} |
| 75 | + |
| 76 | +QgsFeatureList QgsPolygonsToLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * ) |
| 77 | +{ |
| 78 | + QgsFeatureList result; |
| 79 | + QgsFeature feat = feature; |
| 80 | + if ( feat.hasGeometry() ) |
| 81 | + feat.setGeometry( convertToLines( feat.geometry() ) ); |
| 82 | + |
| 83 | + result << feat; |
| 84 | + return result; |
| 85 | +} |
| 86 | + |
| 87 | +QgsGeometry QgsPolygonsToLinesAlgorithm::convertToLines( const QgsGeometry &geometry ) const |
| 88 | +{ |
| 89 | + auto rings = extractRings( geometry.constGet() ); |
| 90 | + |
| 91 | + QgsWkbTypes::Type resultType = outWkbType( geometry.wkbType() ); |
| 92 | + |
| 93 | + std::unique_ptr<QgsMultiCurve> lineGeometry; |
| 94 | + |
| 95 | + if ( QgsWkbTypes::flatType( resultType ) == QgsWkbTypes::MultiLineString ) |
| 96 | + lineGeometry = qgis::make_unique<QgsMultiLineString>(); |
| 97 | + else |
| 98 | + lineGeometry = qgis::make_unique<QgsMultiCurve>(); |
| 99 | + |
| 100 | + for ( auto ring : qgis::as_const( rings ) ) |
| 101 | + lineGeometry->addGeometry( ring ); |
| 102 | + |
| 103 | + return QgsGeometry( lineGeometry.release() ); |
| 104 | +} |
| 105 | + |
| 106 | +QgsWkbTypes::Type QgsPolygonsToLinesAlgorithm::outWkbType( QgsWkbTypes::Type polygonWkbType ) const |
| 107 | +{ |
| 108 | + QgsWkbTypes::Type wkbType = QgsWkbTypes::NoGeometry; |
| 109 | + |
| 110 | + if ( QgsWkbTypes::singleType( QgsWkbTypes::flatType( polygonWkbType ) ) == QgsWkbTypes::Polygon ) |
| 111 | + wkbType = QgsWkbTypes::MultiLineString; |
| 112 | + else if ( QgsWkbTypes::singleType( QgsWkbTypes::flatType( polygonWkbType ) ) == QgsWkbTypes::CurvePolygon ) |
| 113 | + wkbType = QgsWkbTypes::MultiCurve; |
| 114 | + |
| 115 | + if ( QgsWkbTypes::hasM( polygonWkbType ) ) |
| 116 | + wkbType = QgsWkbTypes::addM( wkbType ); |
| 117 | + if ( QgsWkbTypes::hasZ( polygonWkbType ) ) |
| 118 | + wkbType = QgsWkbTypes::addZ( wkbType ); |
| 119 | + |
| 120 | + return wkbType; |
| 121 | +} |
| 122 | + |
| 123 | +QList<QgsCurve *> QgsPolygonsToLinesAlgorithm::extractRings( const QgsAbstractGeometry *geom ) const |
| 124 | +{ |
| 125 | + QList<QgsCurve *> rings; |
| 126 | + |
| 127 | + if ( QgsGeometryCollection *collection = qgsgeometry_cast<QgsGeometryCollection *>( geom ) ) |
| 128 | + { |
| 129 | + for ( int i = 0; i < collection->numGeometries(); ++i ) |
| 130 | + { |
| 131 | + rings.append( extractRings( collection->geometryN( i ) ) ); |
| 132 | + } |
| 133 | + } |
| 134 | + else if ( QgsCurvePolygon *polygon = qgsgeometry_cast<QgsCurvePolygon *>( geom ) ) |
| 135 | + { |
| 136 | + rings.append( polygon->exteriorRing()->clone() ); |
| 137 | + for ( int i = 0; i < polygon->numInteriorRings(); ++i ) |
| 138 | + { |
| 139 | + rings.append( polygon->interiorRing( i )->clone() ); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return rings; |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +///@endcond |
| 149 | + |
| 150 | + |
0 commit comments