Skip to content
Permalink
Browse files

Use QgsFeatureSource instead of QgsVectorLayer for QgsKde

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 d9e29b974268b07202db0d6300822a9c23a75400
Showing with 17 additions and 16 deletions.
  1. +2 −2 python/analysis/raster/qgskde.sip
  2. +11 −10 src/analysis/raster/qgskde.cpp
  3. +4 −4 src/analysis/raster/qgskde.h
@@ -49,9 +49,9 @@ class QgsKernelDensityEstimation

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

double radius;
@@ -14,7 +14,8 @@
***************************************************************************/

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

#define NO_DATA -9999
@@ -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 )
@@ -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()
@@ -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 ) )
@@ -79,7 +80,7 @@ QgsKernelDensityEstimation::Result QgsKernelDensityEstimation::prepare()
return DriverError;
}

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

mBounds = calculateBounds();
@@ -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 );
@@ -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
{
@@ -25,7 +25,7 @@
#include <cpl_conv.h>
#include "qgis_analysis.h"

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

@@ -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;
@@ -146,7 +146,7 @@ class ANALYSIS_EXPORT QgsKernelDensityEstimation

QgsRectangle calculateBounds() const;

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

QString mOutputFile;
QString mOutputFormat;

0 comments on commit d9e29b9

Please sign in to comment.
You can’t perform that action at this time.