|
43 | 43 |
|
44 | 44 | #define NO_DATA -9999
|
45 | 45 |
|
| 46 | +#ifndef M_PI |
| 47 | +#define M_PI 3.14159265358979323846 |
| 48 | +#endif |
46 | 49 |
|
47 | 50 | static const QString sName = QObject::tr( "Heatmap" );
|
48 | 51 | static const QString sDescription = QObject::tr( "Creates a Heatmap raster for the input point vector" );
|
@@ -109,7 +112,8 @@ void Heatmap::run()
|
109 | 112 | int rows = d.rows();
|
110 | 113 | float cellsize = d.cellSizeX(); // or d.cellSizeY(); both have the same value
|
111 | 114 | float myDecay = d.decayRatio();
|
112 |
| - |
| 115 | + int kernelShape = d.kernelShape(); |
| 116 | + |
113 | 117 | // Start working on the input vector
|
114 | 118 | QgsVectorLayer* inputLayer = d.inputVectorLayer();
|
115 | 119 |
|
@@ -259,7 +263,7 @@ void Heatmap::run()
|
259 | 263 | continue;
|
260 | 264 | }
|
261 | 265 |
|
262 |
| - float pixelValue = weight * ( 1 - (( 1 - myDecay ) * distance / myBuffer ) ); |
| 266 | + float pixelValue = weight * calculateKernelValue( distance, myBuffer, kernelShape ); |
263 | 267 |
|
264 | 268 | // clearing anamolies along the axes
|
265 | 269 | if ( xp == 0 && yp == 0 )
|
@@ -330,6 +334,40 @@ int Heatmap::bufferSize( float radius, float cellsize )
|
330 | 334 | return buffer;
|
331 | 335 | }
|
332 | 336 |
|
| 337 | +float Heatmap::calculateKernelValue( float distance, int bandwidth, int kernelShape ) |
| 338 | +{ |
| 339 | + switch (kernelShape) { |
| 340 | + case HeatmapGui::Triangular: |
| 341 | + return ( 1 - ( distance / bandwidth ) ); |
| 342 | + |
| 343 | + case HeatmapGui::Uniform: |
| 344 | + return uniformKernel( distance, bandwidth ); |
| 345 | + |
| 346 | + case HeatmapGui::Quartic: |
| 347 | + return quarticKernel( distance, bandwidth ); |
| 348 | + } |
| 349 | + return 0; |
| 350 | + |
| 351 | +} |
| 352 | + |
| 353 | +float Heatmap::uniformKernel( float distance, int bandwidth ) |
| 354 | +{ |
| 355 | + // Normalizing constant. Calculated by polar double integrating the kernel function |
| 356 | + // with radius of 0 to bandwidth and equating area to 1. |
| 357 | + float k = 2. / (M_PI * (float)bandwidth); |
| 358 | + |
| 359 | + // Derived from Wand and Jones (1995), p. 175 |
| 360 | + return k * ( 0.5 / (float)bandwidth); |
| 361 | +} |
| 362 | + |
| 363 | +float Heatmap::quarticKernel( float distance, int bandwidth ) |
| 364 | +{ |
| 365 | + // Normalizing constant |
| 366 | + float k = 16. / (5. * M_PI * pow((float)bandwidth, 2)); |
| 367 | + |
| 368 | + // Derived from Wand and Jones (1995), p. 175 |
| 369 | + return k * (15. / 16. ) * pow( 1. - pow( distance / (float)bandwidth, 2), 2); |
| 370 | +} |
333 | 371 |
|
334 | 372 | // Unload the plugin by cleaning up the GUI
|
335 | 373 | void Heatmap::unload()
|
|
0 commit comments