25 changes: 13 additions & 12 deletions src/plugins/heatmap/heatmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void Heatmap::initGui()
// Create the action for tool
mQActionPointer = new QAction( QIcon( ":/heatmap/heatmap.png" ), tr( "Heatmap" ), this );
// Set the what's this text
mQActionPointer->setWhatsThis( tr( "Creats a heatmap raster for the input point vector." ) );
mQActionPointer->setWhatsThis( tr( "Creates a heatmap raster for the input point vector." ) );
// Connect the action to the run
connect( mQActionPointer, SIGNAL( triggered() ), this, SLOT( run() ) );
// Add the icon to the toolbar
Expand Down Expand Up @@ -130,7 +130,7 @@ void Heatmap::createRaster( QgsVectorLayer* theVectorLayer, int theBuffer, float
myDriver = GetGDALDriverManager()->GetDriverByName( theOutputFormat.toUtf8() );
if ( myDriver == NULL )
{
QMessageBox::information( 0, tr( "Error in GDAL Driver!" ), tr( "Cannot open the driver for the format specified" ) );
QMessageBox::information( 0, tr( "GDAL driver error" ), tr( "Cannot open the driver for the specified format" ) );
return;
}

Expand Down Expand Up @@ -159,9 +159,10 @@ void Heatmap::createRaster( QgsVectorLayer* theVectorLayer, int theBuffer, float
poBand->SetNoDataValue( NO_DATA );

float* line = ( float * ) CPLMalloc( sizeof( float ) * xSize );
std::fill_n( line, xSize, NO_DATA );
for ( int i = 0; i < xSize; i++ )
line[i] = NO_DATA;
// Write the empty raster
for ( int i = 0; i < ySize ; i += 1 )
for ( int i = 0; i < ySize ; i++ )
{
poBand->RasterIO( GF_Write, 0, 0, xSize, 1, line, xSize, 1, GDT_Float32, 0, 0 );
}
Expand All @@ -175,7 +176,7 @@ void Heatmap::createRaster( QgsVectorLayer* theVectorLayer, int theBuffer, float
heatmapDS = ( GDALDataset * ) GDALOpen( theOutputFilename.toUtf8(), GA_Update );
if ( !heatmapDS )
{
QMessageBox::information( 0, tr( "Error in Updating Raster!" ), tr( "Couldnot open the created raster for updation. The Heatmap was not generated." ) );
QMessageBox::information( 0, tr( "Raster update error" ), tr( "Could not open the created raster for updating. The heatmap was not generated." ) );
return;
}
poBand = heatmapDS->GetRasterBand( 1 );
Expand All @@ -185,7 +186,7 @@ void Heatmap::createRaster( QgsVectorLayer* theVectorLayer, int theBuffer, float
QgsVectorDataProvider* myVectorProvider = theVectorLayer->dataProvider();
if ( !myVectorProvider )
{
QMessageBox::information( 0, tr( "Error in Point Layer!" ), tr( "Couldnot identify the vector data provider." ) );
QMessageBox::information( 0, tr( "Point layer error" ), tr( "Could not identify the vector data provider." ) );
return;
}
QgsAttributeList dummyList;
Expand All @@ -201,11 +202,11 @@ void Heatmap::createRaster( QgsVectorLayer* theVectorLayer, int theBuffer, float

while ( myVectorProvider->nextFeature( myFeature ) )
{
counter += 1;
counter++;
p.setValue( counter );
if ( p.wasCanceled() )
{
QMessageBox::information( 0, tr( "Heatmap Generation Aborted!" ), tr( "QGIS will now load the partially-computed raster." ) );
QMessageBox::information( 0, tr( "Heatmap generation aborted" ), tr( "QGIS will now load the partially-computed raster." ) );
break;
}

Expand All @@ -228,11 +229,11 @@ void Heatmap::createRaster( QgsVectorLayer* theVectorLayer, int theBuffer, float
float *dataBuffer = ( float * ) CPLMalloc( sizeof( float ) * blockSize * blockSize );
poBand->RasterIO( GF_Read, xPosition, yPosition, blockSize, blockSize, dataBuffer, blockSize, blockSize, GDT_Float32, 0, 0 );

for ( int xp = 0; xp <= theBuffer; xp += 1 )
for ( int xp = 0; xp <= theBuffer; xp++ )
{
for ( int yp = 0; yp <= theBuffer; yp += 1 )
for ( int yp = 0; yp <= theBuffer; yp++ )
{
float distance = sqrt( pow( xp, 2 ) + pow( yp, 2 ) );
float distance = sqrt( pow( xp, 2.0 ) + pow( yp, 2.0 ) );
float pixelValue = 1 - (( 1 - theDecay ) * distance / theBuffer );

// clearing anamolies along the axes
Expand All @@ -252,7 +253,7 @@ void Heatmap::createRaster( QgsVectorLayer* theVectorLayer, int theBuffer, float
pos[1] = ( theBuffer + xp ) * blockSize + ( theBuffer - yp );
pos[2] = ( theBuffer - xp ) * blockSize + ( theBuffer + yp );
pos[3] = ( theBuffer - xp ) * blockSize + ( theBuffer - yp );
for ( int p = 0; p < 4; p += 1 )
for ( int p = 0; p < 4; p++ )
{
if ( dataBuffer[ pos[p] ] == NO_DATA )
{
Expand Down
46 changes: 16 additions & 30 deletions src/plugins/heatmap/heatmapgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,13 @@ HeatmapGui::HeatmapGui( QWidget* parent, Qt::WFlags fl )
setupUi( this );

// Adding point layers to the mInputVectorCombo
QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
QMapIterator<QString, QgsMapLayer*> layers( mapLayers );

while ( layers.hasNext() )
foreach( QgsMapLayer *l, QgsMapLayerRegistry::instance()->mapLayers() )
{
layers.next();
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer *>( layers.value() );
if (( vl ) && ( vl->geometryType() == QGis::Point ) )
{
mInputVectorCombo->addItem( vl->name(), QVariant( vl->id() ) );
}
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( l );
if ( !vl || vl->geometryType() != QGis::Point )
continue;

mInputVectorCombo->addItem( vl->name(), vl->id() );
}

// Adding GDAL drivers with CREATE to the mFormatCombo
Expand Down Expand Up @@ -85,7 +81,6 @@ HeatmapGui::~HeatmapGui()
void HeatmapGui::on_mButtonBox_accepted()
{
// Variables to be emitted with the createRaster signal
QgsVectorLayer* inputLayer;
int bufferDistance;
float decayRatio;
QString outputFileName;
Expand All @@ -94,31 +89,21 @@ void HeatmapGui::on_mButtonBox_accepted()
QString dummyText;

// The input vector layer
int myLayerId = mInputVectorCombo->itemData( mInputVectorCombo->currentIndex() ).toInt();

QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
QMapIterator<QString, QgsMapLayer*> layers( mapLayers );
QString myLayerId = mInputVectorCombo->itemData( mInputVectorCombo->currentIndex() ).toString();

while ( layers.hasNext() )
QgsVectorLayer* inputLayer = qobject_cast<QgsVectorLayer *>( QgsMapLayerRegistry::instance()->mapLayer( myLayerId ) );
if ( !inputLayer )
{
layers.next();
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer *>( layers.value() );
if ( vl )
{
dummyText = vl->id();
if ( dummyText.toInt() == myLayerId )
{
inputLayer = vl;
}
}
QMessageBox::information( 0, tr( "Layer not found" ), tr( "Layer %1 not found." ).arg( myLayerId ) );
return;
}

// The buffer distance
dummyText = mBufferLineEdit->text();
bufferDistance = dummyText.toInt();
if ( bufferDistance == NULL )
if ( bufferDistance == 0 )
{
QMessageBox::information( 0, tr( "Invalid Buffer Value!" ), tr( "Buffer distance cannot be NULL, kindly enter a valid value." ) );
QMessageBox::information( 0, tr( "Invalid buffer value" ), tr( "Buffer distance cannot be zero. Please enter a valid value." ) );
return;
}
// The decay ratio
Expand All @@ -130,7 +115,7 @@ void HeatmapGui::on_mButtonBox_accepted()
QFileInfo myFileInfo( outputFileName );
if ( outputFileName.isEmpty() || !myFileInfo.dir().exists() )
{
QMessageBox::information( 0, tr( "Output filename is invalid!" ), tr( "Kindly enter a valid output file path and name." ) );
QMessageBox::information( 0, tr( "Invalid output filename" ), tr( "Please enter a valid output file path and name." ) );
return;
}

Expand All @@ -155,6 +140,7 @@ void HeatmapGui::on_mButtonBox_accepted()
}

emit createRaster( inputLayer, bufferDistance, decayRatio, outputFileName, outputFormat );

//and finally
accept();
}
Expand All @@ -174,7 +160,7 @@ void HeatmapGui::on_mBrowseButton_clicked()
QSettings s;
QString lastDir = s.value( "/Heatmap/lastOutputDir", "" ).toString();

QString outputFilename = QFileDialog::getSaveFileName( 0, tr( "Save Heatmap as: " ), lastDir );
QString outputFilename = QFileDialog::getSaveFileName( 0, tr( "Save Heatmap as:" ), lastDir );
if ( !outputFilename.isEmpty() )
{
mOutputRasterLineEdit->setText( outputFilename );
Expand Down