QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsalgorithmrandompointsextent.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmrandompointsextent.cpp
3  ---------------------
4  begin : November 2019
5  copyright : (C) 2019 by Clemens Raffler
6  email : clemens dot raffler at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 //Disclaimer: The algorithm optimizes the original Random points in extent algorithm, (C) Alexander Bruy, 2014
19 
21 #include "random"
22 
24 
25 QString QgsRandomPointsExtentAlgorithm::name() const
26 {
27  return QStringLiteral( "randompointsinextent" );
28 }
29 
30 QString QgsRandomPointsExtentAlgorithm::displayName() const
31 {
32  return QObject::tr( "Random points in extent" );
33 }
34 
35 QStringList QgsRandomPointsExtentAlgorithm::tags() const
36 {
37  return QObject::tr( "random,points,extent,create" ).split( ',' );
38 }
39 
40 QString QgsRandomPointsExtentAlgorithm::group() const
41 {
42  return QObject::tr( "Vector creation" );
43 }
44 
45 QString QgsRandomPointsExtentAlgorithm::groupId() const
46 {
47  return QStringLiteral( "vectorcreation" );
48 }
49 
50 void QgsRandomPointsExtentAlgorithm::initAlgorithm( const QVariantMap & )
51 {
52 
53  addParameter( new QgsProcessingParameterExtent( QStringLiteral( "EXTENT" ), QObject::tr( "Input extent" ) ) );
54  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "POINTS_NUMBER" ), QObject::tr( "Number of points" ), QgsProcessingParameterNumber::Integer, 1, false, 1 ) );
55  addParameter( new QgsProcessingParameterDistance( QStringLiteral( "MIN_DISTANCE" ), QObject::tr( "Minimum distance between points" ), 0, QStringLiteral( "TARGET_CRS" ), true, 0 ) );
56  addParameter( new QgsProcessingParameterCrs( QStringLiteral( "TARGET_CRS" ), QObject::tr( "Target CRS" ), QStringLiteral( "ProjectCrs" ), false ) );
57 
58  std::unique_ptr< QgsProcessingParameterNumber > maxAttempts_param = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "MAX_ATTEMPTS" ), QObject::tr( "Maximum number of search attempts given the minimum distance" ), QgsProcessingParameterNumber::Integer, 200, true, 1 );
59  maxAttempts_param->setFlags( maxAttempts_param->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
60  addParameter( maxAttempts_param.release() );
61 
62  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Random points" ), QgsProcessing::TypeVectorPoint ) );
63 }
64 
65 QString QgsRandomPointsExtentAlgorithm::shortHelpString() const
66 {
67  return QObject::tr( "This algorithm creates a new point layer with a given "
68  "number of random points, all of them within a given extent. "
69  "A distance factor can be specified, to avoid points being "
70  "too close to each other. If the minimum distance between points "
71  "makes it impossible to create new points, either "
72  "distance can be decreased or the maximum number of attempts may be "
73  "increased."
74  );
75 }
76 
77 QgsRandomPointsExtentAlgorithm *QgsRandomPointsExtentAlgorithm::createInstance() const
78 {
79  return new QgsRandomPointsExtentAlgorithm();
80 }
81 
82 bool QgsRandomPointsExtentAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
83 {
84  mCrs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );
85  mExtent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context, mCrs );
86  mNumPoints = parameterAsInt( parameters, QStringLiteral( "POINTS_NUMBER" ), context );
87  mDistance = parameterAsDouble( parameters, QStringLiteral( "MIN_DISTANCE" ), context );
88  mMaxAttempts = parameterAsInt( parameters, QStringLiteral( "MAX_ATTEMPTS" ), context );
89 
90  return true;
91 }
92 
93 QVariantMap QgsRandomPointsExtentAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
94 {
95 
96  QgsFields fields = QgsFields();
97  fields.append( QgsField( QStringLiteral( "id" ), QVariant::LongLong ) );
98 
99  QString dest;
100  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::Point, mCrs ) );
101  if ( !sink )
102  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
103 
104  //initialize random engine
105  std::random_device random_device;
106  std::mt19937 mersenne_twister( random_device() );
107 
108  std::uniform_real_distribution<double> x_distribution( mExtent.xMinimum(), mExtent.xMaximum() );
109  std::uniform_real_distribution<double> y_distribution( mExtent.yMinimum(), mExtent.yMaximum() );
110 
111  if ( mDistance == 0 )
112  {
113  int i = 0;
114  while ( i < mNumPoints )
115  {
116  if ( feedback->isCanceled() )
117  break;
118 
119  double rx = x_distribution( random_device );
120  double ry = y_distribution( random_device );
121 
122  QgsFeature f = QgsFeature( i );
123 
124  f.setGeometry( QgsGeometry( new QgsPoint( rx, ry ) ) );
125  f.setAttributes( QgsAttributes() << i );
126  sink->addFeature( f, QgsFeatureSink::FastInsert );
127  i++;
128  feedback->setProgress( static_cast<int>( static_cast<double>( i ) / static_cast<double>( mNumPoints ) * 100 ) );
129  }
130  }
131  else
132  {
134  int distCheckIterations = 0;
135 
136  int i = 0;
137  while ( i < mNumPoints )
138  {
139  if ( feedback->isCanceled() )
140  break;
141 
142  double rx = x_distribution( random_device );
143  double ry = y_distribution( random_device );
144 
145  //check if new random point is inside searching distance to existing points
146  QList<QgsFeatureId> neighbors = index.nearestNeighbor( QgsPointXY( rx, ry ), 1, mDistance );
147  if ( neighbors.empty() )
148  {
149  QgsFeature f = QgsFeature( i );
150  f.setAttributes( QgsAttributes() << i );
151  QgsGeometry randomPointGeom = QgsGeometry( new QgsPoint( rx, ry ) );
152  f.setGeometry( randomPointGeom );
153  index.addFeature( f );
154  sink->addFeature( f, QgsFeatureSink::FastInsert );
155  i++;
156  distCheckIterations = 0; //reset distCheckIterations if a point is added
157  feedback->setProgress( static_cast<int>( static_cast<double>( i ) / static_cast<double>( mNumPoints ) * 100 ) );
158  }
159  else
160  {
161  if ( distCheckIterations == mMaxAttempts )
162  {
163  throw QgsProcessingException( QObject::tr( "%1 of %2 points have been successfully created, but no more random points could be found "
164  "due to the given minimum distance between points. Either choose a larger extent, "
165  "lower the minimum distance between points or try increasing the number "
166  "of attempts for searching new points." ).arg( i ).arg( mNumPoints ) );
167  }
168  else
169  {
170  distCheckIterations++;
171  continue; //retry with new point
172  }
173 
174  }
175  }
176  }
177 
178  QVariantMap outputs;
179  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
180 
181  return outputs;
182 }
183 
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:75
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:71
QgsProcessingParameterNumber
Definition: qgsprocessingparameters.h:1838
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsFields
Definition: qgsfields.h:44
QgsProcessingParameterDefinition::FlagAdvanced
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition: qgsprocessingparameters.h:419
QgsFields::append
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
QgsProcessing::TypeVectorPoint
@ TypeVectorPoint
Vector point layers.
Definition: qgsprocessing.h:48
QgsProcessingParameterFeatureSink
Definition: qgsprocessingparameters.h:2773
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingParameterCrs
Definition: qgsprocessingparameters.h:1470
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsProcessingParameterExtent
Definition: qgsprocessingparameters.h:1502
QgsSpatialIndex
Definition: qgsspatialindex.h:67
QgsProcessingParameterNumber::Integer
@ Integer
Integer values.
Definition: qgsprocessingparameters.h:1845
qgsalgorithmrandompointsextent.h
QgsProcessingParameterDistance
Definition: qgsprocessingparameters.h:1941
QgsPointXY
Definition: qgspointxy.h:43
QgsFeedback::isCanceled
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:66
QgsSpatialIndex::nearestNeighbor
QList< QgsFeatureId > nearestNeighbor(const QgsPointXY &point, int neighbors=1, double maxDistance=0) const
Returns nearest neighbors to a point.
Definition: qgsspatialindex.cpp:501
QgsGeometry
Definition: qgsgeometry.h:122
QgsAttributes
Definition: qgsattributes.h:57
QgsFeature
Definition: qgsfeature.h:55
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:127
QgsSpatialIndex::addFeature
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a feature to the index.
Definition: qgsspatialindex.cpp:403
QgsProcessingException
Definition: qgsexception.h:82
QgsFeatureSink::FastInsert
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Definition: qgsfeaturesink.h:70
QgsField
Definition: qgsfield.h:49