22using namespace Qt::StringLiterals;
26QString QgsConvertGeometryTypeAlgorithm::name()
const
28 return u
"convertgeometrytype"_s;
31QString QgsConvertGeometryTypeAlgorithm::displayName()
const
33 return QObject::tr(
"Convert geometry type" );
36QStringList QgsConvertGeometryTypeAlgorithm::tags()
const
38 return QObject::tr(
"polygon,line,point,centroids,nodes,convert,type,geometry" ).split(
',' );
41QString QgsConvertGeometryTypeAlgorithm::group()
const
43 return QObject::tr(
"Vector geometry" );
46QString QgsConvertGeometryTypeAlgorithm::groupId()
const
48 return u
"vectorgeometry"_s;
51QString QgsConvertGeometryTypeAlgorithm::shortHelpString()
const
53 return QObject::tr(
"This algorithm generates a new layer based on an existing one, with a different type of geometry.\n\n"
54 "Not all conversions are possible. For instance, a line layer can be converted to a "
55 "point layer, but a point layer cannot be converted to a line layer.\n\n"
56 "See the \"Polygonize\" or \"Lines to polygons\" algorithms for alternative options." );
59QString QgsConvertGeometryTypeAlgorithm::shortDescription()
const
61 return QObject::tr(
"Converts the geometries from a vector layer to a different geometry type." );
64QgsConvertGeometryTypeAlgorithm *QgsConvertGeometryTypeAlgorithm::createInstance()
const
66 return new QgsConvertGeometryTypeAlgorithm();
69void QgsConvertGeometryTypeAlgorithm::initAlgorithm(
const QVariantMap & )
73 QStringList geometryTypes = QStringList() << QObject::tr(
"Centroids" )
74 << QObject::tr(
"Nodes" )
75 << QObject::tr(
"Linestrings" )
76 << QObject::tr(
"Multilinestrings" )
77 << QObject::tr(
"Polygons" );
85 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
91 const int typeIndex = parameterAsEnum( parameters, u
"TYPE"_s, context );
98 else if ( typeIndex == 1 )
102 else if ( typeIndex == 2 )
106 else if ( typeIndex == 3 )
110 else if ( typeIndex == 4 )
130 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, source->fields(), outputWkbType, source->sourceCrs() ) );
137 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0;
153 const QVector< QgsGeometry > geometries = convertGeometry( f.
geometry(), typeIndex, outputWkbType );
171 results.insert( u
"OUTPUT"_s, dest );
175const QVector< QgsGeometry > QgsConvertGeometryTypeAlgorithm::convertGeometry(
const QgsGeometry &geom,
const int typeIndex,
const Qgis::WkbType outputWkbType )
177 QVector< QgsGeometry > geometries;
178 if ( typeIndex == 0 )
184 geometries = geom.
coerceToType( outputWkbType, 0, 0,
false );
@ VectorAnyGeometry
Any vector layer with geometry.
WkbType
The WKB type describes the number of dimensions a geometry has.
@ 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...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
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.
void setProgress(double progress)
Sets the current progress for the feedback object.
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.