QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsalgorithmboundary.cpp
Go to the documentation of this file.
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 
21 
22 QString QgsBoundaryAlgorithm::name() const
23 {
24  return QStringLiteral( "boundary" );
25 }
26 
27 QString QgsBoundaryAlgorithm::displayName() const
28 {
29  return QObject::tr( "Boundary" );
30 }
31 
32 QStringList QgsBoundaryAlgorithm::tags() const
33 {
34  return QObject::tr( "boundary,ring,border,exterior" ).split( ',' );
35 }
36 
37 QString QgsBoundaryAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsBoundaryAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsBoundaryAlgorithm::outputName() const
48 {
49  return QObject::tr( "Boundary" );
50 }
51 
52 QString QgsBoundaryAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "Returns the closure of the combinatorial boundary of the input geometries (ie the "
55  "topological boundary of the geometry). For instance, a polygon geometry will have a "
56  "boundary consisting of the linestrings for each ring in the polygon. Only valid for "
57  "polygon or line layers." );
58 }
59 
60 QList<int> QgsBoundaryAlgorithm::inputLayerTypes() const
61 {
63 }
64 
65 QgsBoundaryAlgorithm *QgsBoundaryAlgorithm::createInstance() const
66 {
67  return new QgsBoundaryAlgorithm();
68 }
69 
70 QgsWkbTypes::Type QgsBoundaryAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
71 {
73  switch ( QgsWkbTypes::geometryType( inputWkbType ) )
74  {
76  outputWkb = QgsWkbTypes::MultiPoint;
77  break;
78 
80  outputWkb = QgsWkbTypes::MultiLineString;
81  break;
82 
86  outputWkb = QgsWkbTypes::NoGeometry;
87  break;
88  }
89 
90  if ( QgsWkbTypes::hasZ( inputWkbType ) )
91  outputWkb = QgsWkbTypes::addZ( outputWkb );
92  if ( QgsWkbTypes::hasM( inputWkbType ) )
93  outputWkb = QgsWkbTypes::addM( outputWkb );
94 
95  return outputWkb;
96 }
97 
98 QgsFeatureList QgsBoundaryAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
99 {
100  QgsFeature outFeature = feature;
101 
102  if ( feature.hasGeometry() )
103  {
104  QgsGeometry inputGeometry = feature.geometry();
105  QgsGeometry outputGeometry = QgsGeometry( inputGeometry.constGet()->boundary() );
106  if ( !outputGeometry )
107  {
108  feedback->reportError( QObject::tr( "No boundary for feature %1 (possibly a closed linestring?)'" ).arg( feature.id() ) );
109  outFeature.clearGeometry();
110  }
111  else
112  {
113  outFeature.setGeometry( outputGeometry );
114  }
115  }
116  return QgsFeatureList() << outFeature;
117 }
118 
QgsFeatureId id
Definition: qgsfeature.h:71
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
virtual QgsAbstractGeometry * boundary() const =0
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:768
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:889
static GeometryType geometryType(Type type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:663
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:864
Vector polygon layers.
Definition: qgsprocessing.h:50
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
void clearGeometry()
Removes any geometry associated with the feature.
Definition: qgsfeature.cpp:144
Vector line layers.
Definition: qgsprocessing.h:49
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:818
Contains information about the context in which a processing algorithm is executed.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.