1 /***************************************************************************
2                          qgsalgorithmboundary.cpp
3                          ------------------------
4     begin                : April 2017
5     copyright            : (C) 2017 by Nyall Dawson
6     email                : nyall dot dawson 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 
18 #include "qgsalgorithmboundary.h"
19 #include "qgsvectorlayer.h"
20 
21 ///@cond PRIVATE
22 
name() const23 QString QgsBoundaryAlgorithm::name() const
24 {
25   return QStringLiteral( "boundary" );
26 }
27 
displayName() const28 QString QgsBoundaryAlgorithm::displayName() const
29 {
30   return QObject::tr( "Boundary" );
31 }
32 
tags() const33 QStringList QgsBoundaryAlgorithm::tags() const
34 {
35   return QObject::tr( "boundary,ring,border,exterior" ).split( ',' );
36 }
37 
group() const38 QString QgsBoundaryAlgorithm::group() const
39 {
40   return QObject::tr( "Vector geometry" );
41 }
42 
groupId() const43 QString QgsBoundaryAlgorithm::groupId() const
44 {
45   return  QStringLiteral( "vectorgeometry" );
46 }
47 
outputName() const48 QString QgsBoundaryAlgorithm::outputName() const
49 {
50   return QObject::tr( "Boundary" );
51 }
52 
shortHelpString() const53 QString QgsBoundaryAlgorithm::shortHelpString() const
54 {
55   return QObject::tr( "Returns the closure of the combinatorial boundary of the input geometries (ie the "
56                       "topological boundary of the geometry). For instance, a polygon geometry will have a "
57                       "boundary consisting of the linestrings for each ring in the polygon. Only valid for "
58                       "polygon or line layers." );
59 }
60 
inputLayerTypes() const61 QList<int> QgsBoundaryAlgorithm::inputLayerTypes() const
62 {
63   return QList<int>() << QgsProcessing::TypeVectorLine << QgsProcessing::TypeVectorPolygon;
64 }
65 
supportInPlaceEdit(const QgsMapLayer *) const66 bool QgsBoundaryAlgorithm::supportInPlaceEdit( const QgsMapLayer * ) const
67 {
68   return false;
69 }
70 
createInstance() const71 QgsBoundaryAlgorithm *QgsBoundaryAlgorithm::createInstance() const
72 {
73   return new QgsBoundaryAlgorithm();
74 }
75 
sourceFlags() const76 QgsProcessingFeatureSource::Flag QgsBoundaryAlgorithm::sourceFlags() const
77 {
78   return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
79 }
80 
outputWkbType(QgsWkbTypes::Type inputWkbType) const81 QgsWkbTypes::Type QgsBoundaryAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
82 {
83   QgsWkbTypes::Type outputWkb = QgsWkbTypes::Unknown;
84   switch ( QgsWkbTypes::geometryType( inputWkbType ) )
85   {
86     case QgsWkbTypes::LineGeometry:
87       outputWkb = QgsWkbTypes::MultiPoint;
88       break;
89 
90     case QgsWkbTypes::PolygonGeometry:
91       outputWkb = QgsWkbTypes::MultiLineString;
92       break;
93 
94     case QgsWkbTypes::PointGeometry:
95     case QgsWkbTypes::UnknownGeometry:
96     case QgsWkbTypes::NullGeometry:
97       outputWkb = QgsWkbTypes::NoGeometry;
98       break;
99   }
100 
101   if ( QgsWkbTypes::hasZ( inputWkbType ) )
102     outputWkb = QgsWkbTypes::addZ( outputWkb );
103   if ( QgsWkbTypes::hasM( inputWkbType ) )
104     outputWkb = QgsWkbTypes::addM( outputWkb );
105 
106   return outputWkb;
107 }
108 
processFeature(const QgsFeature & feature,QgsProcessingContext &,QgsProcessingFeedback * feedback)109 QgsFeatureList QgsBoundaryAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
110 {
111   QgsFeature outFeature = feature;
112 
113   if ( feature.hasGeometry() )
114   {
115     const QgsGeometry inputGeometry = feature.geometry();
116     const QgsGeometry outputGeometry = QgsGeometry( inputGeometry.constGet()->boundary() );
117     if ( outputGeometry.isNull() )
118     {
119       feedback->reportError( QObject::tr( "No boundary for feature %1 (possibly a closed linestring?)'" ).arg( feature.id() ) );
120       outFeature.clearGeometry();
121     }
122     else
123     {
124       outFeature.setGeometry( outputGeometry );
125     }
126   }
127   return QgsFeatureList() << outFeature;
128 }
129 
130 ///@endcond
131