Skip to content

Commit

Permalink
Use QgsFeatureSource instead of QgsVectorLayer for QgsKde
Browse files Browse the repository at this point in the history
Allows generation of heatmaps from feature sources instead
of requiring vector layer inputs
  • Loading branch information
nyalldawson committed Jul 13, 2017
1 parent eb0c301 commit d9e29b9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
4 changes: 2 additions & 2 deletions python/analysis/raster/qgskde.sip
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class QgsKernelDensityEstimation

struct Parameters
{
QgsVectorLayer *vectorLayer;
QgsFeatureSource *source;
%Docstring
Vector point layer
Point feature source
%End

double radius;
Expand Down
21 changes: 11 additions & 10 deletions src/analysis/raster/qgskde.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
***************************************************************************/

#include "qgskde.h"
#include "qgsvectorlayer.h"
#include "qgsfeaturesource.h"
#include "qgsfeatureiterator.h"
#include "qgsgeometry.h"

#define NO_DATA -9999
Expand All @@ -24,7 +25,7 @@
#endif

QgsKernelDensityEstimation::QgsKernelDensityEstimation( const QgsKernelDensityEstimation::Parameters &parameters, const QString &outputFile, const QString &outputFormat )
: mInputLayer( parameters.vectorLayer )
: mSource( parameters.source )
, mOutputFile( outputFile )
, mOutputFormat( outputFormat )
, mRadiusField( -1 )
Expand All @@ -39,9 +40,9 @@ QgsKernelDensityEstimation::QgsKernelDensityEstimation( const QgsKernelDensityEs
, mRasterBandH( nullptr )
{
if ( !parameters.radiusField.isEmpty() )
mRadiusField = mInputLayer->fields().lookupField( parameters.radiusField );
mRadiusField = mSource->fields().lookupField( parameters.radiusField );
if ( !parameters.weightField.isEmpty() )
mWeightField = mInputLayer->fields().lookupField( parameters.weightField );
mWeightField = mSource->fields().lookupField( parameters.weightField );
}

QgsKernelDensityEstimation::Result QgsKernelDensityEstimation::run()
Expand All @@ -58,7 +59,7 @@ QgsKernelDensityEstimation::Result QgsKernelDensityEstimation::run()
if ( mWeightField >= 0 )
requiredAttributes << mWeightField;

QgsFeatureIterator fit = mInputLayer->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( requiredAttributes ) );
QgsFeatureIterator fit = mSource->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( requiredAttributes ) );

QgsFeature f;
while ( fit.nextFeature( f ) )
Expand All @@ -79,7 +80,7 @@ QgsKernelDensityEstimation::Result QgsKernelDensityEstimation::prepare()
return DriverError;
}

if ( !mInputLayer )
if ( !mSource )
return InvalidParameters;

mBounds = calculateBounds();
Expand Down Expand Up @@ -235,7 +236,7 @@ bool QgsKernelDensityEstimation::createEmptyLayer( GDALDriverH driver, const Qgs
return false;

// Set the projection on the raster destination to match the input layer
if ( GDALSetProjection( emptyDataset, mInputLayer->crs().toWkt().toLocal8Bit().data() ) != CE_None )
if ( GDALSetProjection( emptyDataset, mSource->sourceCrs().toWkt().toLocal8Bit().data() ) != CE_None )
return false;

GDALRasterBandH poBand = GDALGetRasterBand( emptyDataset, 1 );
Expand Down Expand Up @@ -399,16 +400,16 @@ double QgsKernelDensityEstimation::triangularKernel( const double distance, cons

QgsRectangle QgsKernelDensityEstimation::calculateBounds() const
{
if ( !mInputLayer )
if ( !mSource )
return QgsRectangle();

QgsRectangle bbox = mInputLayer->extent();
QgsRectangle bbox = mSource->sourceExtent();

double radius = 0;
if ( mRadiusField >= 0 )
{
// if radius is using a field, find the max value
radius = mInputLayer->maximumValue( mRadiusField ).toDouble();
radius = mSource->maximumValue( mRadiusField ).toDouble();
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions src/analysis/raster/qgskde.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <cpl_conv.h>
#include "qgis_analysis.h"

class QgsVectorLayer;
class QgsFeatureSource;
class QProgressDialog;
class QgsFeature;

Expand Down Expand Up @@ -70,8 +70,8 @@ class ANALYSIS_EXPORT QgsKernelDensityEstimation
//! KDE parameters
struct Parameters
{
//! Vector point layer
QgsVectorLayer *vectorLayer = nullptr;
//! Point feature source
QgsFeatureSource *source = nullptr;

//! Fixed radius, in map units
double radius;
Expand Down Expand Up @@ -146,7 +146,7 @@ class ANALYSIS_EXPORT QgsKernelDensityEstimation

QgsRectangle calculateBounds() const;

QgsVectorLayer *mInputLayer = nullptr;
QgsFeatureSource *mSource = nullptr;

QString mOutputFile;
QString mOutputFormat;
Expand Down

0 comments on commit d9e29b9

Please sign in to comment.