29using namespace Qt::StringLiterals;
33QString QgsGridAlgorithm::name()
const
35 return u
"creategrid"_s;
38QString QgsGridAlgorithm::displayName()
const
40 return QObject::tr(
"Create grid" );
43QStringList QgsGridAlgorithm::tags()
const
45 return QObject::tr(
"grid,lines,polygons,vector,create,fishnet,diamond,hexagon" ).split(
',' );
48QString QgsGridAlgorithm::group()
const
50 return QObject::tr(
"Vector creation" );
53QString QgsGridAlgorithm::groupId()
const
55 return u
"vectorcreation"_s;
58void QgsGridAlgorithm::initAlgorithm(
const QVariantMap & )
62 QObject::tr(
"Grid type" ),
63 QStringList() << QObject::tr(
"Point" ) << QObject::tr(
"Line" ) << QObject::tr(
"Rectangle (Polygon)" ) << QObject::tr(
"Diamond (Polygon)" ) << QObject::tr(
"Hexagon (Polygon)" ),
81QString QgsGridAlgorithm::shortHelpString()
const
84 "This algorithm creates a vector layer with a grid covering a given extent. "
85 "Elements in the grid can be points, lines or polygons. The size and/or "
86 "placement of each element in the grid is defined using a horizontal and "
87 "vertical spacing. The CRS of the output layer must be defined. The grid extent "
88 "and the spacing values must be expressed in the coordinates and units of "
89 "this CRS. The top-left point (minX, maxY) is used as the reference point. "
90 "That means that, at that point, an element is guaranteed to be placed. "
91 "Unless the width and height of the selected extent is a multiple of the "
92 "selected spacing, that is not true for the other points that define that extent."
95QString QgsGridAlgorithm::shortDescription()
const
97 return QObject::tr(
"Creates a vector layer with a grid covering a given extent." );
100QgsGridAlgorithm *QgsGridAlgorithm::createInstance()
const
102 return new QgsGridAlgorithm();
107 mIdx = parameterAsEnum( parameters, u
"TYPE"_s, context );
108 mHSpacing = parameterAsDouble( parameters, u
"HSPACING"_s, context );
109 mVSpacing = parameterAsDouble( parameters, u
"VSPACING"_s, context );
110 mHOverlay = parameterAsDouble( parameters, u
"HOVERLAY"_s, context );
111 mVOverlay = parameterAsDouble( parameters, u
"VOVERLAY"_s, context );
112 mCrs = parameterAsCrs( parameters, u
"CRS"_s, context );
113 mGridExtent = parameterAsExtent( parameters, u
"EXTENT"_s, context, mCrs );
120 if ( mHSpacing <= 0 || mVSpacing <= 0 )
121 throw QgsProcessingException( QObject::tr(
"Invalid grid spacing. horizontal: '%1', vertical: '%2'" ).arg( mHSpacing ).arg( mVSpacing ) );
123 if ( mGridExtent.width() < mHSpacing )
126 if ( mGridExtent.height() < mVSpacing )
137 fields.
append(
QgsField( u
"bottom"_s, QMetaType::Type::Double ) );
144 fields.
append(
QgsField( u
"row_index"_s, QMetaType::Type::LongLong ) );
145 fields.
append(
QgsField( u
"col_index"_s, QMetaType::Type::LongLong ) );
164 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields, outputWkb, mCrs ) );
173 createPointGrid( sink, feedback );
176 createLineGrid( sink, feedback );
179 createRectangleGrid( sink, feedback );
182 createDiamondGrid( sink, feedback );
185 createHexagonGrid( sink, feedback );
191 outputs.insert( u
"OUTPUT"_s, dest );
195void QgsGridAlgorithm::createPointGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
199 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHSpacing - mHOverlay ) ) );
200 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
204 const long long cellcnt = rows * cols;
206 int thisProgress = 0;
207 int lastProgress = 0;
209 for (
long long col = 0; col < cols; col++ )
211 const double x = mGridExtent.xMinimum() + ( col * mHSpacing - col * mHOverlay );
213 for (
long long row = 0; row < rows; row++ )
215 const double y = mGridExtent.yMaximum() - ( row * mVSpacing - row * mVOverlay );
225 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
226 if ( feedback && thisProgress != lastProgress )
228 lastProgress = thisProgress;
240void QgsGridAlgorithm::createLineGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
247 hSpace[0] = mHSpacing - mHOverlay;
248 hSpace[1] = mHOverlay;
252 hSpace[0] = mHSpacing;
253 hSpace[1] = mHSpacing;
259 vSpace[0] = mVSpacing - mVOverlay;
260 vSpace[1] = mVOverlay;
264 vSpace[0] = mVSpacing;
265 vSpace[1] = mVSpacing;
272 double cntMax = mGridExtent.height() / mVSpacing;
274 int thisProgress = 0;
275 int lastProgress = 0;
277 double y = mGridExtent.yMaximum();
279 while ( y >= mGridExtent.yMinimum() )
291 y = y - vSpace[cnt % 2];
297 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) / cntMax ) * 50 );
298 if ( feedback && thisProgress != lastProgress )
300 lastProgress = thisProgress;
312 cntMax = mGridExtent.width() / mHSpacing;
316 double x = mGridExtent.xMinimum();
318 while ( x <= mGridExtent.xMaximum() )
329 x = x + hSpace[cnt % 2];
334 thisProgress =
static_cast<int>(
static_cast<double>( 50 ) + (
static_cast<double>( cnt ) / cntMax ) * 100 );
335 if ( feedback && thisProgress != lastProgress )
337 lastProgress = thisProgress;
345void QgsGridAlgorithm::createRectangleGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
349 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHSpacing - mHOverlay ) ) );
350 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
354 const long long cellcnt = rows * cols;
356 int thisProgress = 0;
357 int lastProgress = 0;
358 QVector<double> ringX( 5 );
359 QVector<double> ringY( 5 );
361 for (
long long col = 0; col < cols; col++ )
366 const double x1 = mGridExtent.xMinimum() + ( col * mHSpacing - col * mHOverlay );
367 const double x2 = x1 + mHSpacing;
369 for (
long long row = 0; row < rows; row++ )
371 const double y1 = mGridExtent.yMaximum() - ( row * mVSpacing - row * mVOverlay );
372 const double y2 = y1 - mVSpacing;
374 ringX = { x1, x2, x2, x1, x1 };
375 ringY = { y1, y1, y2, y2, y1 };
376 auto poly = std::make_unique<QgsPolygon>();
386 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
387 if ( feedback && thisProgress != lastProgress )
389 lastProgress = thisProgress;
399void QgsGridAlgorithm::createDiamondGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
403 const double halfHSpacing = mHSpacing / 2;
404 const double halfVSpacing = mVSpacing / 2;
406 const double halfHOverlay = mHOverlay / 2;
407 const double halfVOverlay = mVOverlay / 2;
409 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( halfHSpacing - halfHOverlay ) ) );
410 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - halfVOverlay ) ) );
414 const long long cellcnt = rows * cols;
416 int thisProgress = 0;
417 int lastProgress = 0;
418 QVector<double> ringX( 5 );
419 QVector<double> ringY( 5 );
421 for (
long long col = 0; col < cols; col++ )
426 const double x = mGridExtent.xMinimum() - ( col * halfHOverlay );
427 const double x1 = x + ( ( col + 0 ) * halfHSpacing );
428 const double x2 = x + ( ( col + 1 ) * halfHSpacing );
429 const double x3 = x + ( ( col + 2 ) * halfHSpacing );
431 for (
long long row = 0; row < rows; row++ )
433 const double y = mGridExtent.yMaximum() + ( row * halfVOverlay );
439 if ( ( col % 2 ) == 0 )
441 y1 = y - ( ( ( row * 2 ) + 0 ) * halfVSpacing );
442 y2 = y - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
443 y3 = y - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
447 y1 = y - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
448 y2 = y - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
449 y3 = y - ( ( ( row * 2 ) + 3 ) * halfVSpacing );
452 ringX = { x1, x2, x3, x2, x1 };
453 ringY = { y2, y1, y2, y3, y2 };
454 auto poly = std::make_unique<QgsPolygon>();
464 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
465 if ( feedback && thisProgress != lastProgress )
467 lastProgress = thisProgress;
477void QgsGridAlgorithm::createHexagonGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
482 const double xVertexLo = 0.288675134594813 * mVSpacing;
483 const double xVertexHi = 0.577350269189626 * mVSpacing;
485 mHSpacing = xVertexLo + xVertexHi;
487 mHOverlay = mHSpacing - mHOverlay;
492 QObject::tr(
"To preserve symmetry, hspacing is fixed relative to vspacing\n hspacing is fixed at: %1 and hoverlay is fixed at: %2 hoverlay cannot be negative. Increase hoverlay." )
498 const double halfVSpacing = mVSpacing / 2;
500 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHOverlay ) ) );
501 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
505 const long long cellcnt = rows * cols;
507 int thisProgress = 0;
508 int lastProgress = 0;
510 QVector<double> ringX( 7 );
511 QVector<double> ringY( 7 );
512 for (
long long col = 0; col < cols; col++ )
521 const double x1 = mGridExtent.xMinimum() + ( col * mHOverlay );
522 const double x2 = x1 + ( xVertexHi - xVertexLo );
523 const double x3 = mGridExtent.xMinimum() + ( col * mHOverlay ) + mHSpacing;
524 const double x4 = x3 + ( xVertexHi - xVertexLo );
526 for (
long long row = 0; row < rows; row++ )
532 if ( ( col % 2 ) == 0 )
534 y1 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 0 ) * halfVSpacing );
535 y2 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
536 y3 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
540 y1 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
541 y2 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
542 y3 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 3 ) * halfVSpacing );
545 ringX = { x1, x2, x3, x4, x3, x2, x1 };
546 ringY = { y2, y1, y1, y2, y3, y3, y2 };
547 auto poly = std::make_unique<QgsPolygon>();
557 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
558 if ( feedback && thisProgress != lastProgress )
560 lastProgress = thisProgress;
@ VectorAnyGeometry
Any vector layer with geometry.
WkbType
The WKB type describes the number of dimensions a geometry has.
@ 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.
Line string geometry type, with support for z-dimension and m-values.
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.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A rectangular map extent parameter for processing algorithms.
A feature sink output for processing algorithms.