Skip to content

Commit 3289c09

Browse files
committed
heatmap updates:
- fix warnings on windows - minor cleanups - some string updates
1 parent 30e2f32 commit 3289c09

File tree

5 files changed

+36
-49
lines changed

5 files changed

+36
-49
lines changed

python/core/qgsmaplayerregistry.sip

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public:
2020

2121
~QgsMapLayerRegistry();
2222

23-
//! Retrieve a pointer to a loaded plugin by id
24-
QgsMapLayer * mapLayer(QString theLayerId);
23+
//! Retrieve a pointer to a loaded layer by id
24+
QgsMapLayer *mapLayer(QString theLayerId);
2525

2626
//! Retrieve the mapLayers collection (mainly intended for use by projection)
2727
QMap<QString,QgsMapLayer*> & mapLayers();

resources/context_help/HeatmapGui-en_US

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<h1>Heatmap Plugin Help</h1>
2-
<p>Heatmap Plugin creates a heatmap raster for the input point vector layer. The heatmap is generated based on the number of points in a given location. The higher the number of points, the higher will be the corresponding pixel(s) value in the raster.</p>
2+
<p>Heatmap Plugin creates a heatmap raster for the input point vector layer. The heatmap is generated based on the number of points in a given location. The higher the number of points, the higher the corresponding pixel(s) value will be in the raster.</p>
33

44
<h2>The Dialog Parameters</h2>
55

66
<h3>Input Point Vector</h3>
7-
<p>The input is always a vector layer of point type. All the point vector layers that are currently loaded in the window are automatically populated in the input layer drop-down list. Click the Dropdown button and select the required layer.</p>
7+
<p>The input is always a vector layer of point type. All the point vector layers that are currently loaded in the canvas are automatically populated in the input layer drop-down list. Click the dropdown button and select the desired layer.</p>
88

99
<h3>Output Ratser</h3>
1010
<p>The output raster location and filename can be set by clicking the <label>...</label> button next to the output raster textbox.<br/>
1111
<b>Note:</b>The file format is automatically added depending upon the output format selected, if not explicitly given.</p>
1212

1313
<h3>Output Format</h3>
1414
<p>All the file creation supporting GDAL formats are available in the drop down list. Click and select the required output format for your file.<br/>
15-
<b>Note:</b> GeoTiff and ERDAS Imagine .img formats are recommended. Some formats makes the application crash. Kindly stick to the recommended formats until the crash issue is resolved or Use other formats if you know about its GDAL support completely.</p>
15+
<b>Note:</b> GeoTiff and ERDAS Imagine .img formats are recommended. Some formats make the application crash. Kindly stick to the recommended formats until the crash issue is resolved or use an other formats if you know GDAL supports it completely.</p>
1616

1717
<h3>Heatmap Point Attributes</h3>
1818
<h4>Buffer Radius</h4>
1919
<p>The buffer radius specifies the number of <b>pixels or cells</b> around a point where the heat(influence) of the point will be felt. Smaller values give you clear distinction between points and bigger values gives you nicely merged heatmap regions. This is the spatial parameter of the heat region of a point.<br/>
2020
<b>Note:</b> The value is always a whole number.</p>
2121
<h4>Decay Ratio</h4>
2222
<p>The decay ratio defines amount of heat(influence) that should reach the outermost pixel in the Buffer Radius. It is the direct measure of the outer most value.<br/>
23-
<b>Example:</b> If Buffer Ratio is set as 0 and Radius as 10, then the centre pixel of a point will have value and the pixel away by a distance of 10 units will have the value 0, a pixel which is 5 units away will have a value of 0.5 and so on. Here distance is measure not by pixel count rather using sqrt( xOffset^2 + yOffset^2 ), so you always get a circular heat region.</p>
23+
<b>Example:</b> If Buffer Ratio is set as 0 and Radius as 10, then the center pixel of a point will have value and the pixel away by a distance of 10 units will have the value 0, a pixel which is 5 units away will have a value of 0.5 and so on. Here distance is measure not by pixel count rather using sqrt( xOffset^2 + yOffset^2 ), so you always get a circular heat region.</p>
2424

2525
<h2>Further Details</h2>
2626
<p>Contact the author through aruntheguy at gmail dot com</p>

src/core/qgsmaplayerregistry.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
4646

4747
~QgsMapLayerRegistry();
4848

49-
//! Retrieve a pointer to a loaded plugin by id
49+
//! Retrieve a pointer to a loaded layer by id
5050
QgsMapLayer * mapLayer( QString theLayerId );
5151

5252
//! Retrieve the mapLayers collection (mainly intended for use by projectio)

