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 "Output is generated as an HTML file with the computed statistical values." );
54 QString QgsNearestNeighbourAnalysisAlgorithm::svgIconPath()
const
59 QIcon QgsNearestNeighbourAnalysisAlgorithm::icon()
const
64 QgsNearestNeighbourAnalysisAlgorithm *QgsNearestNeighbourAnalysisAlgorithm::createInstance()
const
66 return new QgsNearestNeighbourAnalysisAlgorithm();
69 void QgsNearestNeighbourAnalysisAlgorithm::initAlgorithm(
const QVariantMap & )
73 QObject::tr(
"HTML files (*.html *.HTML)" ), QVariant(),
true ) );
83 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
87 QString outputFile = parameterAsFileOutput( parameters, QStringLiteral(
"OUTPUT_HTML_FILE" ), context );
94 double step = source->featureCount() ? 100.0 / source->featureCount() : 1;
100 double area = source->sourceExtent().width() * source->sourceExtent().height();
118 int count = source->featureCount() > 0 ? source->featureCount() : 1;
119 double observedDistance = sumDist / count;
120 double expectedDistance = 0.5 / std::sqrt( count / area );
121 double nnIndex = observedDistance / expectedDistance;
122 double se = 0.26136 / std::sqrt( std::pow( count, 2 ) / area );
123 double zScore = ( observedDistance - expectedDistance ) / se;
126 outputs.insert( QStringLiteral(
"OBSERVED_MD" ), observedDistance );
127 outputs.insert( QStringLiteral(
"EXPECTED_MD" ), expectedDistance );
128 outputs.insert( QStringLiteral(
"NN_INDEX" ), nnIndex );
129 outputs.insert( QStringLiteral(
"POINT_COUNT" ), count );
130 outputs.insert( QStringLiteral(
"Z_SCORE" ), zScore );
132 if ( !outputFile.isEmpty() )
134 QFile file( outputFile );
135 if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
137 QTextStream out( &file );
138 out << QStringLiteral(
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
139 out << QObject::tr(
"<p>Observed mean distance: %1</p>\n" ).arg( observedDistance, 0,
'f', 11 );
140 out << QObject::tr(
"<p>Expected mean distance: %1</p>\n" ).arg( expectedDistance, 0,
'f', 11 );
141 out << QObject::tr(
"<p>Nearest neighbour index: %1</p>\n" ).arg( nnIndex, 0,
'f', 11 );
142 out << QObject::tr(
"<p>Number of points: %1</p>\n" ).arg( count );
143 out << QObject::tr(
"<p>Z-Score: %1</p>\n" ).arg( zScore, 0,
'f', 11 );
144 out << QStringLiteral(
"</body></html>" );
146 outputs.insert( QStringLiteral(
"OUTPUT_HTML_FILE" ), outputFile );