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 );
Wrapper for iterator of features from vector data provider or vector layer.
Base class for providing feedback from a processing algorithm.
void setProgress(double progress)
Sets the current progress for the feedback object.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
A numeric output for processing algorithms.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QgsProject * project() const
Returns the project in which the algorithm is being executed.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
A spatial index for QgsFeature objects.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
bool isCanceled() const
Tells whether the operation has been canceled already.
An input feature source (such as vector layers) parameter for processing algorithms.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
bool nextFeature(QgsFeature &f)
Contains information about the context in which a processing algorithm is executed.
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
Indicates that the spatial index should also store feature geometries. This requires more memory...