QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
19#include "qgstessellator.h"
20#include "qgsmultipolygon.h"
21#include "qgstriangle.h"
23
24QString QgsTessellateAlgorithm::name() const
25{
26 return QStringLiteral( "tessellate" );
27}
28
29QString QgsTessellateAlgorithm::displayName() const
30{
31 return QObject::tr( "Tessellate" );
32}
33
34QStringList QgsTessellateAlgorithm::tags() const
35{
36 return QObject::tr( "3d,triangle" ).split( ',' );
37}
38
39QString QgsTessellateAlgorithm::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsTessellateAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsTessellateAlgorithm::outputName() const
50{
51 return QObject::tr( "Tessellated" );
52}
53
54QgsProcessing::SourceType QgsTessellateAlgorithm::outputLayerType() const
55{
57}
58
59Qgis::WkbType QgsTessellateAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
60{
61 Q_UNUSED( inputWkbType )
63}
64
65QString 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
72QList<int> QgsTessellateAlgorithm::inputLayerTypes() const
73{
74 return QList<int>() << QgsProcessing::TypeVectorPolygon;
75}
76
77QgsTessellateAlgorithm *QgsTessellateAlgorithm::createInstance() const
78{
79 return new QgsTessellateAlgorithm();
80}
81
82QgsFeatureList QgsTessellateAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
83{
84 QgsFeature f = feature;
85 if ( f.hasGeometry() )
86 {
87 if ( QgsWkbTypes::geometryType( f.geometry().wkbType() ) != Qgis::GeometryType::Polygon )
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
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition: qgis.h:155
@ MultiPolygonZ
MultiPolygonZ.
virtual QgsAbstractGeometry * segmentize(double tolerance=M_PI/180., SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a version of the geometry without curves.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsGeometry geometry
Definition: qgsfeature.h:67
void clearGeometry()
Removes any geometry associated with the feature.
Definition: qgsfeature.cpp:184
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:233
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
Q_GADGET QgsFeatureId id
Definition: qgsfeature.h:64
int numGeometries() const SIP_HOLDGIL
Returns the number of geometries within the collection.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Qgis::WkbType wkbType() const SIP_HOLDGIL
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
bool isMultipart() const SIP_HOLDGIL
Returns true if WKB of the geometry is of WKBMulti* type.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Multi surface geometry collection.
Contains information about the context in which a processing algorithm is executed.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
SourceType
Data source types enum.
Definition: qgsprocessing.h:47
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:52
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198
Class that takes care of tessellation of polygons into triangles.
static Qgis::GeometryType geometryType(Qgis::WkbType type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:865
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:920