25 QString QgsLineDensityAlgorithm::name()
const 27 return QStringLiteral(
"linedensity" );
30 QString QgsLineDensityAlgorithm::displayName()
const 32 return QObject::tr(
"Line density" );
35 QStringList QgsLineDensityAlgorithm::tags()
const 37 return QObject::tr(
"density,kernel,line,line density,interpolation,weight" ).split(
',' );
40 QString QgsLineDensityAlgorithm::group()
const 42 return QObject::tr(
"Interpolation" );
45 QString QgsLineDensityAlgorithm::groupId()
const 47 return QStringLiteral(
"interpolation" );
50 void QgsLineDensityAlgorithm::initAlgorithm(
const QVariantMap & )
54 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"RADIUS" ), QObject::tr(
"Search radius" ), 10, QStringLiteral(
"INPUT" ),
false, 0 ) );
55 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"PIXEL_SIZE" ), QObject::tr(
"Pixel size" ), 10, QStringLiteral(
"INPUT" ),
false ) );
60 QString QgsLineDensityAlgorithm::shortHelpString()
const 62 return QObject::tr(
"The line density interpolation algorithm calculates a density measure of linear features " 63 "which is obtained in a circular neighborhood within each raster cell. " 64 "First, the length of the segment of each line that is intersected by the circular neighborhood " 65 "is multiplied with the lines weight factor. In a second step, all length values are summed and " 66 "divided by the area of the circular neighborhood. This process is repeated for all raster cells." 70 QgsLineDensityAlgorithm *QgsLineDensityAlgorithm::createInstance()
const 72 return new QgsLineDensityAlgorithm();
78 mSource.reset( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
82 mWeightField = parameterAsString( parameters, QStringLiteral(
"WEIGHT" ), context );
84 mPixelSize = parameterAsDouble( parameters, QStringLiteral(
"PIXEL_SIZE" ), context );
86 mSearchRadius = parameterAsDouble( parameters, QStringLiteral(
"RADIUS" ), context );
87 if ( mSearchRadius < 0.5 * mPixelSize * std::sqrt( 2 ) )
88 throw QgsProcessingException( QObject::tr(
"Raster cells must be fully contained by the search circle. Therefore, " 89 "the search radius must not be smaller than half of the pixel diagonal." ) );
91 mExtent = mSource->sourceExtent();
92 mCrs = mSource->sourceCrs();
103 QgsPoint firstCellMidpoint =
QgsPoint( mExtent.xMinimum() + ( mPixelSize / 2 ), mExtent.yMaximum() - ( mPixelSize / 2 ) );
114 QStringList weightName = QStringList( mWeightField );
115 QgsFields attrFields = mSource->fields();
127 if ( !mWeightField.isEmpty() )
129 double analysisWeight = f.
attribute( mWeightField ).toDouble();
130 mFeatureWeights.insert( f.
id(), analysisWeight );
134 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
135 QFileInfo fi( outputFile );
138 int rows = std::max( std::ceil( mExtent.height() / mPixelSize ), 1.0 );
139 int cols = std::max( std::ceil( mExtent.width() / mPixelSize ), 1.0 );
143 QgsRectangle rasterExtent =
QgsRectangle( mExtent.xMinimum(), mExtent.yMaximum() - ( rows * mPixelSize ), mExtent.xMinimum() + ( cols * mPixelSize ), mExtent.yMaximum() );
146 writer.setOutputProviderKey( QStringLiteral(
"gdal" ) );
147 writer.setOutputFormat( outputFormat );
148 std::unique_ptr<QgsRasterDataProvider > provider( writer.createOneBandRaster(
Qgis::Float32, cols, rows, rasterExtent, mCrs ) );
151 if ( !provider->isValid() )
154 provider->setNoDataValue( 1, -9999 );
159 std::unique_ptr< QgsRasterBlock > rasterDataLine = qgis::make_unique< QgsRasterBlock >(
Qgis::Float32, cols, 1 );
161 for (
int row = 0; row < rows; row++ )
163 for (
int col = 0; col < cols; col++ )
171 mSearchGeometry.translate( mPixelSize, 0 );
173 const QList<QgsFeatureId> fids = mIndex.intersects( mSearchGeometry.boundingBox() );
175 if ( !fids.isEmpty() )
178 engine->prepareGeometry();
180 double absDensity = 0;
185 if ( engine->intersects( lineGeom.
constGet() ) )
187 double analysisLineLength = mDa.measureLength(
QgsGeometry( engine->intersection( mIndex.geometry(
id ).constGet() ) ) );
190 if ( !mWeightField.isEmpty() )
192 weight = mFeatureWeights.value(
id );
195 absDensity += ( analysisLineLength * weight );
199 double lineDensity = 0;
200 if ( absDensity > 0 )
203 double analysisSearchGeometryArea = mDa.measureArea( mSearchGeometry );
204 lineDensity = absDensity / analysisSearchGeometryArea;
206 rasterDataLine->setValue( 0, col, lineDensity );
211 rasterDataLine->setValue( 0, col, 0.0 );
214 feedback->
setProgress( static_cast<double>( cellcnt ) / static_cast<double>( totalCellcnt ) * 100 );
217 provider->writeBlock( rasterDataLine.get(), 1, 0, row );
220 mSearchGeometry.translate( ( cols - 1 ) * -mPixelSize, -mPixelSize );
224 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
Wrapper for iterator of features from vector data provider or vector layer.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
A rectangle specified with double values.
Base class for providing feedback from a processing algorithm.
A vector layer or feature source field parameter for processing algorithms.
void setProgress(double progress)
Sets the current progress for the feedback object.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
Thirty two bit floating point (float)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QgsProject * project() const
Returns the project in which the algorithm is being executed.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
virtual QgsPolygon * toPolygon(unsigned int segments=36) const
Returns a segmented polygon.
A double numeric parameter for distance values.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Point geometry type, with support for z-dimension and m-values.
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry)
Creates and returns a new geometry engine.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
A spatial index for QgsFeature objects.
bool isCanceled() const
Tells whether the operation has been canceled already.
An input feature source (such as vector layers) parameter for processing algorithms.
bool nextFeature(QgsFeature &f)
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Contains information about the context in which a processing algorithm is executed.
Indicates that the spatial index should also store feature geometries. This requires more memory...
The raster file writer which allows you to save a raster to a new file.