QGIS API Documentation 3.43.0-Master (261ee7da134)
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 "qgspolygon.h"
22#include <geos_c.h>
23
25
26QString QgsTessellateAlgorithm::name() const
27{
28 return QStringLiteral( "tessellate" );
29}
30
31QString QgsTessellateAlgorithm::displayName() const
32{
33 return QObject::tr( "Tessellate" );
34}
35
36QStringList QgsTessellateAlgorithm::tags() const
37{
38 return QObject::tr( "3d,triangle" ).split( ',' );
39}
40
41QString QgsTessellateAlgorithm::group() const
42{
43 return QObject::tr( "Vector geometry" );
44}
45
46QString QgsTessellateAlgorithm::groupId() const
47{
48 return QStringLiteral( "vectorgeometry" );
49}
50
51QString QgsTessellateAlgorithm::outputName() const
52{
53 return QObject::tr( "Tessellated" );
54}
55
56Qgis::ProcessingSourceType QgsTessellateAlgorithm::outputLayerType() const
57{
59}
60
61Qgis::WkbType QgsTessellateAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
62{
64}
65
66QString QgsTessellateAlgorithm::shortHelpString() const
67{
68 return QObject::tr( "This algorithm tessellates a polygon geometry layer, dividing the geometries into triangular components." )
69 + QStringLiteral( "\n\n" )
70 + QObject::tr( "The output layer consists of multipolygon geometries for each input feature, with each multipolygon consisting of multiple triangle component polygons." );
71}
72
73QString QgsTessellateAlgorithm::shortDescription() const
74{
75 return QObject::tr( "Tessellates a polygon geometry layer, dividing the geometries into triangular components." );
76}
77
78QList<int> QgsTessellateAlgorithm::inputLayerTypes() const
79{
80 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
81}
82
83QgsTessellateAlgorithm *QgsTessellateAlgorithm::createInstance() const
84{
85 return new QgsTessellateAlgorithm();
86}
87
88QgsFeatureList QgsTessellateAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
89{
90 QgsFeature f = feature;
91 if ( f.hasGeometry() )
92 {
94 f.clearGeometry();
95 else
96 {
97 const QgsGeometry inputGeometry = f.geometry();
98 bool tessellationComplete = false;
99#if ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR >= 11 ) || GEOS_VERSION_MAJOR > 3
100 if ( !inputGeometry.constGet()->is3D() )
101 {
102 // on supported GEOS versions we prefer to use GEOS GEOSConstrainedDelaunayTriangulation
103 // for 2D triangulation, as it's more stable and tolerant to situations like polygon
104 // holes touching an exterior ring vs the poly2tri based tessellation
105 const QgsGeometry triangulation = inputGeometry.constrainedDelaunayTriangulation();
106 if ( triangulation.isEmpty() && !inputGeometry.isEmpty() )
107 {
108 if ( !triangulation.lastError().isEmpty() )
109 {
110 feedback->reportError( QObject::tr( "Feature ID %1 could not be tessellated: %2" ).arg( f.id() ).arg( triangulation.lastError() ) );
111 }
112 else
113 {
114 feedback->reportError( QObject::tr( "Feature ID %1 could not be tessellated" ).arg( f.id() ) );
115 }
116 }
117 else
118 {
119 f.setGeometry( triangulation );
120 }
121 tessellationComplete = true;
122 }
123#endif
124
125 if ( !tessellationComplete )
126 {
127 // 3D case, or 2D case with unsupported GEOS version -- use less stable poly2tri backend
128 const QgsRectangle bounds = f.geometry().boundingBox();
129 QgsTessellator t( bounds, false );
130 t.setOutputZUp( true );
131
132 if ( f.geometry().isMultipart() )
133 {
134 const QgsMultiSurface *ms = qgsgeometry_cast<const QgsMultiSurface *>( f.geometry().constGet() );
135 for ( int i = 0; i < ms->numGeometries(); ++i )
136 {
137 const std::unique_ptr<QgsPolygon> p( qgsgeometry_cast<QgsPolygon *>( ms->geometryN( i )->segmentize() ) );
138 t.addPolygon( *p, 0 );
139 }
140 }
141 else
142 {
143 const std::unique_ptr<QgsPolygon> p( qgsgeometry_cast<QgsPolygon *>( f.geometry().constGet()->segmentize() ) );
144 t.addPolygon( *p, 0 );
145 }
146 QgsGeometry g( t.asMultiPolygon() );
147 if ( !g.isEmpty() )
148 {
149 if ( !t.error().isEmpty() )
150 {
151 feedback->reportError( QObject::tr( "Feature ID %1 was only partially tessellated: %2" ).arg( f.id() ).arg( t.error() ) );
152 }
153
154 g.translate( bounds.xMinimum(), bounds.yMinimum() );
155 }
156 else
157 {
158 if ( !t.error().isEmpty() )
159 feedback->reportError( QObject::tr( "Feature ID %1 could not be tessellated: %2" ).arg( f.id() ).arg( t.error() ) );
160 else
161 feedback->reportError( QObject::tr( "Feature ID %1 could not be divided into triangular components." ).arg( f.id() ) );
162 }
163 if ( !inputGeometry.constGet()->is3D() )
164 g.get()->dropZValue();
165
166 f.setGeometry( g );
167 }
168 }
169 }
170 return QgsFeatureList() << f;
171}
172
ProcessingSourceType
Processing data source types.
Definition qgis.h:3399
@ VectorPolygon
Vector polygon layers.
@ Polygon
Polygons.
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ MultiPolygon
MultiPolygon.
@ MultiPolygonZ
MultiPolygonZ.
virtual QgsAbstractGeometry * segmentize(double tolerance=M_PI/180., SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a version of the geometry without curves.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsFeatureId id
Definition qgsfeature.h:66
QgsGeometry geometry
Definition qgsfeature.h:69
void clearGeometry()
Removes any geometry associated with the feature.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
int numGeometries() const
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.
QString lastError() const
Returns an error string referring to the last error encountered either when this geometry was created...
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
QgsGeometry constrainedDelaunayTriangulation() const
Returns a constrained Delaunay triangulation for the vertices 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.
A rectangle specified with double values.
double xMinimum
double yMinimum
Tessellates polygons into triangles.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
QList< QgsFeature > QgsFeatureList