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 & )
114 QObject::tr(
"Lines length field name" ), QStringLiteral(
"LENGTH" ) ) );
116 QObject::tr(
"Lines count field name" ), QStringLiteral(
"COUNT" ) ) );
121 mLengthFieldName = parameterAsString( parameters, QStringLiteral(
"LEN_FIELD" ), context );
122 mCountFieldName = parameterAsString( parameters, QStringLiteral(
"COUNT_FIELD" ), context );
124 mLinesSource.reset( parameterAsSource( parameters, QStringLiteral(
"LINES" ), context ) );
129 feedback->
reportError( QObject::tr(
"No spatial index exists for lines layer, performance will be severely degraded" ) );
137 QgsFields QgsSumLineLengthAlgorithm::outputFields(
const QgsFields &inputFields )
const
140 mLengthFieldIndex = inputFields.
lookupField( mLengthFieldName );
141 if ( mLengthFieldIndex < 0 )
142 outFields.
append(
QgsField( mLengthFieldName, QVariant::Double ) );
144 mCountFieldIndex = inputFields.
lookupField( mCountFieldName );
145 if ( mCountFieldIndex < 0 )
146 outFields.
append(
QgsField( mCountFieldName, QVariant::Double ) );
158 if ( mLengthFieldIndex < 0 )
161 attrs[mLengthFieldIndex] = 0;
163 if ( mCountFieldIndex < 0 )
166 attrs[mCountFieldIndex] = 0;
169 return QList< QgsFeature > () << outputFeature;
175 engine->prepareGeometry();
193 length += mDa.measureLength( outGeom );
199 if ( mLengthFieldIndex < 0 )
200 attrs.append( length );
202 attrs[mLengthFieldIndex] = length;
204 if ( mCountFieldIndex < 0 )
205 attrs.append( count );
207 attrs[mCountFieldIndex] = count;
210 return QList< QgsFeature >() << outputFeature;