26QString QgsExportGeometryAttributesAlgorithm::name()
const
28 return QStringLiteral(
"exportaddgeometrycolumns" );
31QString QgsExportGeometryAttributesAlgorithm::displayName()
const
33 return QObject::tr(
"Add geometry attributes" );
36QStringList QgsExportGeometryAttributesAlgorithm::tags()
const
38 return QObject::tr(
"export,add,information,measurements,areas,lengths,perimeters,latitudes,longitudes,x,y,z,extract,points,lines,polygons,sinuosity,fields" ).split(
',' );
41QString QgsExportGeometryAttributesAlgorithm::group()
const
43 return QObject::tr(
"Vector geometry" );
46QString QgsExportGeometryAttributesAlgorithm::groupId()
const
48 return QStringLiteral(
"vectorgeometry" );
51QString QgsExportGeometryAttributesAlgorithm::shortHelpString()
const
53 return QObject::tr(
"This algorithm computes geometric properties of the features in a vector layer. The algorithm generates a new "
54 "vector layer with the same content as the input one, but with additional attributes in its "
55 "attributes table, containing geometric measurements.\n\n"
56 "Depending on the geometry type of the vector layer, the attributes added to the table will "
60QString QgsExportGeometryAttributesAlgorithm::shortDescription()
const
62 return QObject::tr(
"Computes geometric properties of the features in a vector layer." );
70QgsExportGeometryAttributesAlgorithm *QgsExportGeometryAttributesAlgorithm::createInstance()
const
72 return new QgsExportGeometryAttributesAlgorithm();
75void QgsExportGeometryAttributesAlgorithm::initAlgorithm(
const QVariantMap & )
79 const QStringList options = QStringList()
80 << QObject::tr(
"Cartesian Calculations in Layer's CRS" )
81 << QObject::tr(
"Cartesian Calculations in Project's CRS" )
82 << QObject::tr(
"Ellipsoidal Calculations" );
83 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"METHOD" ), QObject::tr(
"Calculate using" ), options,
false, 0 ) );
89 Q_UNUSED( parameters );
97 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
101 const int method = parameterAsEnum( parameters, QStringLiteral(
"METHOD" ), context );
107 bool exportZ =
false;
108 bool exportM =
false;
111 newFields.
append(
QgsField( QStringLiteral(
"area" ), QMetaType::Type::Double ) );
112 newFields.
append(
QgsField( QStringLiteral(
"perimeter" ), QMetaType::Type::Double ) );
116 newFields.
append(
QgsField( QStringLiteral(
"length" ), QMetaType::Type::Double ) );
119 newFields.
append(
QgsField( QStringLiteral(
"straightdis" ), QMetaType::Type::Double ) );
120 newFields.
append(
QgsField( QStringLiteral(
"sinuosity" ), QMetaType::Type::Double ) );
127 newFields.
append(
QgsField( QStringLiteral(
"numparts" ), QMetaType::Type::Int ) );
131 newFields.
append(
QgsField( QStringLiteral(
"xcoord" ), QMetaType::Type::Double ) );
132 newFields.
append(
QgsField( QStringLiteral(
"ycoord" ), QMetaType::Type::Double ) );
135 newFields.
append(
QgsField( QStringLiteral(
"zcoord" ), QMetaType::Type::Double ) );
140 newFields.
append(
QgsField( QStringLiteral(
"mvalue" ), QMetaType::Type::Double ) );
149 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields, wkbType, source->sourceCrs() ) );
165 else if ( method == 1 )
175 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0;
206 attrs << pointAttributes( geom, exportZ, exportM );
210 attrs << polygonAttributes( geom );
214 attrs << lineAttributes( geom );
220 while ( attrs.size() < fields.
size() )
222 attrs.append( QVariant() );
225 outputFeature.setAttributes( attrs );
238 results.insert( QStringLiteral(
"OUTPUT" ), dest );
242QgsAttributes QgsExportGeometryAttributesAlgorithm::pointAttributes(
const QgsGeometry &geom,
const bool exportZ,
const bool exportM )
249 attrs.append( point->x() );
250 attrs.append( point->y() );
254 attrs.append( point->z() );
258 attrs.append( point->m() );
263 attrs.append( collection->numGeometries() );
278 attrs.append( mDistanceConversionFactor * mDa.measureLength( geom ) );
283 const QgsPoint p1 = curve->startPoint();
284 const QgsPoint p2 = curve->endPoint();
285 const double straightDistance = mDistanceConversionFactor * mDa.measureLine(
QgsPointXY( p1 ),
QgsPointXY( p2 ) );
286 const double sinuosity = curve->sinuosity();
287 attrs.append( mDistanceConversionFactor * mDa.measureLength( geom ) );
288 attrs.append( straightDistance );
289 attrs.append( std::isnan( sinuosity ) ? QVariant() : sinuosity );
297 const double area = mAreaConversionFactor * mDa.measureArea( geom );
298 const double perimeter = mDistanceConversionFactor * mDa.measurePerimeter( geom );
@ VectorAnyGeometry
Any vector layer with geometry.
@ RespectsEllipsoid
Algorithm respects the context's ellipsoid settings, and uses ellipsoidal based measurements.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
WkbType
The WKB type describes the number of dimensions a geometry has.
Custom exception class for Coordinate Reference System related exceptions.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
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...
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
int size() const
Returns number of items.
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
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.
Point geometry type, with support for z-dimension and m-values.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Qgis::AreaUnit areaUnit() const
Returns the area unit to use for area calculations.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Qgis::DistanceUnit distanceUnit() const
Returns the distance unit to use for distance calculations.
QString ellipsoid() const
Returns the ellipsoid to use for distance and area calculations.
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 QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
QgsCoordinateReferenceSystem crs
static Q_INVOKABLE double fromUnitToUnitFactor(Qgis::DistanceUnit fromUnit, Qgis::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
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.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
static Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
T qgsgeometry_cast(QgsAbstractGeometry *geom)