Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Dbscan outputs number of clusters
  • Loading branch information
nyalldawson committed Jul 16, 2018
1 parent c21fe59 commit d4d9186
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/analysis/processing/qgsalgorithmdbscanclustering.cpp
Expand Up @@ -70,8 +70,9 @@ void QgsDbscanClusteringAlgorithm::initAlgorithm( const QVariantMap & )
fieldNameParam->setFlags( fieldNameParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
addParameter( fieldNameParam.release() );


addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Clusters" ), QgsProcessing::TypeVectorPoint ) );

addOutput( new QgsProcessingOutputNumber( QStringLiteral( "NUM_CLUSTERS" ), QObject::tr( "Number of clusters" ) ) );
}

QString QgsDbscanClusteringAlgorithm::shortHelpString() const
Expand Down Expand Up @@ -145,7 +146,8 @@ QVariantMap QgsDbscanClusteringAlgorithm::processAlgorithm( const QVariantMap &p
QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() ) );
const long featureCount = source->featureCount();

dbscan( minSize, eps, borderPointsAreNoise, featureCount, features, index, idToCluster, feedback );
int clusterCount = 0;
dbscan( minSize, eps, borderPointsAreNoise, featureCount, features, index, idToCluster, clusterCount, feedback );

// write clusters
const double writeStep = featureCount > 0 ? 10.0 / featureCount : 1;
Expand Down Expand Up @@ -177,6 +179,7 @@ QVariantMap QgsDbscanClusteringAlgorithm::processAlgorithm( const QVariantMap &p

QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
outputs.insert( QStringLiteral( "NUM_CLUSTERS" ), clusterCount );
return outputs;
}

Expand All @@ -188,6 +191,7 @@ void QgsDbscanClusteringAlgorithm::dbscan( const std::size_t minSize,
QgsFeatureIterator features,
QgsSpatialIndexKDBush &index,
std::unordered_map< QgsFeatureId, int> &idToCluster,
int &clusterCount,
QgsProcessingFeedback *feedback )
{
const double step = featureCount > 0 ? 90.0 / featureCount : 1;
Expand All @@ -196,8 +200,8 @@ void QgsDbscanClusteringAlgorithm::dbscan( const std::size_t minSize,
visited.reserve( index.size() );

QgsFeature feat;
int clusterIdx = 0;
int i = 0;
clusterCount = 0;

while ( features.nextFeature( feat ) )
{
Expand Down Expand Up @@ -249,8 +253,8 @@ void QgsDbscanClusteringAlgorithm::dbscan( const std::size_t minSize,
}

// start new cluster
clusterIdx++;
idToCluster[ feat.id() ] = clusterIdx;
clusterCount++;
idToCluster[ feat.id() ] = clusterCount;
feedback->setProgress( ++i * step );

while ( !within.empty() )
Expand Down Expand Up @@ -290,7 +294,7 @@ void QgsDbscanClusteringAlgorithm::dbscan( const std::size_t minSize,
}
if ( !borderPointsAreNoise || within2.size() >= minSize )
{
idToCluster[ j.id ] = clusterIdx;
idToCluster[ j.id ] = clusterCount;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmdbscanclustering.h
Expand Up @@ -61,6 +61,7 @@ class ANALYSIS_EXPORT QgsDbscanClusteringAlgorithm : public QgsProcessingAlgorit
QgsFeatureIterator features,
QgsSpatialIndexKDBush &index,
std::unordered_map< QgsFeatureId, int> &idToCluster,
int &clusterCount,
QgsProcessingFeedback *feedback );
};

Expand Down

0 comments on commit d4d9186

Please sign in to comment.