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