27 : mSource( parameters.source )
28 , mOutputFile( outputFile )
29 , mOutputFormat( outputFormat )
32 , mRadius( parameters.radius )
33 , mPixelSize( parameters.pixelSize )
34 , mShape( parameters.shape )
35 , mDecay( parameters.decayRatio )
36 , mOutputValues( parameters.outputValues )
53 if ( mRadiusField >= 0 )
54 requiredAttributes << mRadiusField;
56 if ( mWeightField >= 0 )
57 requiredAttributes << mWeightField;
75 mBounds = calculateBounds();
79 int rows = std::max( std::ceil( mBounds.
height() / mPixelSize ) + 1, 1.0 );
80 int cols = std::max( std::ceil( mBounds.
width() / mPixelSize ) + 1, 1.0 );
100 for (
int i = 0; i < rows ; i++ )
106 if ( mRadiusField < 0 )
107 mBufferSize = radiusSizeInPixels( mRadius );
115 if ( featureGeometry.
isNull() )
137 double radius = mRadius;
138 int buffer = mBufferSize;
139 if ( mRadiusField >= 0 )
141 radius = feature.
attribute( mRadiusField ).toDouble();
142 buffer = radiusSizeInPixels( radius );
144 int blockSize = 2 * buffer + 1;
148 if ( mWeightField >= 0 )
150 weight = feature.
attribute( mWeightField ).toDouble();
156 for ( QgsMultiPointXY::const_iterator pointIt = multiPoints.constBegin(); pointIt != multiPoints.constEnd(); ++pointIt )
159 if ( !mBounds.
contains( *pointIt ) )
165 unsigned int xPosition = ( ( ( *pointIt ).x() - mBounds.
xMinimum() ) / mPixelSize ) - buffer;
166 unsigned int yPosition = ( ( ( *pointIt ).y() - mBounds.
yMinimum() ) / mPixelSize ) - buffer;
167 unsigned int yPositionIO = ( ( mBounds.
yMaximum() - ( *pointIt ).y() ) / mPixelSize ) - buffer;
171 QgsRectangle extent( ( *pointIt ).x() - radius, ( *pointIt ).y() - radius, ( *pointIt ).x() + radius, ( *pointIt ).y() + radius );
174 std::unique_ptr< QgsRasterBlock > block( mProvider->
block( 1, extent, blockSize, blockSize ) );
175 QByteArray blockData = block->data();
176 float *dataBuffer = (
float * ) blockData.data();
178 for (
int xp = 0; xp < blockSize; xp++ )
180 for (
int yp = 0; yp < blockSize; yp++ )
182 double pixelCentroidX = ( xPosition + xp + 0.5 ) * mPixelSize + mBounds.
xMinimum();
183 double pixelCentroidY = ( yPosition + yp + 0.5 ) * mPixelSize + mBounds.
yMinimum();
185 double distance = std::sqrt( std::pow( pixelCentroidX - ( *pointIt ).x(), 2.0 ) + std::pow( pixelCentroidY - ( *pointIt ).y(), 2.0 ) );
188 if ( distance > radius )
193 double pixelValue = weight * calculateKernelValue( distance, radius, mShape, mOutputValues );
194 int pos = xp + blockSize * yp;
195 if ( dataBuffer[ pos ] ==
NO_DATA )
197 dataBuffer[ pos ] = 0;
199 dataBuffer[ pos ] += pixelValue;
203 block->setData( blockData );
204 if ( !mProvider->
writeBlock( block.get(), 1, xPosition, yPositionIO ) )
221 int QgsKernelDensityEstimation::radiusSizeInPixels(
double radius )
const 223 int buffer = radius / mPixelSize;
224 if ( radius - ( mPixelSize * buffer ) > 0.5 )
236 return triangularKernel( distance, bandwidth, outputType );
239 return uniformKernel( distance, bandwidth, outputType );
242 return quarticKernel( distance, bandwidth, outputType );
245 return triweightKernel( distance, bandwidth, outputType );
248 return epanechnikovKernel( distance, bandwidth, outputType );
264 switch ( outputType )
269 double k = 2. / ( M_PI * bandwidth );
272 return k * ( 0.5 / bandwidth );
282 switch ( outputType )
287 double k = 116. / ( 5. * M_PI * std::pow( bandwidth, 2 ) );
290 return k * ( 15. / 16. ) * std::pow( 1. - std::pow( distance / bandwidth, 2 ), 2 );
293 return std::pow( 1. - std::pow( distance / bandwidth, 2 ), 2 );
300 switch ( outputType )
305 double k = 128. / ( 35. * M_PI * std::pow( bandwidth, 2 ) );
308 return k * ( 35. / 32. ) * std::pow( 1. - std::pow( distance / bandwidth, 2 ), 3 );
311 return std::pow( 1. - std::pow( distance / bandwidth, 2 ), 3 );
318 switch ( outputType )
323 double k = 8. / ( 3. * M_PI * std::pow( bandwidth, 2 ) );
326 return k * ( 3. / 4. ) * ( 1. - std::pow( distance / bandwidth, 2 ) );
329 return ( 1. - std::pow( distance / bandwidth, 2 ) );
337 switch ( outputType )
346 double k = 3. / ( ( 1. + 2. * mDecay ) * M_PI * std::pow( bandwidth, 2 ) );
349 return k * ( 1. - ( 1. - mDecay ) * ( distance / bandwidth ) );
354 return ( 1. - ( 1. - mDecay ) * ( distance / bandwidth ) );
358 return ( 1. - ( 1. - mDecay ) * ( distance / bandwidth ) );
363 QgsRectangle QgsKernelDensityEstimation::calculateBounds()
const 371 if ( mRadiusField >= 0 )
374 radius = mSource->
maximumValue( mRadiusField ).toDouble();
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QgsRasterDataProvider * createOneBandRaster(Qgis::DataType dataType, int width, int height, const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs)
Create a raster file with one band without initializing the pixel data.
QgsKernelDensityEstimation(const Parameters ¶meters, const QString &outputFile, const QString &outputFormat)
Constructor for QgsKernelDensityEstimation.
void setNoDataValue(double noDataValue)
Sets cell value that will be considered as "no data".
Wrapper for iterator of features from vector data provider or vector layer.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
A rectangle specified with double values.
QString weightField
Field name for weighting field, or empty if not using weights.
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
void setXMaximum(double x)
Set the maximum x value.
A class to represent a 2D point.
void setOutputFormat(const QString &format)
A geometry is the spatial representation of a feature.
virtual bool setNoDataValue(int bandNo, double noDataValue)
Set no data value on created dataset.
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
Thirty two bit floating point (float)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
KernelShape
Kernel shape type.
bool setIsNoData(int row, int column)
Set no data on pixel.
OutputValues
Output values type.
virtual bool setEditable(bool enabled)
Turns on/off editing mode of the provider.
Input parameters were not valid.
double width() const
Returns the width of the rectangle.
void setYMinimum(double y)
Set the minimum y value.
Result finalise()
Finalises the output file.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Output the raw KDE values.
void setOutputProviderKey(const QString &key)
Output mathematically correct scaled values.
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
Operation completed successfully.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
QgsRasterBlock * block(int bandNo, const QgsRectangle &boundingBox, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
QString radiusField
Field for radius, or empty if using a fixed radius.
void setYMaximum(double y)
Set the maximum y value.
Result addFeature(const QgsFeature &feature)
Adds a single feature to the KDE surface.
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Result run()
Runs the KDE calculation across the whole layer at once.
QgsMultiPointXY asMultiPoint() const
Returns the contents of the geometry as a multi-point.
virtual QVariant maximumValue(int fieldIndex) const
Returns the maximum value for an attribute column or an invalid variant in case of error...
QList< int > QgsAttributeList
bool nextFeature(QgsFeature &f)
Result
Result of operation.
Result prepare()
Prepares the output file for writing and setups up the surface calculation.
bool writeBlock(QgsRasterBlock *block, int band, int xOffset=0, int yOffset=0)
Writes pixel data from a raster block into the provider data source.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Error creating output file.
void setXMinimum(double x)
Set the minimum x value.
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
double height() const
Returns the height of the rectangle.
The raster file writer which allows you to save a raster to a new file.