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 & )
60 addParameter(
new QgsProcessingParameterEnum( u
"TYPE"_s, QObject::tr(
"Grid type" ), QStringList() << QObject::tr(
"Point" ) << QObject::tr(
"Line" ) << QObject::tr(
"Rectangle (Polygon)" ) << QObject::tr(
"Diamond (Polygon)" ) << QObject::tr(
"Hexagon (Polygon)" ),
false, 0 ) );
75QString QgsGridAlgorithm::shortHelpString()
const
77 return QObject::tr(
"This algorithm creates a vector layer with a grid covering a given extent. "
78 "Elements in the grid can be points, lines or polygons. The size and/or "
79 "placement of each element in the grid is defined using a horizontal and "
80 "vertical spacing. The CRS of the output layer must be defined. The grid extent "
81 "and the spacing values must be expressed in the coordinates and units of "
82 "this CRS. The top-left point (minX, maxY) is used as the reference point. "
83 "That means that, at that point, an element is guaranteed to be placed. "
84 "Unless the width and height of the selected extent is a multiple of the "
85 "selected spacing, that is not true for the other points that define that extent."
88QString QgsGridAlgorithm::shortDescription()
const
90 return QObject::tr(
"Creates a vector layer with a grid covering a given extent." );
93QgsGridAlgorithm *QgsGridAlgorithm::createInstance()
const
95 return new QgsGridAlgorithm();
100 mIdx = parameterAsEnum( parameters, u
"TYPE"_s, context );
101 mHSpacing = parameterAsDouble( parameters, u
"HSPACING"_s, context );
102 mVSpacing = parameterAsDouble( parameters, u
"VSPACING"_s, context );
103 mHOverlay = parameterAsDouble( parameters, u
"HOVERLAY"_s, context );
104 mVOverlay = parameterAsDouble( parameters, u
"VOVERLAY"_s, context );
105 mCrs = parameterAsCrs( parameters, u
"CRS"_s, context );
106 mGridExtent = parameterAsExtent( parameters, u
"EXTENT"_s, context, mCrs );
113 if ( mHSpacing <= 0 || mVSpacing <= 0 )
114 throw QgsProcessingException( QObject::tr(
"Invalid grid spacing. horizontal: '%1', vertical: '%2'" ).arg( mHSpacing ).arg( mVSpacing ) );
116 if ( mGridExtent.width() < mHSpacing )
119 if ( mGridExtent.height() < mVSpacing )
130 fields.
append(
QgsField( u
"bottom"_s, QMetaType::Type::Double ) );
137 fields.
append(
QgsField( u
"row_index"_s, QMetaType::Type::LongLong ) );
138 fields.
append(
QgsField( u
"col_index"_s, QMetaType::Type::LongLong ) );
157 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields, outputWkb, mCrs ) );
166 createPointGrid( sink, feedback );
169 createLineGrid( sink, feedback );
172 createRectangleGrid( sink, feedback );
175 createDiamondGrid( sink, feedback );
178 createHexagonGrid( sink, feedback );
184 outputs.insert( u
"OUTPUT"_s, dest );
188void QgsGridAlgorithm::createPointGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
192 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHSpacing - mHOverlay ) ) );
193 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
197 const long long cellcnt = rows * cols;
199 int thisProgress = 0;
200 int lastProgress = 0;
202 for (
long long col = 0; col < cols; col++ )
204 const double x = mGridExtent.xMinimum() + ( col * mHSpacing - col * mHOverlay );
206 for (
long long row = 0; row < rows; row++ )
208 const double y = mGridExtent.yMaximum() - ( row * mVSpacing - row * mVOverlay );
218 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
219 if ( feedback && thisProgress != lastProgress )
221 lastProgress = thisProgress;
233void QgsGridAlgorithm::createLineGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
240 hSpace[0] = mHSpacing - mHOverlay;
241 hSpace[1] = mHOverlay;
245 hSpace[0] = mHSpacing;
246 hSpace[1] = mHSpacing;
252 vSpace[0] = mVSpacing - mVOverlay;
253 vSpace[1] = mVOverlay;
257 vSpace[0] = mVSpacing;
258 vSpace[1] = mVSpacing;
265 double cntMax = mGridExtent.height() / mVSpacing;
267 int thisProgress = 0;
268 int lastProgress = 0;
270 double y = mGridExtent.yMaximum();
272 while ( y >= mGridExtent.yMinimum() )
284 y = y - vSpace[cnt % 2];
290 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) / cntMax ) * 50 );
291 if ( feedback && thisProgress != lastProgress )
293 lastProgress = thisProgress;
305 cntMax = mGridExtent.width() / mHSpacing;
309 double x = mGridExtent.xMinimum();
311 while ( x <= mGridExtent.xMaximum() )
322 x = x + hSpace[cnt % 2];
327 thisProgress =
static_cast<int>(
static_cast<double>( 50 ) + (
static_cast<double>( cnt ) / cntMax ) * 100 );
328 if ( feedback && thisProgress != lastProgress )
330 lastProgress = thisProgress;
338void QgsGridAlgorithm::createRectangleGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
342 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHSpacing - mHOverlay ) ) );
343 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
347 const long long cellcnt = rows * cols;
349 int thisProgress = 0;
350 int lastProgress = 0;
351 QVector<double> ringX( 5 );
352 QVector<double> ringY( 5 );
354 for (
long long col = 0; col < cols; col++ )
359 const double x1 = mGridExtent.xMinimum() + ( col * mHSpacing - col * mHOverlay );
360 const double x2 = x1 + mHSpacing;
362 for (
long long row = 0; row < rows; row++ )
364 const double y1 = mGridExtent.yMaximum() - ( row * mVSpacing - row * mVOverlay );
365 const double y2 = y1 - mVSpacing;
367 ringX = { x1, x2, x2, x1, x1 };
368 ringY = { y1, y1, y2, y2, y1 };
369 auto poly = std::make_unique<QgsPolygon>();
379 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
380 if ( feedback && thisProgress != lastProgress )
382 lastProgress = thisProgress;
392void QgsGridAlgorithm::createDiamondGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
396 const double halfHSpacing = mHSpacing / 2;
397 const double halfVSpacing = mVSpacing / 2;
399 const double halfHOverlay = mHOverlay / 2;
400 const double halfVOverlay = mVOverlay / 2;
402 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( halfHSpacing - halfHOverlay ) ) );
403 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - halfVOverlay ) ) );
407 const long long cellcnt = rows * cols;
409 int thisProgress = 0;
410 int lastProgress = 0;
411 QVector<double> ringX( 5 );
412 QVector<double> ringY( 5 );
414 for (
long long col = 0; col < cols; col++ )
419 const double x = mGridExtent.xMinimum() - ( col * halfHOverlay );
420 const double x1 = x + ( ( col + 0 ) * halfHSpacing );
421 const double x2 = x + ( ( col + 1 ) * halfHSpacing );
422 const double x3 = x + ( ( col + 2 ) * halfHSpacing );
424 for (
long long row = 0; row < rows; row++ )
426 const double y = mGridExtent.yMaximum() + ( row * halfVOverlay );
432 if ( ( col % 2 ) == 0 )
434 y1 = y - ( ( ( row * 2 ) + 0 ) * halfVSpacing );
435 y2 = y - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
436 y3 = y - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
440 y1 = y - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
441 y2 = y - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
442 y3 = y - ( ( ( row * 2 ) + 3 ) * halfVSpacing );
445 ringX = { x1, x2, x3, x2, x1 };
446 ringY = { y2, y1, y2, y3, y2 };
447 auto poly = std::make_unique<QgsPolygon>();
457 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
458 if ( feedback && thisProgress != lastProgress )
460 lastProgress = thisProgress;
470void QgsGridAlgorithm::createHexagonGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
475 const double xVertexLo = 0.288675134594813 * mVSpacing;
476 const double xVertexHi = 0.577350269189626 * mVSpacing;
478 mHSpacing = xVertexLo + xVertexHi;
480 mHOverlay = mHSpacing - mHOverlay;
484 throw QgsProcessingException( 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." ).arg( mHSpacing ).arg( mHOverlay ) );
487 const double halfVSpacing = mVSpacing / 2;
489 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHOverlay ) ) );
490 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
494 const long long cellcnt = rows * cols;
496 int thisProgress = 0;
497 int lastProgress = 0;
499 QVector<double> ringX( 7 );
500 QVector<double> ringY( 7 );
501 for (
long long col = 0; col < cols; col++ )
510 const double x1 = mGridExtent.xMinimum() + ( col * mHOverlay );
511 const double x2 = x1 + ( xVertexHi - xVertexLo );
512 const double x3 = mGridExtent.xMinimum() + ( col * mHOverlay ) + mHSpacing;
513 const double x4 = x3 + ( xVertexHi - xVertexLo );
515 for (
long long row = 0; row < rows; row++ )
521 if ( ( col % 2 ) == 0 )
523 y1 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 0 ) * halfVSpacing );
524 y2 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
525 y3 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
529 y1 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
530 y2 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
531 y3 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 3 ) * halfVSpacing );
534 ringX = { x1, x2, x3, x4, x3, x2, x1 };
535 ringY = { y2, y1, y1, y2, y3, y3, y2 };
536 auto poly = std::make_unique<QgsPolygon>();
546 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
547 if ( feedback && thisProgress != lastProgress )
549 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.