QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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 );
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 else
111 {
112 throw QgsProcessingException( QObject::tr( "Invalid TYPE parameter value: %1" ).arg( typeIndex ) );
113 }
114
115 // preserve Z/M values
116 if ( QgsWkbTypes::hasM( source->wkbType() ) )
117 {
118 outputWkbType = QgsWkbTypes::addM( outputWkbType );
119 }
120 if ( QgsWkbTypes::hasZ( source->wkbType() ) )
121 {
122 outputWkbType = QgsWkbTypes::addZ( outputWkbType );
123 }
124
125 QString dest;
126 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, source->fields(), outputWkbType, source->sourceCrs() ) );
127 if ( !sink )
128 {
129 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
130 }
131
132 QgsFeatureIterator features = source->getFeatures();
133 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0;
134
135 QgsFeature f;
136 long long i = 0;
137 while ( features.nextFeature( f ) )
138 {
139 if ( feedback->isCanceled() )
140 break;
141
142 if ( !f.hasGeometry() )
143 {
144 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
145 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
146 }
147 else
148 {
149 const QVector< QgsGeometry > geometries = convertGeometry( f.geometry(), typeIndex, outputWkbType );
150 for ( const QgsGeometry &g : geometries )
151 {
152 QgsFeature feat;
153 feat.setGeometry( g );
154 feat.setAttributes( f.attributes() );
155 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
156 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
157 }
158 }
159
160 i++;
161 feedback->setProgress( i * step );
162 }
163
164 sink->finalize();
165
166 QVariantMap results;
167 results.insert( QStringLiteral( "OUTPUT" ), dest );
168 return results;
169}
170
171const QVector< QgsGeometry > QgsConvertGeometryTypeAlgorithm::convertGeometry( const QgsGeometry &geom, const int typeIndex, const Qgis::WkbType outputWkbType )
172{
173 QVector< QgsGeometry > geometries;
174 if ( typeIndex == 0 )
175 {
176 geometries << geom.centroid();
177 }
178 else
179 {
180 geometries = geom.coerceToType( outputWkbType, 0, 0, false );
181 }
182
183 return geometries;
184}
185
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3533
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
@ Point
Point.
Definition qgis.h:279
@ LineString
LineString.
Definition qgis.h:280
@ MultiPoint
MultiPoint.
Definition qgis.h:283
@ Polygon
Polygon.
Definition qgis.h:281
@ MultiLineString
MultiLineString.
Definition qgis.h:284
@ Unknown
Unknown.
Definition qgis.h:278
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 Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.