QGIS API Documentation 4.3.0-Master (52aa6503bed)
Loading...
Searching...
No Matches
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
20#include "qgsgeos.h"
21#include "qgsmultipolygon.h"
22#include "qgspolygon.h"
23#include "qgstessellator.h"
24
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
30
31QString QgsTessellateAlgorithm::name() const
32{
33 return u"tessellate"_s;
34}
35
36QString QgsTessellateAlgorithm::displayName() const
37{
38 return QObject::tr( "Tessellate" );
39}
40
41QStringList QgsTessellateAlgorithm::tags() const
42{
43 return QObject::tr( "3d,triangle" ).split( ',' );
44}
45
46QString QgsTessellateAlgorithm::group() const
47{
48 return QObject::tr( "Vector geometry" );
49}
50
51QString QgsTessellateAlgorithm::groupId() const
52{
53 return u"vectorgeometry"_s;
54}
55
56QString QgsTessellateAlgorithm::outputName() const
57{
58 return QObject::tr( "Tessellated" );
59}
60
61Qgis::ProcessingSourceType QgsTessellateAlgorithm::outputLayerType() const
62{
64}
65
66Qgis::WkbType QgsTessellateAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
67{
69}
70
71QString QgsTessellateAlgorithm::shortHelpString() const
72{
73 return QObject::tr( "This algorithm tessellates a polygon geometry layer, dividing the geometries into triangular components." )
74 + u"\n\n"_s
75 + QObject::tr( "The output layer consists of multipolygon geometries for each input feature, with each multipolygon consisting of multiple triangle component polygons." );
76}
77
78QString QgsTessellateAlgorithm::shortDescription() const
79{
80 return QObject::tr( "Tessellates a polygon geometry layer, dividing the geometries into triangular components." );
81}
82
83QList<int> QgsTessellateAlgorithm::inputLayerTypes() const
84{
85 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
86}
87
88QgsTessellateAlgorithm *QgsTessellateAlgorithm::createInstance() const
89{
90 return new QgsTessellateAlgorithm();
91}
92
93QgsFeatureList QgsTessellateAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
94{
95 QgsFeature f = feature;
96 if ( f.hasGeometry() )
97 {
99 f.clearGeometry();
100 else
101 {
102 const QgsGeometry inputGeometry = f.geometry();
103 bool tessellationComplete = false;
104#if GEOS_VERSION_NUM >= GEOS_COMPUTE_VERSION( 3, 11, 0 )
105 if ( !inputGeometry.constGet()->is3D() )
106 {
107 // on supported GEOS versions we prefer to use GEOS GEOSConstrainedDelaunayTriangulation
108 // for 2D triangulation, as it's more stable and tolerant to situations like polygon
109 // holes touching an exterior ring vs the poly2tri based tessellation
110 const QgsGeometry triangulation = inputGeometry.constrainedDelaunayTriangulation();
111 if ( triangulation.isEmpty() && !inputGeometry.isEmpty() )
112 {
113 if ( !triangulation.lastError().isEmpty() )
114 {
115 feedback->reportError( QObject::tr( "Feature ID %1 could not be tessellated: %2" ).arg( f.id() ).arg( triangulation.lastError() ) );
116 }
117 else
118 {
119 feedback->reportError( QObject::tr( "Feature ID %1 could not be tessellated" ).arg( f.id() ) );
120 }
121 }
122 else
123 {
124 f.setGeometry( triangulation );
125 }
126 tessellationComplete = true;
127 }
128#endif
129
130 if ( !tessellationComplete )
131 {
132 // 3D case, or 2D case with unsupported GEOS version -- use less stable poly2tri backend
133 const QgsRectangle bounds = f.geometry().boundingBox();
135 t.setBounds( bounds );
136
137 if ( f.geometry().isMultipart() )
138 {
140 for ( int i = 0; i < ms->numGeometries(); ++i )
141 {
142 const std::unique_ptr<QgsPolygon> p( qgsgeometry_cast<QgsPolygon *>( ms->geometryN( i )->segmentize() ) );
143 t.addPolygon( *p, 0 );
144 }
145 }
146 else
147 {
148 const std::unique_ptr<QgsPolygon> p( qgsgeometry_cast<QgsPolygon *>( f.geometry().constGet()->segmentize() ) );
149 t.addPolygon( *p, 0 );
150 }
152 if ( !g.isEmpty() )
153 {
154 if ( !t.error().isEmpty() )
155 {
156 feedback->reportError( QObject::tr( "Feature ID %1 was only partially tessellated: %2" ).arg( f.id() ).arg( t.error() ) );
157 }
158
159 g.translate( bounds.xMinimum(), bounds.yMinimum() );
160 }
161 else
162 {
163 if ( !t.error().isEmpty() )
164 feedback->reportError( QObject::tr( "Feature ID %1 could not be tessellated: %2" ).arg( f.id() ).arg( t.error() ) );
165 else
166 feedback->reportError( QObject::tr( "Feature ID %1 could not be divided into triangular components." ).arg( f.id() ) );
167 }
168 if ( !inputGeometry.constGet()->is3D() )
169 g.get()->dropZValue();
170
171 f.setGeometry( g );
172 }
173 }
174 }
175 return QgsFeatureList() << f;
176}
177
ProcessingSourceType
Processing data source types.
Definition qgis.h:3769
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3774
@ Polygon
Polygons.
Definition qgis.h:402
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:314
@ MultiPolygon
MultiPolygon.
Definition qgis.h:322
@ MultiPolygonZ
MultiPolygonZ.
Definition qgis.h:339
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:60
QgsFeatureId id
Definition qgsfeature.h:63
QgsGeometry geometry
Definition qgsfeature.h:66
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.
std::unique_ptr< QgsMultiPolygon > asMultiPolygon() const
Returns the triangulation as a multipolygon geometry.
void setBounds(const QgsRectangle &bounds)
Sets scaling and the bounds of the input geometry coordinates.
void addPolygon(const QgsPolygon &polygon, float extrusionHeight)
Tessellates a triangle and adds its vertex entries to the output data array.
QString error() const
Returns a descriptive error string if the tessellation failed.
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 Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QList< QgsFeature > QgsFeatureList