Skip to content

Commit

Permalink
heatmap plugin: port to C-API (fixes #15028)
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Jun 22, 2016
1 parent d7414d7 commit 1860366
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -568,6 +568,7 @@ void QgsAppLayerTreeViewMenuProvider::editSymbolLegendNodeSymbol()
QScopedPointer< QgsSymbolV2 > symbol( originalSymbol->clone() );
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( node->layerNode()->layer() );
QgsSymbolV2SelectorDialog dlg( symbol.data(), QgsStyleV2::defaultStyle(), vlayer, mView->window() );
dlg.setWindowTitle( tr( "Edit symbol" ) );
dlg.setMapCanvas( mCanvas );
if ( dlg.exec() )
{
Expand Down
39 changes: 17 additions & 22 deletions src/plugins/heatmap/heatmap.cpp
Expand Up @@ -16,9 +16,9 @@
***************************************************************************/

// GDAL includes
#include "gdal_priv.h"
#include "cpl_string.h"
#include "cpl_conv.h"
#include <gdal.h>
#include <cpl_string.h>
#include <cpl_conv.h>

// QGIS Specific includes
#include <qgisinterface.h>
Expand Down Expand Up @@ -142,25 +142,21 @@ void Heatmap::run()
// Getting the rasterdataset in place
GDALAllRegister();

GDALDataset *emptyDataset;
GDALDriver *myDriver;

myDriver = GetGDALDriverManager()->GetDriverByName( d.outputFormat().toUtf8() );
GDALDriverH myDriver = GDALGetDriverByName( d.outputFormat().toUtf8() );
if ( !myDriver )
{
mQGisIface->messageBar()->pushMessage( tr( "GDAL driver error" ), tr( "Cannot open the driver for the specified format" ), QgsMessageBar::WARNING, mQGisIface->messageTimeout() );
return;
}

double geoTransform[6] = { myBBox.xMinimum(), cellsize, 0, myBBox.yMinimum(), 0, cellsize };
emptyDataset = myDriver->Create( d.outputFilename().toUtf8(), columns, rows, 1, GDT_Float32, nullptr );
emptyDataset->SetGeoTransform( geoTransform );
GDALDatasetH emptyDataset = GDALCreate( myDriver, d.outputFilename().toUtf8(), columns, rows, 1, GDT_Float32, nullptr );
GDALSetGeoTransform( emptyDataset, geoTransform );
// Set the projection on the raster destination to match the input layer
emptyDataset->SetProjection( inputLayer->crs().toWkt().toLocal8Bit().data() );
GDALSetProjection( emptyDataset, inputLayer->crs().toWkt().toLocal8Bit().data() );

GDALRasterBand *poBand;
poBand = emptyDataset->GetRasterBand( 1 );
poBand->SetNoDataValue( NO_DATA );
GDALRasterBandH poBand = GDALGetRasterBand( emptyDataset, 1 );
GDALSetRasterNoDataValue( poBand, NO_DATA );

float* line = ( float * ) CPLMalloc( sizeof( float ) * columns );
for ( int i = 0; i < columns ; i++ )
Expand All @@ -170,25 +166,24 @@ void Heatmap::run()
// Write the empty raster
for ( int i = 0; i < rows ; i++ )
{
if ( poBand->RasterIO( GF_Write, 0, i, columns, 1, line, columns, 1, GDT_Float32, 0, 0 ) != CE_None )
if ( GDALRasterIO( poBand, GF_Write, 0, i, columns, 1, line, columns, 1, GDT_Float32, 0, 0 ) != CE_None )
{
QgsDebugMsg( "Raster IO Error" );
}
}

CPLFree( line );
//close the dataset
GDALClose(( GDALDatasetH ) emptyDataset );
GDALClose( emptyDataset );

// open the raster in GA_Update mode
GDALDataset *heatmapDS;
heatmapDS = ( GDALDataset * ) GDALOpen( TO8F( d.outputFilename() ), GA_Update );
GDALDatasetH heatmapDS = GDALOpen( TO8F( d.outputFilename() ), GA_Update );
if ( !heatmapDS )
{
mQGisIface->messageBar()->pushMessage( tr( "Raster update error" ), tr( "Could not open the created raster for updating. The heatmap was not generated." ), QgsMessageBar::WARNING );
return;
}
poBand = heatmapDS->GetRasterBand( 1 );
poBand = GDALGetRasterBand( heatmapDS, 1 );

QgsAttributeList myAttrList;
int rField = 0;
Expand Down Expand Up @@ -302,8 +297,8 @@ void Heatmap::run()

// get the data
float *dataBuffer = ( float * ) CPLMalloc( sizeof( float ) * blockSize * blockSize );
if ( poBand->RasterIO( GF_Read, xPosition, yPosition, blockSize, blockSize,
dataBuffer, blockSize, blockSize, GDT_Float32, 0, 0 ) != CE_None )
if ( GDALRasterIO( poBand, GF_Read, xPosition, yPosition, blockSize, blockSize,
dataBuffer, blockSize, blockSize, GDT_Float32, 0, 0 ) != CE_None )
{
QgsDebugMsg( "Raster IO Error" );
}
Expand Down Expand Up @@ -347,8 +342,8 @@ void Heatmap::run()
}
}
}
if ( poBand->RasterIO( GF_Write, xPosition, yPosition, blockSize, blockSize,
dataBuffer, blockSize, blockSize, GDT_Float32, 0, 0 ) != CE_None )
if ( GDALRasterIO( poBand, GF_Write, xPosition, yPosition, blockSize, blockSize,
dataBuffer, blockSize, blockSize, GDT_Float32, 0, 0 ) != CE_None )
{
QgsDebugMsg( "Raster IO Error" );
}
Expand Down
18 changes: 9 additions & 9 deletions src/plugins/heatmap/heatmapgui.cpp
Expand Up @@ -24,9 +24,9 @@
#include "qgsdistancearea.h"

