23 QString QgsNearestNeighbourAnalysisAlgorithm::name()
const
25 return QStringLiteral(
"nearestneighbouranalysis" );
28 QString QgsNearestNeighbourAnalysisAlgorithm::displayName()
const
30 return QObject::tr(
"Nearest neighbour analysis" );
33 QStringList QgsNearestNeighbourAnalysisAlgorithm::tags()
const
35 return QObject::tr(
"point,node,vertex,nearest,neighbour,distance" ).split(
',' );
38 QString QgsNearestNeighbourAnalysisAlgorithm::group()
const
40 return QObject::tr(
"Vector analysis" );
43 QString QgsNearestNeighbourAnalysisAlgorithm::groupId()
const
45 return QStringLiteral(
"vectoranalysis" );
48 QString QgsNearestNeighbourAnalysisAlgorithm::shortHelpString()
const
50 return QObject::tr(
"This algorithm performs nearest neighbor analysis for a point layer.\n\n"
51 "The output describes how the data are distributed (clustered, randomly or distributed).\n\n"
52 "Output is generated as an HTML file with the computed statistical values." );
55 QString QgsNearestNeighbourAnalysisAlgorithm::svgIconPath()
const
60 QIcon QgsNearestNeighbourAnalysisAlgorithm::icon()
const
65 QgsNearestNeighbourAnalysisAlgorithm *QgsNearestNeighbourAnalysisAlgorithm::createInstance()
const
67 return new QgsNearestNeighbourAnalysisAlgorithm();
70 void QgsNearestNeighbourAnalysisAlgorithm::initAlgorithm(
const QVariantMap & )
74 QObject::tr(
"HTML files (*.html *.HTML)" ), QVariant(),
true ) );
84 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
88 QString outputFile = parameterAsFileOutput( parameters, QStringLiteral(
"OUTPUT_HTML_FILE" ), context );
95 double step = source->featureCount() ? 100.0 / source->featureCount() : 1;
100 double sumDist = 0.0;
101 double area = source->sourceExtent().width() * source->sourceExtent().height();
119 int count = source->featureCount() > 0 ? source->featureCount() : 1;
120 double observedDistance = sumDist / count;
121 double expectedDistance = 0.5 / std::sqrt( count / area );
122 double nnIndex = observedDistance / expectedDistance;
123 double se = 0.26136 / std::sqrt( std::pow( count, 2 ) / area );
124 double zScore = ( observedDistance - expectedDistance ) / se;
127 outputs.insert( QStringLiteral(
"OBSERVED_MD" ), observedDistance );
128 outputs.insert( QStringLiteral(
"EXPECTED_MD" ), expectedDistance );
129 outputs.insert( QStringLiteral(
"NN_INDEX" ), nnIndex );
130 outputs.insert( QStringLiteral(
"POINT_COUNT" ), count );
131 outputs.insert( QStringLiteral(
"Z_SCORE" ), zScore );
133 if ( !outputFile.isEmpty() )
135 QFile file( outputFile );
136 if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
138 QTextStream out( &file );
139 out << QStringLiteral(
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
140 out << QObject::tr(
"<p>Observed mean distance: %1</p>\n" ).arg( observedDistance, 0,
'f', 11 );
141 out << QObject::tr(
"<p>Expected mean distance: %1</p>\n" ).arg( expectedDistance, 0,
'f', 11 );
142 out << QObject::tr(
"<p>Nearest neighbour index: %1</p>\n" ).arg( nnIndex, 0,
'f', 11 );
143 out << QObject::tr(
"<p>Number of points: %1</p>\n" ).arg( count );
144 out << QObject::tr(
"<p>Z-Score: %1</p>\n" ).arg( zScore, 0,
'f', 11 );
145 out << QStringLiteral(
"</body></html>" );
147 outputs.insert( QStringLiteral(
"OUTPUT_HTML_FILE" ), outputFile );