20 #include <unordered_set>
24 QString QgsDbscanClusteringAlgorithm::name()
const
26 return QStringLiteral(
"dbscanclustering" );
29 QString QgsDbscanClusteringAlgorithm::displayName()
const
31 return QObject::tr(
"DBSCAN clustering" );
34 QString QgsDbscanClusteringAlgorithm::shortDescription()
const
36 return QObject::tr(
"Clusters point features using a density based scan algorithm." );
39 QStringList QgsDbscanClusteringAlgorithm::tags()
const
41 return QObject::tr(
"clustering,clusters,density,based,points,distance" ).split(
',' );
44 QString QgsDbscanClusteringAlgorithm::group()
const
46 return QObject::tr(
"Vector analysis" );
49 QString QgsDbscanClusteringAlgorithm::groupId()
const
51 return QStringLiteral(
"vectoranalysis" );
54 void QgsDbscanClusteringAlgorithm::initAlgorithm(
const QVariantMap & )
61 QObject::tr(
"Maximum distance between clustered points" ), 1, QStringLiteral(
"INPUT" ),
false, 0 ) );
63 auto dbscanStarParam = std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral(
"DBSCAN*" ),
64 QObject::tr(
"Treat border points as noise (DBSCAN*)" ),
false,
true );
66 addParameter( dbscanStarParam.release() );
68 auto fieldNameParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"FIELD_NAME" ),
69 QObject::tr(
"Cluster field name" ), QStringLiteral(
"CLUSTER_ID" ) );
71 addParameter( fieldNameParam.release() );
72 auto sizeFieldNameParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"SIZE_FIELD_NAME" ),
73 QObject::tr(
"Cluster size field name" ), QStringLiteral(
"CLUSTER_SIZE" ) );
75 addParameter( sizeFieldNameParam.release() );
82 QString QgsDbscanClusteringAlgorithm::shortHelpString()
const
84 return QObject::tr(
"Clusters point features based on a 2D implementation of Density-based spatial clustering of applications with noise (DBSCAN) algorithm.\n\n"
85 "The algorithm requires two parameters, a minimum cluster size (“minPts”), and the maximum distance allowed between clustered points (“eps”)." );
88 QgsDbscanClusteringAlgorithm *QgsDbscanClusteringAlgorithm::createInstance()
const
90 return new QgsDbscanClusteringAlgorithm();
93 struct KDBushDataEqualById
101 struct KDBushDataHashById
105 return std::hash< QgsFeatureId > {}( a.
id );
111 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
115 const std::size_t minSize =
static_cast< std::size_t
>( parameterAsInt( parameters, QStringLiteral(
"MIN_SIZE" ), context ) );
116 const double eps1 = parameterAsDouble( parameters, QStringLiteral(
"EPS" ), context );
117 const double eps2 = parameterAsDouble( parameters, QStringLiteral(
"EPS2" ), context );
118 const bool borderPointsAreNoise = parameterAsBoolean( parameters, QStringLiteral(
"DBSCAN*" ), context );
120 QgsFields outputFields = source->fields();
122 const QString clusterFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD_NAME" ), context );
124 const QString clusterSizeFieldName = parameterAsString( parameters, QStringLiteral(
"SIZE_FIELD_NAME" ), context );
125 newFields.
append(
QgsField( clusterSizeFieldName, QVariant::Int ) );
129 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, outputFields, source->wkbType(), source->sourceCrs() ) );
135 std::unordered_map< QgsFeatureId, QDateTime> idToDateTime;
136 const QString dateTimeFieldName = parameterAsString( parameters, QStringLiteral(
"DATETIME_FIELD" ), context );
137 int dateTimefieldIndex = -1;
138 if ( !dateTimeFieldName.isEmpty() )
140 dateTimefieldIndex = source->fields().lookupField( dateTimeFieldName );
141 if ( dateTimefieldIndex == -1 )
152 feedback->
pushInfo( QObject::tr(
"Building spatial index" ) );
156 if ( dateTimefieldIndex >= 0 )
157 idToDateTime[ feature.
id() ] = feature.
attributes().at( dateTimefieldIndex ).toDateTime();
162 return QVariantMap();
165 feedback->
pushInfo( QObject::tr(
"Analysing clusters" ) );
166 std::unordered_map< QgsFeatureId, int> idToCluster;
167 idToCluster.reserve( index.size() );
168 const long featureCount = source->featureCount();
170 stdbscan( minSize, eps1, eps2, borderPointsAreNoise, featureCount, features, index, idToCluster, idToDateTime, feedback );
173 std::unordered_map< int, int> clusterSize;
174 std::for_each( idToCluster.begin(), idToCluster.end(), [ &clusterSize ]( std::pair< QgsFeatureId, int > idCluster ) { clusterSize[ idCluster.second ]++; } );
177 const double writeStep = featureCount > 0 ? 10.0 / featureCount : 1;
178 features = source->getFeatures();
191 const auto cluster = idToCluster.find( feat.
id() );
192 if ( cluster != idToCluster.end() )
194 attr << cluster->second << clusterSize[ cluster->second ];
198 attr << QVariant() << QVariant();
206 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
207 outputs.insert( QStringLiteral(
"NUM_CLUSTERS" ),
static_cast< unsigned int >( clusterSize.size() ) );
211 void QgsDbscanClusteringAlgorithm::stdbscan(
const std::size_t minSize,
214 const bool borderPointsAreNoise,
215 const long featureCount,
218 std::unordered_map< QgsFeatureId, int> &idToCluster,
219 std::unordered_map< QgsFeatureId, QDateTime> &idToDateTime,
222 const double step = featureCount > 0 ? 90.0 / featureCount : 1;
224 std::unordered_set< QgsFeatureId > visited;
225 visited.reserve( index.
size() );
229 int clusterCount = 0;
244 if ( visited.find( feat.
id() ) != visited.end() )
261 if ( !idToDateTime.empty() && !idToDateTime[ feat.
id() ].isValid() )
269 std::unordered_set< QgsSpatialIndexKDBushData, KDBushDataHashById, KDBushDataEqualById> within;
275 if ( idToDateTime.empty() || ( idToDateTime[ data.id ].isValid() && std::abs( idToDateTime[ pointId ].msecsTo( idToDateTime[ data.id ] ) ) <= eps2 ) )
276 within.insert( data );
278 if ( within.size() < minSize )
281 visited.insert( feat.
id() );
291 idToCluster[ feat.
id() ] = clusterCount;
294 while ( !within.empty() )
302 within.erase( within.begin() );
304 if ( visited.find( j.
id ) != visited.end() )
310 visited.insert( j.
id );
316 std::unordered_set< QgsSpatialIndexKDBushData, KDBushDataHashById, KDBushDataEqualById > within2;
319 if ( idToDateTime.empty() || ( idToDateTime[ data.id ].isValid() && std::abs( idToDateTime[ point2Id ].msecsTo( idToDateTime[ data.id ] ) ) <= eps2 ) )
320 within2.insert( data );
323 if ( within2.size() >= minSize )
326 std::copy_if( within2.begin(),
328 std::inserter( within, within.end() ),
331 return visited.find( needle.id ) == visited.end();
334 if ( !borderPointsAreNoise || within2.size() >= minSize )
336 idToCluster[ j.
id ] = clusterCount;