26QString QgsNearestNeighbourAnalysisAlgorithm::name()
 const 
   28  return QStringLiteral( 
"nearestneighbouranalysis" );
 
   31QString QgsNearestNeighbourAnalysisAlgorithm::displayName()
 const 
   33  return QObject::tr( 
"Nearest neighbour analysis" );
 
   36QStringList QgsNearestNeighbourAnalysisAlgorithm::tags()
 const 
   38  return QObject::tr( 
"point,node,vertex,nearest,neighbour,distance" ).split( 
',' );
 
   41QString QgsNearestNeighbourAnalysisAlgorithm::group()
 const 
   43  return QObject::tr( 
"Vector analysis" );
 
   46QString QgsNearestNeighbourAnalysisAlgorithm::groupId()
 const 
   48  return QStringLiteral( 
"vectoranalysis" );
 
   51QString QgsNearestNeighbourAnalysisAlgorithm::shortHelpString()
 const 
   53  return QObject::tr( 
"This algorithm performs nearest neighbor analysis for a point layer.\n\n" 
   54                      "The output describes how the data are distributed (clustered, randomly or distributed).\n\n" 
   55                      "Output is generated as an HTML file with the computed statistical values." );
 
   58QString QgsNearestNeighbourAnalysisAlgorithm::shortDescription()
 const 
   60  return QObject::tr( 
"Performs nearest neighbor analysis for a point layer." );
 
   63QString QgsNearestNeighbourAnalysisAlgorithm::svgIconPath()
 const 
   68QIcon QgsNearestNeighbourAnalysisAlgorithm::icon()
 const 
   73QgsNearestNeighbourAnalysisAlgorithm *QgsNearestNeighbourAnalysisAlgorithm::createInstance()
 const 
   75  return new QgsNearestNeighbourAnalysisAlgorithm();
 
   78void QgsNearestNeighbourAnalysisAlgorithm::initAlgorithm( 
const QVariantMap & )
 
   81  addParameter( 
new QgsProcessingParameterFileDestination( QStringLiteral( 
"OUTPUT_HTML_FILE" ), QObject::tr( 
"Nearest neighbour" ), QObject::tr( 
"HTML files (*.html *.HTML)" ), QVariant(), 
true ) );
 
   91  std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( 
"INPUT" ), context ) );
 
   95  const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( 
"OUTPUT_HTML_FILE" ), context );
 
  102  const double step = source->featureCount() ? 100.0 / source->featureCount() : 1;
 
  107  double sumDist = 0.0;
 
  108  const double area = source->sourceExtent().width() * source->sourceExtent().height();
 
  133  const long long count = source->featureCount() > 0 ? source->featureCount() : 1;
 
  134  const double observedDistance = sumDist / count;
 
  135  const double expectedDistance = 0.5 / std::sqrt( count / area );
 
  136  const double nnIndex = observedDistance / expectedDistance;
 
  137  const double se = 0.26136 / std::sqrt( std::pow( count, 2 ) / area );
 
  138  const double zScore = ( observedDistance - expectedDistance ) / se;
 
  141  outputs.insert( QStringLiteral( 
"OBSERVED_MD" ), observedDistance );
 
  142  outputs.insert( QStringLiteral( 
"EXPECTED_MD" ), expectedDistance );
 
  143  outputs.insert( QStringLiteral( 
"NN_INDEX" ), nnIndex );
 
  144  outputs.insert( QStringLiteral( 
"POINT_COUNT" ), count );
 
  145  outputs.insert( QStringLiteral( 
"Z_SCORE" ), zScore );
 
  147  if ( !outputFile.isEmpty() )
 
  149    QFile file( outputFile );
 
  150    if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
 
  152      QTextStream out( &file );
 
  153#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) 
  154      out.setCodec( 
"UTF-8" );
 
  156      out << QStringLiteral( 
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
 
  157      out << QObject::tr( 
"<p>Observed mean distance: %1</p>\n" ).arg( observedDistance, 0, 
'f', 11 );
 
  158      out << QObject::tr( 
"<p>Expected mean distance: %1</p>\n" ).arg( expectedDistance, 0, 
'f', 11 );
 
  159      out << QObject::tr( 
"<p>Nearest neighbour index: %1</p>\n" ).arg( nnIndex, 0, 
'f', 11 );
 
  160      out << QObject::tr( 
"<p>Number of points: %1</p>\n" ).arg( count );
 
  161      out << QObject::tr( 
"<p>Z-Score: %1</p>\n" ).arg( zScore, 0, 
'f', 11 );
 
  162      out << QStringLiteral( 
"</body></html>" );
 
  164      outputs.insert( QStringLiteral( 
"OUTPUT_HTML_FILE" ), outputFile );
 
@ VectorPoint
Vector point layers.
 
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
 
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
 
Custom exception class for Coordinate Reference System related exceptions.
 
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
 
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
 
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
 
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
 
Wrapper for iterator of features from vector data provider or vector layer.
 
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
 
Wraps a request for features to a vector layer (or directly its vector data provider).
 
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
 
bool isCanceled() const
Tells whether the operation has been canceled already.
 
void setProgress(double progress)
Sets the current progress for the feedback object.
 
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
 
Contains information about the context in which a processing algorithm is executed.
 
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
 
QString ellipsoid() const
Returns the ellipsoid to use for distance and area calculations.
 
Custom exception class for processing related exceptions.
 
Base class for providing feedback from a processing algorithm.
 
A numeric output for processing algorithms.
 
An input feature source (such as vector layers) parameter for processing algorithms.
 
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
 
A spatial index for QgsFeature objects.
 
@ FlagStoreFeatureGeometries
Indicates that the spatial index should also store feature geometries. This requires more memory,...
 
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features