27QString QgsRandomPointsExtentAlgorithm::name()
 const 
   29  return QStringLiteral( 
"randompointsinextent" );
 
   32QString QgsRandomPointsExtentAlgorithm::displayName()
 const 
   34  return QObject::tr( 
"Random points in extent" );
 
   37QStringList QgsRandomPointsExtentAlgorithm::tags()
 const 
   39  return QObject::tr( 
"random,points,extent,create" ).split( 
',' );
 
   42QString QgsRandomPointsExtentAlgorithm::group()
 const 
   44  return QObject::tr( 
"Vector creation" );
 
   47QString QgsRandomPointsExtentAlgorithm::groupId()
 const 
   49  return QStringLiteral( 
"vectorcreation" );
 
   52void QgsRandomPointsExtentAlgorithm::initAlgorithm( 
const QVariantMap & )
 
   56  addParameter( 
new QgsProcessingParameterDistance( QStringLiteral( 
"MIN_DISTANCE" ), QObject::tr( 
"Minimum distance between points" ), 0, QStringLiteral( 
"TARGET_CRS" ), 
true, 0 ) );
 
   57  addParameter( 
new QgsProcessingParameterCrs( QStringLiteral( 
"TARGET_CRS" ), QObject::tr( 
"Target CRS" ), QStringLiteral( 
"ProjectCrs" ), 
false ) );
 
   59  auto maxAttempts_param = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( 
"MAX_ATTEMPTS" ), QObject::tr( 
"Maximum number of search attempts given the minimum distance" ), 
Qgis::ProcessingNumberParameterType::Integer, 200, 
true, 1 );
 
   61  addParameter( maxAttempts_param.release() );
 
   66QString QgsRandomPointsExtentAlgorithm::shortHelpString()
 const 
   68  return QObject::tr( 
"This algorithm creates a new point layer with a given " 
   69                      "number of random points, all of them within a given extent. " 
   70                      "A distance factor can be specified, to avoid points being " 
   71                      "too close to each other. If the minimum distance between points " 
   72                      "makes it impossible to create new points, either " 
   73                      "distance can be decreased or the maximum number of attempts may be " 
   78QString QgsRandomPointsExtentAlgorithm::shortDescription()
 const 
   80  return QObject::tr( 
"Creates a point layer with a given number of random points, all of them within a given extent." );
 
   83QgsRandomPointsExtentAlgorithm *QgsRandomPointsExtentAlgorithm::createInstance()
 const 
   85  return new QgsRandomPointsExtentAlgorithm();
 
   90  mCrs = parameterAsCrs( parameters, QStringLiteral( 
"TARGET_CRS" ), context );
 
   91  mExtent = parameterAsExtent( parameters, QStringLiteral( 
"EXTENT" ), context, mCrs );
 
   92  mNumPoints = parameterAsInt( parameters, QStringLiteral( 
"POINTS_NUMBER" ), context );
 
   93  mDistance = parameterAsDouble( parameters, QStringLiteral( 
"MIN_DISTANCE" ), context );
 
   94  mMaxAttempts = parameterAsInt( parameters, QStringLiteral( 
"MAX_ATTEMPTS" ), context );
 
  102  fields.
append( 
QgsField( QStringLiteral( 
"id" ), QMetaType::Type::LongLong ) );
 
  105  std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( 
"OUTPUT" ), context, dest, fields, 
Qgis::WkbType::Point, mCrs ) );
 
  110  std::random_device random_device;
 
  111  const std::mt19937 mersenne_twister( random_device() );
 
  113  std::uniform_real_distribution<double> x_distribution( mExtent.xMinimum(), mExtent.xMaximum() );
 
  114  std::uniform_real_distribution<double> y_distribution( mExtent.yMinimum(), mExtent.yMaximum() );
 
  116  if ( mDistance == 0 )
 
  119    while ( i < mNumPoints )
 
  124      const double rx = x_distribution( random_device );
 
  125      const double ry = y_distribution( random_device );
 
  134      feedback->
setProgress( 
static_cast<int>( 
static_cast<double>( i ) / 
static_cast<double>( mNumPoints ) * 100 ) );
 
  140    int distCheckIterations = 0;
 
  143    while ( i < mNumPoints )
 
  148      const double rx = x_distribution( random_device );
 
  149      const double ry = y_distribution( random_device );
 
  153      if ( neighbors.empty() )
 
  162        distCheckIterations = 0; 
 
  163        feedback->
setProgress( 
static_cast<int>( 
static_cast<double>( i ) / 
static_cast<double>( mNumPoints ) * 100 ) );
 
  167        if ( distCheckIterations == mMaxAttempts )
 
  169          throw QgsProcessingException( QObject::tr( 
"%1 of %2 points have been successfully created, but no more random points could be found " 
  170                                                     "due to the given minimum distance between points. Either choose a larger extent, " 
  171                                                     "lower the minimum distance between points or try increasing the number " 
  172                                                     "of attempts for searching new points." )
 
  174                                          .arg( mNumPoints ) );
 
  178          distCheckIterations++;
 
  188  outputs.insert( QStringLiteral( 
"OUTPUT" ), dest );
 
@ VectorPoint
Vector point layers.
 
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
 
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
 
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
 
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
 
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
 
bool isCanceled() const
Tells whether the operation has been canceled already.
 
void setProgress(double progress)
Sets the current progress for the feedback object.
 
Encapsulate a field in an attribute table or data source.
 
Container of fields for a vector layer.
 
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
 
A geometry is the spatial representation of a feature.
 
Point geometry type, with support for z-dimension and m-values.
 
Contains information about the context in which a processing algorithm is executed.
 
Custom exception class for processing related exceptions.
 
Base class for providing feedback from a processing algorithm.
 
A coordinate reference system parameter for processing algorithms.
 
A double numeric parameter for distance values.
 
A rectangular map extent parameter for processing algorithms.
 
A feature sink output for processing algorithms.
 
A numeric parameter for processing algorithms.
 
A spatial index for QgsFeature objects.
 
QList< QgsFeatureId > nearestNeighbor(const QgsPointXY &point, int neighbors=1, double maxDistance=0) const
Returns nearest neighbors to a point.
 
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a feature to the index.