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;