QGIS API Documentation 3.43.0-Master (5250e42f050)
qgsalgorithmconvertgeometrytype.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmconvertgeometrytype.cpp
3 ---------------------
4 begin : March 2025
5 copyright : (C) 2025 by Alexander Bruy
6 email : alexander dot bruy 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
21
22QString QgsConvertGeometryTypeAlgorithm::name() const
23{
24 return QStringLiteral( "convertgeometrytype" );
25}
26
27QString QgsConvertGeometryTypeAlgorithm::displayName() const
28{
29 return QObject::tr( "Convert geometry type" );
30}
31
32QStringList QgsConvertGeometryTypeAlgorithm::tags() const
33{
34 return QObject::tr( "polygon,line,point,centroids,nodes,convert,type,geometry" ).split( ',' );
35}
36
37QString QgsConvertGeometryTypeAlgorithm::group() const
38{
39 return QObject::tr( "Vector geometry" );
40}
41
42QString QgsConvertGeometryTypeAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorgeometry" );
45}
46
47QString QgsConvertGeometryTypeAlgorithm::shortHelpString() const
48{
49 return QObject::tr( "This algorithm generates a new layer based on an existing one, with a different type of geometry.\n\n"
50 "Not all conversions are possible. For instance, a line layer can be converted to a "
51 "point layer, but a point layer cannot be converted to a line layer.\n\n"
52 "See the \"Polygonize\" or \"Lines to polygons\" algorithms for alternative options." );
53}
54
55QString QgsConvertGeometryTypeAlgorithm::shortDescription() const
56{
57 return QObject::tr( "Converts the geometries from a vector layer to a different geometry type." );
58}
59
60QgsConvertGeometryTypeAlgorithm *QgsConvertGeometryTypeAlgorithm::createInstance() const
61{
62 return new QgsConvertGeometryTypeAlgorithm();
63}
64
65void QgsConvertGeometryTypeAlgorithm::initAlgorithm( const QVariantMap & )
66{
67 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorAnyGeometry ) ) );
68
69 QStringList geometryTypes = QStringList() << QObject::tr( "Centroids" )
70 << QObject::tr( "Nodes" )
71 << QObject::tr( "Linestrings" )
72 << QObject::tr( "Multilinestrings" )
73 << QObject::tr( "Polygons" );
74
75 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "TYPE" ), QObject::tr( "New geometry type" ), geometryTypes ) );
76 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Converted" ) ) );
77}
78
79QVariantMap QgsConvertGeometryTypeAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
80{
81 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
82 if ( !source )
83 {
84 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
85 }
86
87 const int typeIndex = parameterAsEnum( parameters, QStringLiteral( "TYPE" ), context );
88 Qgis::WkbType outputWkbType;
89
90 if ( typeIndex == 0 ) // centroids
91 {
92 outputWkbType = Qgis::WkbType::Point;
93 }
94 else if ( typeIndex == 1 ) // nodes
95 {
96 outputWkbType = Qgis::WkbType::MultiPoint;
97 }
98 else if ( typeIndex == 2 ) // LineStrings
99 {
100 outputWkbType = Qgis::WkbType::LineString;
101 }
102 else if ( typeIndex == 3 ) // MultiLineStrings
103 {
104 outputWkbType = Qgis::WkbType::MultiLineString;
105 }
106 else if ( typeIndex == 4 ) // polygons
107 {
108 outputWkbType = Qgis::WkbType::Polygon;
109 }
110
111 // preserve Z/M values
112 if ( QgsWkbTypes::hasM( source->wkbType() ) )
113 {
114 outputWkbType = QgsWkbTypes::addM( outputWkbType );
115 }
116 if ( QgsWkbTypes::hasZ( source->wkbType() ) )
117 {
118 outputWkbType = QgsWkbTypes::addZ( outputWkbType );
119 }
120
121 QString dest;
122 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, source->fields(), outputWkbType, source->sourceCrs() ) );
123 if ( !sink )
124 {
125 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
126 }
127
128 QgsFeatureIterator features = source->getFeatures();
129 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0;
130
131 QgsFeature f;
132 long long i = 0;
133 while ( features.nextFeature( f ) )
134 {
135 if ( feedback->isCanceled() )
136 break;
137
138 if ( !f.hasGeometry() )
139 {
140 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
141 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
142 }
143 else
144 {
145 const QVector< QgsGeometry > geometries = convertGeometry( f.geometry(), typeIndex, outputWkbType );
146 for ( const QgsGeometry &g : geometries )
147 {
148 QgsFeature feat;
149 feat.setGeometry( g );
150 feat.setAttributes( f.attributes() );
151 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
152 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
153 }
154 }
155
156 i++;
157 feedback->setProgress( i * step );
158 }
159
160 sink->finalize();
161
162 QVariantMap results;
163 results.insert( QStringLiteral( "OUTPUT" ), dest );
164 return results;
165}
166
167const QVector< QgsGeometry > QgsConvertGeometryTypeAlgorithm::convertGeometry( const QgsGeometry &geom, const int typeIndex, const Qgis::WkbType outputWkbType )
168{
169 QVector< QgsGeometry > geometries;
170 if ( typeIndex == 0 )
171 {
172 geometries << geom.centroid();
173 }
174 else
175 {
176 geometries = geom.coerceToType( outputWkbType, 0, 0, false );
177 }
178
179 return geometries;
180}
181
@ VectorAnyGeometry
Any vector layer with geometry.
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ LineString
LineString.
@ MultiPoint
MultiPoint.
@ Polygon
Polygon.
@ MultiLineString
MultiLineString.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:69
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
A geometry is the spatial representation of a feature.
QgsGeometry centroid() const
Returns the center of mass of a geometry.
QVector< QgsGeometry > coerceToType(Qgis::WkbType type, double defaultZ=0, double defaultM=0, bool avoidDuplicates=true) const
Attempts to coerce this geometry into the specified destination type.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
static bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.