QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsalgorithmtessellate.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmtessellate.cpp
3  ---------------------
4  begin : November 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 "qgsalgorithmtessellate.h"
19 #include "qgstessellator.h"
20 #include "qgsmultipolygon.h"
21 #include "qgstriangle.h"
23 
24 QString QgsTessellateAlgorithm::name() const
25 {
26  return QStringLiteral( "tessellate" );
27 }
28 
29 QString QgsTessellateAlgorithm::displayName() const
30 {
31  return QObject::tr( "Tessellate" );
32 }
33 
34 QStringList QgsTessellateAlgorithm::tags() const
35 {
36  return QObject::tr( "3d,triangle" ).split( ',' );
37 }
38 
39 QString QgsTessellateAlgorithm::group() const
40 {
41  return QObject::tr( "Vector geometry" );
42 }
43 
44 QString QgsTessellateAlgorithm::groupId() const
45 {
46  return QStringLiteral( "vectorgeometry" );
47 }
48 
49 QString QgsTessellateAlgorithm::outputName() const
50 {
51  return QObject::tr( "Tessellated" );
52 }
53 
54 QgsProcessing::SourceType QgsTessellateAlgorithm::outputLayerType() const
55 {
57 }
58 
59 QgsWkbTypes::Type QgsTessellateAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
60 {
61  Q_UNUSED( inputWkbType )
63 }
64 
65 QString QgsTessellateAlgorithm::shortHelpString() const
66 {
67  return QObject::tr( "This algorithm tessellates a polygon geometry layer, dividing the geometries into triangular components." )
68  + QStringLiteral( "\n\n" )
69  + QObject::tr( "The output layer consists of multipolygon geometries for each input feature, with each multipolygon consisting of multiple triangle component polygons." );
70 }
71 
72 QList<int> QgsTessellateAlgorithm::inputLayerTypes() const
73 {
74  return QList<int>() << QgsProcessing::TypeVectorPolygon;
75 }
76 
77 QgsTessellateAlgorithm *QgsTessellateAlgorithm::createInstance() const
78 {
79  return new QgsTessellateAlgorithm();
80 }
81 
82 QgsFeatureList QgsTessellateAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
83 {
84  QgsFeature f = feature;
85  if ( f.hasGeometry() )
86  {
88  f.clearGeometry();
89  else
90  {
91  const QgsRectangle bounds = f.geometry().boundingBox();
92  QgsTessellator t( bounds, false );
93 
94  if ( f.geometry().isMultipart() )
95  {
96  const QgsMultiSurface *ms = qgsgeometry_cast< const QgsMultiSurface * >( f.geometry().constGet() );
97  for ( int i = 0; i < ms->numGeometries(); ++i )
98  {
99  const std::unique_ptr< QgsPolygon > p( qgsgeometry_cast< QgsPolygon * >( ms->geometryN( i )->segmentize() ) );
100  t.addPolygon( *p, 0 );
101  }
102  }
103  else
104  {
105  const std::unique_ptr< QgsPolygon > p( qgsgeometry_cast< QgsPolygon * >( f.geometry().constGet()->segmentize() ) );
106  t.addPolygon( *p, 0 );
107  }
108  QgsGeometry g( t.asMultiPolygon() );
109  if ( !g.isEmpty() )
110  {
111  g.translate( bounds.xMinimum(), bounds.yMinimum() );
112  }
113  else
114  {
115  feedback->reportError( QObject::tr( "Feature ID %1 could not be divided into triangular components." ).arg( f.id() ) );
116  }
117  f.setGeometry( g );
118  }
119  }
120  return QgsFeatureList() << f;
121 }
122 
124 
125 
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
qgsalgorithmtessellate.h
QgsProcessingFeedback::reportError
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Definition: qgsprocessingfeedback.cpp:59
QgsProcessing::TypeVectorPolygon
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:51
QgsRectangle::yMinimum
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
QgsMultiSurface
Multi surface geometry collection.
Definition: qgsmultisurface.h:32
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
QgsGeometryCollection::numGeometries
int numGeometries() const SIP_HOLDGIL
Returns the number of geometries within the collection.
Definition: qgsgeometrycollection.h:58
QgsGeometry::isMultipart
bool isMultipart() const SIP_HOLDGIL
Returns true if WKB of the geometry is of WKBMulti* type.
Definition: qgsgeometry.cpp:389
QgsFeature::clearGeometry
void clearGeometry()
Removes any geometry associated with the feature.
Definition: qgsfeature.cpp:184
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
qgsmultipolygon.h
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
qgstriangle.h
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
qgstessellator.h
QgsRectangle::xMinimum
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:136
QgsWkbTypes::MultiPolygonZ
@ MultiPolygonZ
Definition: qgswkbtypes.h:92
QgsAbstractGeometry::segmentize
virtual QgsAbstractGeometry * segmentize(double tolerance=M_PI/180., SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a version of the geometry without curves.
Definition: qgsabstractgeometry.cpp:395
QgsGeometryCollection::geometryN
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
Definition: qgsgeometrycollection.h:86
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsTessellator
Class that takes care of tessellation of polygons into triangles.
Definition: qgstessellator.h:40
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsWkbTypes::geometryType
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:968
QgsGeometry::boundingBox
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Definition: qgsgeometry.cpp:1080
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:45
QgsGeometry::wkbType
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
Definition: qgsgeometry.cpp:357