src/plugins/heatmap/heatmap.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void Heatmap::initGui()
7474
// Create the action for tool
7575
mQActionPointer = new QAction( QIcon( ":/heatmap/heatmap.png" ), tr( "Heatmap" ), this );
7676
// Set the what's this text
77-
mQActionPointer->setWhatsThis( tr( "Creats a heatmap raster for the input point vector." ) );
77+
mQActionPointer->setWhatsThis( tr( "Creates a heatmap raster for the input point vector." ) );
7878
// Connect the action to the run
7979
connect( mQActionPointer, SIGNAL( triggered() ), this, SLOT( run() ) );
8080
// Add the icon to the toolbar
@@ -130,7 +130,7 @@ void Heatmap::createRaster( QgsVectorLayer* theVectorLayer, int theBuffer, float
130130
myDriver = GetGDALDriverManager()->GetDriverByName( theOutputFormat.toUtf8() );
131131
if ( myDriver == NULL )
132132
{
133-
QMessageBox::information( 0, tr( "Error in GDAL Driver!" ), tr( "Cannot open the driver for the format specified" ) );
133+
QMessageBox::information( 0, tr( "GDAL driver error" ), tr( "Cannot open the driver for the specified format" ) );
134134
return;
135135
}
136136

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

161161
float* line = ( float * ) CPLMalloc( sizeof( float ) * xSize );
162-
std::fill_n( line, xSize, NO_DATA );
162+
for ( int i = 0; i < xSize; i++ )
163+
line[i] = NO_DATA;
163164
// Write the empty raster
164-
for ( int i = 0; i < ySize ; i += 1 )
165+
for ( int i = 0; i < ySize ; i++ )
165166
{
166167
poBand->RasterIO( GF_Write, 0, 0, xSize, 1, line, xSize, 1, GDT_Float32, 0, 0 );
167168
}
@@ -175,7 +176,7 @@ void Heatmap::createRaster( QgsVectorLayer* theVectorLayer, int theBuffer, float
175176
heatmapDS = ( GDALDataset * ) GDALOpen( theOutputFilename.toUtf8(), GA_Update );
176177
if ( !heatmapDS )
177178
{
178-
QMessageBox::information( 0, tr( "Error in Updating Raster!" ), tr( "Couldnot open the created raster for updation. The Heatmap was not generated." ) );
179+
QMessageBox::information( 0, tr( "Raster update error" ), tr( "Could not open the created raster for updating. The heatmap was not generated." ) );
179180
return;
180181
}
181182
poBand = heatmapDS->GetRasterBand( 1 );
@@ -185,7 +186,7 @@ void Heatmap::createRaster( QgsVectorLayer* theVectorLayer, int theBuffer, float
185186
QgsVectorDataProvider* myVectorProvider = theVectorLayer->dataProvider();
186187
if ( !myVectorProvider )
187188
{
188-
QMessageBox::information( 0, tr( "Error in Point Layer!" ), tr( "Couldnot identify the vector data provider." ) );
189+
QMessageBox::information( 0, tr( "Point layer error" ), tr( "Could not identify the vector data provider." ) );
189190
return;
190191
}
191192
QgsAttributeList dummyList;
@@ -201,11 +202,11 @@ void Heatmap::createRaster( QgsVectorLayer* theVectorLayer, int theBuffer, float
201202

202203
while ( myVectorProvider->nextFeature( myFeature ) )
203204
{
204-
counter += 1;
205+
counter++;
205206
p.setValue( counter );
206207
if ( p.wasCanceled() )
207208
{
208-
QMessageBox::information( 0, tr( "Heatmap Generation Aborted!" ), tr( "QGIS will now load the partially-computed raster." ) );
209+
QMessageBox::information( 0, tr( "Heatmap generation aborted" ), tr( "QGIS will now load the partially-computed raster." ) );
209210
break;
210211
}
211212

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

231-
for ( int xp = 0; xp <= theBuffer; xp += 1 )
232+
for ( int xp = 0; xp <= theBuffer; xp++ )
232233
{
233-
for ( int yp = 0; yp <= theBuffer; yp += 1 )
234+
for ( int yp = 0; yp <= theBuffer; yp++ )
234235
{
235-
float distance = sqrt( pow( xp, 2 ) + pow( yp, 2 ) );
236+
float distance = sqrt( pow( xp, 2.0 ) + pow( yp, 2.0 ) );
236237
float pixelValue = 1 - (( 1 - theDecay ) * distance / theBuffer );
237238

238239
// clearing anamolies along the axes
@@ -252,7 +253,7 @@ void Heatmap::createRaster( QgsVectorLayer* theVectorLayer, int theBuffer, float
252253
pos[1] = ( theBuffer + xp ) * blockSize + ( theBuffer - yp );
253254
pos[2] = ( theBuffer - xp ) * blockSize + ( theBuffer + yp );
254255
pos[3] = ( theBuffer - xp ) * blockSize + ( theBuffer - yp );
255-
for ( int p = 0; p < 4; p += 1 )
256+
for ( int p = 0; p < 4; p++ )
256257
{
257258
if ( dataBuffer[ pos[p] ] == NO_DATA )
258259
{

src/plugins/heatmap/heatmapgui.cpp

+16-30
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,13 @@ HeatmapGui::HeatmapGui( QWidget* parent, Qt::WFlags fl )
3636
setupUi( this );
3737

3838
// Adding point layers to the mInputVectorCombo
39-
QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
40-
QMapIterator<QString, QgsMapLayer*> layers( mapLayers );
41-
42-
while ( layers.hasNext() )
39+
foreach( QgsMapLayer *l, QgsMapLayerRegistry::instance()->mapLayers() )
4340
{
44-
layers.next();
45-
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer *>( layers.value() );
46-
if (( vl ) && ( vl->geometryType() == QGis::Point ) )
47-
{
48-
mInputVectorCombo->addItem( vl->name(), QVariant( vl->id() ) );
49-
}
41+
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( l );
42+
if ( !vl || vl->geometryType() != QGis::Point )
43+
continue;
44+
45+
mInputVectorCombo->addItem( vl->name(), vl->id() );
5046
}
5147

5248
// Adding GDAL drivers with CREATE to the mFormatCombo
@@ -85,7 +81,6 @@ HeatmapGui::~HeatmapGui()
8581
void HeatmapGui::on_mButtonBox_accepted()
8682
{
8783
// Variables to be emitted with the createRaster signal
88-
QgsVectorLayer* inputLayer;
8984
int bufferDistance;
9085
float decayRatio;
9186
QString outputFileName;
@@ -94,31 +89,21 @@ void HeatmapGui::on_mButtonBox_accepted()
9489
QString dummyText;
9590

9691
// The input vector layer
97-
int myLayerId = mInputVectorCombo->itemData( mInputVectorCombo->currentIndex() ).toInt();
98-
99-
QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
100-
QMapIterator<QString, QgsMapLayer*> layers( mapLayers );
92+
QString myLayerId = mInputVectorCombo->itemData( mInputVectorCombo->currentIndex() ).toString();
10193

102-
while ( layers.hasNext() )
94+
QgsVectorLayer* inputLayer = qobject_cast<QgsVectorLayer *>( QgsMapLayerRegistry::instance()->mapLayer( myLayerId ) );
95+
if ( !inputLayer )
10396
{
104-
layers.next();
105-
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer *>( layers.value() );
106-
if ( vl )
107-
{
108-
dummyText = vl->id();
109-
if ( dummyText.toInt() == myLayerId )
110-
{
111-
inputLayer = vl;
112-
}
113-
}
97+
QMessageBox::information( 0, tr( "Layer not found" ), tr( "Layer %1 not found." ).arg( myLayerId ) );
98+
return;
11499
}
115100

116101
// The buffer distance
117102
dummyText = mBufferLineEdit->text();
118103
bufferDistance = dummyText.toInt();
119-
if ( bufferDistance == NULL )
104+
if ( bufferDistance == 0 )
120105
{
121-
QMessageBox::information( 0, tr( "Invalid Buffer Value!" ), tr( "Buffer distance cannot be NULL, kindly enter a valid value." ) );
106+
QMessageBox::information( 0, tr( "Invalid buffer value" ), tr( "Buffer distance cannot be zero. Please enter a valid value." ) );
122107
return;
123108
}
124109
// The decay ratio
@@ -130,7 +115,7 @@ void HeatmapGui::on_mButtonBox_accepted()
130115
QFileInfo myFileInfo( outputFileName );
131116
if ( outputFileName.isEmpty() || !myFileInfo.dir().exists() )
132117
{
133-
QMessageBox::information( 0, tr( "Output filename is invalid!" ), tr( "Kindly enter a valid output file path and name." ) );
118+
QMessageBox::information( 0, tr( "Invalid output filename" ), tr( "Please enter a valid output file path and name." ) );
134119
return;
135120
}
136121

@@ -155,6 +140,7 @@ void HeatmapGui::on_mButtonBox_accepted()
155140
}
156141

157142
emit createRaster( inputLayer, bufferDistance, decayRatio, outputFileName, outputFormat );
143+
158144
//and finally
159145
accept();
160146
}
@@ -174,7 +160,7 @@ void HeatmapGui::on_mBrowseButton_clicked()
174160
QSettings s;
175161
QString lastDir = s.value( "/Heatmap/lastOutputDir", "" ).toString();
176162

177-
QString outputFilename = QFileDialog::getSaveFileName( 0, tr( "Save Heatmap as: " ), lastDir );
163+
QString outputFilename = QFileDialog::getSaveFileName( 0, tr( "Save Heatmap as:" ), lastDir );
178164
if ( !outputFilename.isEmpty() )
179165
{
180166
mOutputRasterLineEdit->setText( outputFilename );

0 commit comments

Comments
 (0)