27QString QgsSumLineLengthAlgorithm::name()
const
29 return QStringLiteral(
"sumlinelengths" );
32QString QgsSumLineLengthAlgorithm::displayName()
const
34 return QObject::tr(
"Sum line lengths" );
37QStringList QgsSumLineLengthAlgorithm::tags()
const
39 return QObject::tr(
"line,intersects,intersecting,sum,length,count" ).split(
',' );
42QString QgsSumLineLengthAlgorithm::svgIconPath()
const
47QIcon QgsSumLineLengthAlgorithm::icon()
const
52QString QgsSumLineLengthAlgorithm::group()
const
54 return QObject::tr(
"Vector analysis" );
57QString QgsSumLineLengthAlgorithm::groupId()
const
59 return QStringLiteral(
"vectoranalysis" );
62QString QgsSumLineLengthAlgorithm::shortHelpString()
const
64 return QObject::tr(
"This algorithm takes a polygon layer and a line layer and "
65 "measures the total length of lines and the total number of "
66 "them that cross each polygon.\n\n"
67 "The resulting layer has the same features as the input polygon "
68 "layer, but with two additional attributes containing the length "
69 "and count of the lines across each polygon. The names of these "
70 "two fields can be configured in the algorithm parameters." );
73QString QgsSumLineLengthAlgorithm::shortDescription()
const
75 return QObject::tr(
"Takes a polygon layer and a line layer and "
76 "measures the total length of lines and the total number of "
77 "them that cross each polygon." );
85QgsSumLineLengthAlgorithm *QgsSumLineLengthAlgorithm::createInstance()
const
87 return new QgsSumLineLengthAlgorithm();
90QList<int> QgsSumLineLengthAlgorithm::inputLayerTypes()
const
103 mDa.setSourceCrs( mCrs, mTransformContext );
107QString QgsSumLineLengthAlgorithm::inputParameterName()
const
109 return QStringLiteral(
"POLYGONS" );
112QString QgsSumLineLengthAlgorithm::inputParameterDescription()
const
114 return QObject::tr(
"Polygons" );
117QString QgsSumLineLengthAlgorithm::outputName()
const
119 return QObject::tr(
"Line length" );
122void QgsSumLineLengthAlgorithm::initParameters(
const QVariantMap &configuration )
124 mIsInPlace = configuration.value( QStringLiteral(
"IN_PLACE" ) ).toBool();
134 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"LEN_FIELD" ), QObject::tr(
"Lines length field name" ), QStringLiteral(
"LENGTH" ) ) );
135 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"COUNT_FIELD" ), QObject::tr(
"Lines count field name" ), QStringLiteral(
"COUNT" ) ) );
141 mLengthFieldName = parameterAsString( parameters, QStringLiteral(
"LEN_FIELD" ), context );
142 mCountFieldName = parameterAsString( parameters, QStringLiteral(
"COUNT_FIELD" ), context );
144 mLinesSource.reset( parameterAsSource( parameters, QStringLiteral(
"LINES" ), context ) );
149 feedback->
pushWarning( QObject::tr(
"No spatial index exists for lines layer, performance will be severely degraded" ) );
157QgsFields QgsSumLineLengthAlgorithm::outputFields(
const QgsFields &inputFields )
const
161 mLengthFieldIndex = mLengthFieldName.isEmpty() ? -1 : inputFields.
lookupField( mLengthFieldName );
162 mCountFieldIndex = mCountFieldName.isEmpty() ? -1 : inputFields.
lookupField( mCountFieldName );
168 mLengthFieldIndex = inputFields.
lookupField( mLengthFieldName );
169 if ( mLengthFieldIndex < 0 )
170 outFields.
append(
QgsField( mLengthFieldName, QMetaType::Type::Double ) );
172 mCountFieldIndex = inputFields.
lookupField( mCountFieldName );
173 if ( mCountFieldIndex < 0 )
174 outFields.
append(
QgsField( mCountFieldName, QMetaType::Type::Double ) );
181bool QgsSumLineLengthAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
183 if (
const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
196 if ( !mIsInPlace && mLengthFieldIndex < 0 )
198 else if ( mLengthFieldIndex >= 0 )
199 attrs[mLengthFieldIndex] = 0;
201 if ( !mIsInPlace && mCountFieldIndex < 0 )
203 else if ( mCountFieldIndex >= 0 )
204 attrs[mCountFieldIndex] = 0;
207 return QList<QgsFeature>() << outputFeature;
213 engine->prepareGeometry();
233 length += mDa.measureLength( outGeom );
244 if ( !mIsInPlace && mLengthFieldIndex < 0 )
245 attrs.append( length );
246 else if ( mLengthFieldIndex >= 0 )
247 attrs[mLengthFieldIndex] = length;
249 if ( !mIsInPlace && mCountFieldIndex < 0 )
250 attrs.append( count );
251 else if ( mCountFieldIndex >= 0 )
252 attrs[mCountFieldIndex] = count;
255 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.
@ RespectsEllipsoid
Algorithm respects the context's ellipsoid settings, and uses ellipsoidal based measurements.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
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