Skip to content

Commit

Permalink
Use double rather than float when appropriate to improve precision of…
Browse files Browse the repository at this point in the history
… heatmaps
  • Loading branch information
nyalldawson committed Mar 13, 2013
1 parent 3b8cbc2 commit 239479a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
22 changes: 11 additions & 11 deletions src/plugins/heatmap/heatmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ void Heatmap::run()
QgsRectangle myBBox = d.bbox();
int columns = d.columns();
int rows = d.rows();
float cellsize = d.cellSizeX(); // or d.cellSizeY(); both have the same value
float myDecay = d.decayRatio();
double cellsize = d.cellSizeX(); // or d.cellSizeY(); both have the same value
double myDecay = d.decayRatio();

// Start working on the input vector
QgsVectorLayer* inputLayer = d.inputVectorLayer();
Expand Down Expand Up @@ -164,8 +164,8 @@ void Heatmap::run()
int wField = 0;

// Handle different radius options
float radius;
float radiusToMapUnits = 1;
double radius;
double radiusToMapUnits = 1;
int myBuffer;
if ( d.variableRadius() )
{
Expand Down Expand Up @@ -226,7 +226,7 @@ void Heatmap::run()
// If radius is variable then fetch it and calculate new pixel buffer size
if ( d.variableRadius() )
{
radius = myFeature.attribute( rField ).toFloat() * radiusToMapUnits;
radius = myFeature.attribute( rField ).toDouble() * radiusToMapUnits;
myBuffer = bufferSize( radius, cellsize );
}

Expand All @@ -241,25 +241,25 @@ void Heatmap::run()
poBand->RasterIO( GF_Read, xPosition, yPosition, blockSize, blockSize,
dataBuffer, blockSize, blockSize, GDT_Float32, 0, 0 );

float weight = 1.0;
double weight = 1.0;
if ( d.weighted() )
{
weight = myFeature.attribute( wField ).toFloat();
weight = myFeature.attribute( wField ).toDouble();
}

for ( int xp = 0; xp <= myBuffer; xp++ )
{
for ( int yp = 0; yp <= myBuffer; yp++ )
{
float distance = sqrt( pow( xp, 2.0 ) + pow( yp, 2.0 ) );
double distance = sqrt( pow( xp, 2.0 ) + pow( yp, 2.0 ) );

// is pixel outside search bandwidth of feature?
if ( distance > myBuffer )
{
continue;
}

float pixelValue = weight * ( 1 - (( 1 - myDecay ) * distance / myBuffer ) );
double pixelValue = weight * ( 1 - (( 1 - myDecay ) * distance / myBuffer ) );

// clearing anamolies along the axes
if ( xp == 0 && yp == 0 )
Expand Down Expand Up @@ -305,7 +305,7 @@ void Heatmap::run()
*
*/

float Heatmap::mapUnitsOf( float meters, QgsCoordinateReferenceSystem layerCrs )
double Heatmap::mapUnitsOf( double meters, QgsCoordinateReferenceSystem layerCrs )
{
// Worker to transform metres input to mapunits
QgsDistanceArea da;
Expand All @@ -318,7 +318,7 @@ float Heatmap::mapUnitsOf( float meters, QgsCoordinateReferenceSystem layerCrs )
return meters / da.measureLine( QgsPoint( 0.0, 0.0 ), QgsPoint( 0.0, 1.0 ) );
}

int Heatmap::bufferSize( float radius, float cellsize )
int Heatmap::bufferSize( double radius, double cellsize )
{
// Calculate the buffer size in pixels

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/heatmap/heatmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ class Heatmap: public QObject, public QgisPlugin

private:
//! Worker to convert meters to map units
float mapUnitsOf( float meters, QgsCoordinateReferenceSystem crs );
double mapUnitsOf( double meters, QgsCoordinateReferenceSystem layerCrs );
//! Worker to calculate buffer size in pixels
int bufferSize( float radius, float cellsize );
int bufferSize( double radius, double cellsize );

// MANDATORY PLUGIN PROPERTY DECLARATIONS .....

Expand Down
20 changes: 10 additions & 10 deletions src/plugins/heatmap/heatmapgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void HeatmapGui::on_columnLineEdit_editingFinished()

void HeatmapGui::on_cellXLineEdit_editingFinished()
{
mXcellsize = cellXLineEdit->text().toFloat();
mXcellsize = cellXLineEdit->text().toDouble();
mYcellsize = mXcellsize;
mRows = mBBox.height() / mYcellsize + 1;
mColumns = mBBox.width() / mXcellsize + 1;
Expand All @@ -180,7 +180,7 @@ void HeatmapGui::on_cellXLineEdit_editingFinished()

void HeatmapGui::on_cellYLineEdit_editingFinished()
{
mYcellsize = cellYLineEdit->text().toFloat();
mYcellsize = cellYLineEdit->text().toDouble();
mXcellsize = mYcellsize;
mRows = mBBox.height() / mYcellsize + 1;
mColumns = mBBox.width() / mXcellsize + 1;
Expand Down Expand Up @@ -281,10 +281,10 @@ void HeatmapGui::updateBBox()
mBBox = inputLayer->extent();
QgsCoordinateReferenceSystem layerCrs = inputLayer->crs();

float radiusInMapUnits = 0.0;
double radiusInMapUnits = 0.0;
if ( useRadius->isChecked() )
{
float maxInField = inputLayer->maximumValue( radiusFieldCombo->itemData( radiusFieldCombo->currentIndex() ).toInt() ).toFloat();
double maxInField = inputLayer->maximumValue( radiusFieldCombo->itemData( radiusFieldCombo->currentIndex() ).toInt() ).toDouble();

if ( radiusFieldUnitCombo->currentIndex() == HeatmapGui::Meters )
{
Expand All @@ -297,7 +297,7 @@ void HeatmapGui::updateBBox()
}
else
{
float radiusValue = mBufferLineEdit->text().toFloat();
double radiusValue = mBufferLineEdit->text().toDouble();
if ( mRadiusUnitCombo->currentIndex() == HeatmapGui::Meters )
{
radiusInMapUnits = mapUnitsOf( radiusValue, layerCrs );
Expand Down Expand Up @@ -325,7 +325,7 @@ void HeatmapGui::updateBBox()
}
}

float HeatmapGui::mapUnitsOf( float meters, QgsCoordinateReferenceSystem layerCrs )
double HeatmapGui::mapUnitsOf( double meters, QgsCoordinateReferenceSystem layerCrs )
{
// converter function to transform metres input to mapunits
// so that bounding box can be updated
Expand Down Expand Up @@ -356,9 +356,9 @@ bool HeatmapGui::variableRadius()
return useRadius->isChecked();
}

float HeatmapGui::radius()
double HeatmapGui::radius()
{
float radius = mBufferLineEdit->text().toFloat();
double radius = mBufferLineEdit->text().toDouble();
if ( mRadiusUnitCombo->currentIndex() == HeatmapGui::Meters )
{
radius = mapUnitsOf( radius, inputVectorLayer()->crs() );
Expand All @@ -375,9 +375,9 @@ int HeatmapGui::radiusUnit()
return mRadiusUnitCombo->currentIndex();
}

float HeatmapGui::decayRatio()
double HeatmapGui::decayRatio()
{
return mDecayLineEdit->text().toFloat();
return mDecayLineEdit->text().toDouble();
}

int HeatmapGui::radiusField()
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/heatmap/heatmapgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class HeatmapGui : public QDialog, private Ui::HeatmapGuiBase
bool variableRadius();

/** Returns the fixed radius value */
float radius();
double radius();

/** Return the radius Unit (meters/map units) */
int radiusUnit();

/** Return the decay ratio */
float decayRatio();
double decayRatio();

/** Return the attribute field for variable radius */
int radiusField();
Expand All @@ -73,10 +73,10 @@ class HeatmapGui : public QDialog, private Ui::HeatmapGuiBase
int columns() { return mColumns; }

/** Returns the cell size X value */
float cellSizeX() { return mXcellsize; }
double cellSizeX() { return mXcellsize; }

/** Returns the cell size Y valuue */
float cellSizeY() { return mYcellsize; }
double cellSizeY() { return mYcellsize; }

/** Return the BBox */
QgsRectangle bbox() { return mBBox; }
Expand All @@ -86,7 +86,7 @@ class HeatmapGui : public QDialog, private Ui::HeatmapGuiBase

// bbox of layer for lineedit changes
QgsRectangle mBBox;
float mXcellsize, mYcellsize;
double mXcellsize, mYcellsize;
int mRows, mColumns;

/** Function to check wether all constrains are satisfied and enable the OK button */
Expand All @@ -102,7 +102,7 @@ class HeatmapGui : public QDialog, private Ui::HeatmapGuiBase
void updateSize();

/** Convert Maters value to the corresponding map units based on Layer projection */
float mapUnitsOf( float meters, QgsCoordinateReferenceSystem layerCrs );
double mapUnitsOf( double meters, QgsCoordinateReferenceSystem layerCrs );

private slots:
void on_mButtonBox_accepted();
Expand Down

0 comments on commit 239479a

Please sign in to comment.