26QString QgsSumLineLengthAlgorithm::name()
const
28 return QStringLiteral(
"sumlinelengths" );
31QString QgsSumLineLengthAlgorithm::displayName()
const
33 return QObject::tr(
"Sum line lengths" );
36QStringList QgsSumLineLengthAlgorithm::tags()
const
38 return QObject::tr(
"line,intersects,intersecting,sum,length,count" ).split(
',' );
41QString QgsSumLineLengthAlgorithm::svgIconPath()
const
46QIcon QgsSumLineLengthAlgorithm::icon()
const
51QString QgsSumLineLengthAlgorithm::group()
const
53 return QObject::tr(
"Vector analysis" );
56QString QgsSumLineLengthAlgorithm::groupId()
const
58 return QStringLiteral(
"vectoranalysis" );
61QString QgsSumLineLengthAlgorithm::shortHelpString()
const
63 return QObject::tr(
"This algorithm takes a polygon layer and a line layer and "
64 "measures the total length of lines and the total number of "
65 "them that cross each polygon.\n\n"
66 "The resulting layer has the same features as the input polygon "
67 "layer, but with two additional attributes containing the length "
68 "and count of the lines across each polygon. The names of these "
69 "two fields can be configured in the algorithm parameters." );
72QString QgsSumLineLengthAlgorithm::shortDescription()
const
74 return QObject::tr(
"Takes a polygon layer and a line layer and "
75 "measures the total length of lines and the total number of "
76 "them that cross each polygon." );
79QgsSumLineLengthAlgorithm *QgsSumLineLengthAlgorithm::createInstance()
const
81 return new QgsSumLineLengthAlgorithm();
84QList<int> QgsSumLineLengthAlgorithm::inputLayerTypes()
const
97 mDa.setSourceCrs( mCrs, mTransformContext );
101QString QgsSumLineLengthAlgorithm::inputParameterName()
const
103 return QStringLiteral(
"POLYGONS" );
106QString QgsSumLineLengthAlgorithm::inputParameterDescription()
const
108 return QObject::tr(
"Polygons" );
111QString QgsSumLineLengthAlgorithm::outputName()
const
113 return QObject::tr(
"Line length" );
116void QgsSumLineLengthAlgorithm::initParameters(
const QVariantMap &configuration )
118 mIsInPlace = configuration.value( QStringLiteral(
"IN_PLACE" ) ).toBool();
128 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"LEN_FIELD" ), QObject::tr(
"Lines length field name" ), QStringLiteral(
"LENGTH" ) ) );
129 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"COUNT_FIELD" ), QObject::tr(
"Lines count field name" ), QStringLiteral(
"COUNT" ) ) );
135 mLengthFieldName = parameterAsString( parameters, QStringLiteral(
"LEN_FIELD" ), context );
136 mCountFieldName = parameterAsString( parameters, QStringLiteral(
"COUNT_FIELD" ), context );
138 mLinesSource.reset( parameterAsSource( parameters, QStringLiteral(
"LINES" ), context ) );
143 feedback->
pushWarning( QObject::tr(
"No spatial index exists for lines layer, performance will be severely degraded" ) );
151QgsFields QgsSumLineLengthAlgorithm::outputFields(
const QgsFields &inputFields )
const
155 mLengthFieldIndex = mLengthFieldName.
isEmpty() ? -1 : inputFields.
lookupField( mLengthFieldName );
156 mCountFieldIndex = mCountFieldName.isEmpty() ? -1 : inputFields.
lookupField( mCountFieldName );
162 mLengthFieldIndex = inputFields.
lookupField( mLengthFieldName );
163 if ( mLengthFieldIndex < 0 )
164 outFields.
append(
QgsField( mLengthFieldName, QMetaType::Type::Double ) );
166 mCountFieldIndex = inputFields.
lookupField( mCountFieldName );
167 if ( mCountFieldIndex < 0 )
168 outFields.
append(
QgsField( mCountFieldName, QMetaType::Type::Double ) );
175bool QgsSumLineLengthAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
177 if (
const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
190 if ( !mIsInPlace && mLengthFieldIndex < 0 )
192 else if ( mLengthFieldIndex >= 0 )
193 attrs[mLengthFieldIndex] = 0;
195 if ( !mIsInPlace && mCountFieldIndex < 0 )
197 else if ( mCountFieldIndex >= 0 )
198 attrs[mCountFieldIndex] = 0;
201 return QList<QgsFeature>() << outputFeature;
207 engine->prepareGeometry();
227 length += mDa.measureLength( outGeom );
238 if ( !mIsInPlace && mLengthFieldIndex < 0 )
239 attrs.append( length );
240 else if ( mLengthFieldIndex >= 0 )
241 attrs[mLengthFieldIndex] = length;
243 if ( !mIsInPlace && mCountFieldIndex < 0 )
244 attrs.append( count );
245 else if ( mCountFieldIndex >= 0 )
246 attrs[mCountFieldIndex] = count;
249 return QList<QgsFeature>() << outputFeature;
ProcessingSourceType
Processing data source types.
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
@ NotPresent
No spatial index exists for the source.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
Represents a coordinate reference system (CRS).
Custom exception class for Coordinate Reference System related exceptions.
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.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
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.
bool isCanceled() const
Tells whether the operation has been canceled already.
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.
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsGeometry intersection(const QgsGeometry &geometry, const QgsGeometryParameters ¶meters=QgsGeometryParameters()) const
Returns a geometry representing the points shared by this geometry and other.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry, double precision=0.0, Qgis::GeosCreationFlags flags=Qgis::GeosCreationFlag::SkipEmptyInteriorRings)
Creates and returns a new geometry engine representing the specified geometry using precision on a gr...
Base class for all map layer types.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
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.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A string parameter for processing algorithms.
Represents a vector layer which manages a vector based dataset.
QList< QgsFeature > QgsFeatureList