// GDAL includes
#include "gdal_priv.h"
#include "cpl_string.h"
#include "cpl_conv.h"
#include <gdal.h>
#include <cpl_string.h>
#include <cpl_conv.h>

//qt includes
#include <QComboBox>
Expand Down Expand Up @@ -83,18 +83,18 @@ HeatmapGui::HeatmapGui( QWidget* parent, Qt::WindowFlags fl, QMap<QString, QVari
int nDrivers = GDALGetDriverCount();
for ( int i = 0; i < nDrivers; i += 1 )
{
GDALDriver* nthDriver = GetGDALDriverManager()->GetDriver( i );
char** driverMetadata = nthDriver->GetMetadata();
GDALDriverH nthDriver = GDALGetDriver( i );
char **driverMetadata = GDALGetMetadata( nthDriver, nullptr );
// Only formats which allow creation of Float32 data types are valid
if ( CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) &&
QString( nthDriver->GetMetadataItem( GDAL_DMD_CREATIONDATATYPES, nullptr ) ).contains( "Float32" ) )
QString( GDALGetMetadataItem( nthDriver, GDAL_DMD_CREATIONDATATYPES, nullptr ) ).contains( "Float32" ) )
{
++myIndex;
QString myLongName = nthDriver->GetMetadataItem( GDAL_DMD_LONGNAME );
QString myLongName = GDALGetMetadataItem( nthDriver, GDAL_DMD_LONGNAME, nullptr );
// Add LongName text, shortname variant; GetDescription actually gets the shortname
mFormatCombo->addItem( myLongName, QVariant( nthDriver->GetDescription() ) );
mFormatCombo->addItem( myLongName, QVariant( GDALGetDescription( nthDriver ) ) );
// Add the drivers and their extensions to a map for filename correction
mExtensionMap.insert( nthDriver->GetDescription(), nthDriver->GetMetadataItem( GDAL_DMD_EXTENSION ) );
mExtensionMap.insert( GDALGetDescription( nthDriver ), GDALGetMetadataItem( nthDriver, GDAL_DMD_EXTENSION, nullptr ) );
if ( myLongName == "GeoTIFF" )
{
myTiffIndex = myIndex;
Expand Down

0 comments on commit 1860366

Please sign in to comment.