26 QString QgsSumLineLengthAlgorithm::name()
const
28 return QStringLiteral(
"sumlinelengths" );
31 QString QgsSumLineLengthAlgorithm::displayName()
const
33 return QObject::tr(
"Sum line lengths" );
36 QStringList QgsSumLineLengthAlgorithm::tags()
const
38 return QObject::tr(
"line,intersects,intersecting,sum,length,count" ).split(
',' );
41 QString QgsSumLineLengthAlgorithm::svgIconPath()
const
46 QIcon QgsSumLineLengthAlgorithm::icon()
const
51 QString QgsSumLineLengthAlgorithm::group()
const
53 return QObject::tr(
"Vector analysis" );
56 QString QgsSumLineLengthAlgorithm::groupId()
const
58 return QStringLiteral(
"vectoranalysis" );
61 QString 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." );
72 QgsSumLineLengthAlgorithm *QgsSumLineLengthAlgorithm::createInstance()
const
74 return new QgsSumLineLengthAlgorithm();
77 QList<int> QgsSumLineLengthAlgorithm::inputLayerTypes()
const
90 mDa.setSourceCrs( mCrs, mTransformContext );
94 QString QgsSumLineLengthAlgorithm::inputParameterName()
const
96 return QStringLiteral(
"POLYGONS" );
99 QString QgsSumLineLengthAlgorithm::inputParameterDescription()
const
101 return QObject::tr(
"Polygons" );
104 QString QgsSumLineLengthAlgorithm::outputName()
const
106 return QObject::tr(
"Line length" );
109 void QgsSumLineLengthAlgorithm::initParameters(
const QVariantMap &configuration )
111 mIsInPlace = configuration.value( QStringLiteral(
"IN_PLACE" ) ).toBool();
125 QObject::tr(
"Lines length field name" ), QStringLiteral(
"LENGTH" ) ) );
127 QObject::tr(
"Lines count field name" ), QStringLiteral(
"COUNT" ) ) );
133 mLengthFieldName = parameterAsString( parameters, QStringLiteral(
"LEN_FIELD" ), context );
134 mCountFieldName = parameterAsString( parameters, QStringLiteral(
"COUNT_FIELD" ), context );
136 mLinesSource.reset( parameterAsSource( parameters, QStringLiteral(
"LINES" ), context ) );
141 feedback->
pushWarning( QObject::tr(
"No spatial index exists for lines layer, performance will be severely degraded" ) );
149 QgsFields QgsSumLineLengthAlgorithm::outputFields(
const QgsFields &inputFields )
const
153 mLengthFieldIndex = mLengthFieldName.
isEmpty() ? -1 : inputFields.
lookupField( mLengthFieldName );
154 mCountFieldIndex = mCountFieldName.isEmpty() ? -1 : inputFields.
lookupField( mCountFieldName );
160 mLengthFieldIndex = inputFields.
lookupField( mLengthFieldName );
161 if ( mLengthFieldIndex < 0 )
162 outFields.
append(
QgsField( mLengthFieldName, QVariant::Double ) );
164 mCountFieldIndex = inputFields.
lookupField( mCountFieldName );
165 if ( mCountFieldIndex < 0 )
166 outFields.
append(
QgsField( mCountFieldName, QVariant::Double ) );
173 bool QgsSumLineLengthAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
175 if (
const QgsVectorLayer *vl = qobject_cast< const QgsVectorLayer * >( layer ) )
188 if ( !mIsInPlace && mLengthFieldIndex < 0 )
190 else if ( mLengthFieldIndex >= 0 )
191 attrs[mLengthFieldIndex] = 0;
193 if ( !mIsInPlace && mCountFieldIndex < 0 )
195 else if ( mCountFieldIndex >= 0 )
196 attrs[mCountFieldIndex] = 0;
199 return QList< QgsFeature > () << outputFeature;
205 engine->prepareGeometry();
223 length += mDa.measureLength( outGeom );
229 if ( !mIsInPlace && mLengthFieldIndex < 0 )
230 attrs.append( length );
231 else if ( mLengthFieldIndex >= 0 )
232 attrs[mLengthFieldIndex] = length;
234 if ( !mIsInPlace && mCountFieldIndex < 0 )
235 attrs.append( count );
236 else if ( mCountFieldIndex >= 0 )
237 attrs[mCountFieldIndex] = count;
240 return QList< QgsFeature >() << outputFeature;
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.
This class represents a coordinate reference system (CRS).
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class 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.
@ SpatialIndexNotPresent
No spatial index exists for the source.
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 SIP_HOLDGIL
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, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
bool isEmpty() const
Checks whether the container is empty.
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 SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsGeometry intersection(const QgsGeometry &geometry) const
Returns a geometry representing the points shared by this geometry and other.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry)
Creates and returns a new geometry engine representing the specified geometry.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
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.
SourceType
Data source types enum.
@ TypeVectorLine
Vector line layers.
@ TypeVectorPolygon
Vector polygon layers.
Represents a vector layer which manages a vector based data sets.
QList< QgsFeature > QgsFeatureList
const QgsCoordinateReferenceSystem & outputCrs