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 );
192 outputs.insert( u
"OUTPUT"_s, dest );
196void QgsGridAlgorithm::createPointGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
200 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHSpacing - mHOverlay ) ) );
201 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
205 const long long cellcnt = rows * cols;
207 int thisProgress = 0;
208 int lastProgress = 0;
210 for (
long long col = 0; col < cols; col++ )
212 const double x = mGridExtent.xMinimum() + ( col * mHSpacing - col * mHOverlay );
214 for (
long long row = 0; row < rows; row++ )
216 const double y = mGridExtent.yMaximum() - ( row * mVSpacing - row * mVOverlay );
228 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
229 if ( feedback && thisProgress != lastProgress )
231 lastProgress = thisProgress;
243void QgsGridAlgorithm::createLineGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
250 hSpace[0] = mHSpacing - mHOverlay;
251 hSpace[1] = mHOverlay;
255 hSpace[0] = mHSpacing;
256 hSpace[1] = mHSpacing;
262 vSpace[0] = mVSpacing - mVOverlay;
263 vSpace[1] = mVOverlay;
267 vSpace[0] = mVSpacing;
268 vSpace[1] = mVSpacing;
275 double cntMax = mGridExtent.height() / mVSpacing;
277 int thisProgress = 0;
278 int lastProgress = 0;
280 double y = mGridExtent.yMaximum();
282 while ( y >= mGridExtent.yMinimum() )
296 y = y - vSpace[cnt % 2];
302 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) / cntMax ) * 50 );
303 if ( feedback && thisProgress != lastProgress )
305 lastProgress = thisProgress;
317 cntMax = mGridExtent.width() / mHSpacing;
321 double x = mGridExtent.xMinimum();
323 while ( x <= mGridExtent.xMaximum() )
336 x = x + hSpace[cnt % 2];
341 thisProgress =
static_cast<int>(
static_cast<double>( 50 ) + (
static_cast<double>( cnt ) / cntMax ) * 100 );
342 if ( feedback && thisProgress != lastProgress )
344 lastProgress = thisProgress;
352void QgsGridAlgorithm::createRectangleGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
356 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHSpacing - mHOverlay ) ) );
357 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
361 const long long cellcnt = rows * cols;
363 int thisProgress = 0;
364 int lastProgress = 0;
365 QVector<double> ringX( 5 );
366 QVector<double> ringY( 5 );
368 for (
long long col = 0; col < cols; col++ )
373 const double x1 = mGridExtent.xMinimum() + ( col * mHSpacing - col * mHOverlay );
374 const double x2 = x1 + mHSpacing;
376 for (
long long row = 0; row < rows; row++ )
378 const double y1 = mGridExtent.yMaximum() - ( row * mVSpacing - row * mVOverlay );
379 const double y2 = y1 - mVSpacing;
381 ringX = { x1, x2, x2, x1, x1 };
382 ringY = { y1, y1, y2, y2, y1 };
383 auto poly = std::make_unique<QgsPolygon>();
395 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
396 if ( feedback && thisProgress != lastProgress )
398 lastProgress = thisProgress;
408void QgsGridAlgorithm::createDiamondGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
412 const double halfHSpacing = mHSpacing / 2;
413 const double halfVSpacing = mVSpacing / 2;
415 const double halfHOverlay = mHOverlay / 2;
416 const double halfVOverlay = mVOverlay / 2;
418 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( halfHSpacing - halfHOverlay ) ) );
419 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - halfVOverlay ) ) );
423 const long long cellcnt = rows * cols;
425 int thisProgress = 0;
426 int lastProgress = 0;
427 QVector<double> ringX( 5 );
428 QVector<double> ringY( 5 );
430 for (
long long col = 0; col < cols; col++ )
435 const double x = mGridExtent.xMinimum() - ( col * halfHOverlay );
436 const double x1 = x + ( ( col + 0 ) * halfHSpacing );
437 const double x2 = x + ( ( col + 1 ) * halfHSpacing );
438 const double x3 = x + ( ( col + 2 ) * halfHSpacing );
440 for (
long long row = 0; row < rows; row++ )
442 const double y = mGridExtent.yMaximum() + ( row * halfVOverlay );
448 if ( ( col % 2 ) == 0 )
450 y1 = y - ( ( ( row * 2 ) + 0 ) * halfVSpacing );
451 y2 = y - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
452 y3 = y - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
456 y1 = y - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
457 y2 = y - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
458 y3 = y - ( ( ( row * 2 ) + 3 ) * halfVSpacing );
461 ringX = { x1, x2, x3, x2, x1 };
462 ringY = { y2, y1, y2, y3, y2 };
463 auto poly = std::make_unique<QgsPolygon>();
475 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
476 if ( feedback && thisProgress != lastProgress )
478 lastProgress = thisProgress;
488void QgsGridAlgorithm::createHexagonGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
493 const double xVertexLo = 0.288675134594813 * mVSpacing;
494 const double xVertexHi = 0.577350269189626 * mVSpacing;
496 mHSpacing = xVertexLo + xVertexHi;
498 mHOverlay = mHSpacing - mHOverlay;
503 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." )
509 const double halfVSpacing = mVSpacing / 2;
511 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHOverlay ) ) );
512 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
516 const long long cellcnt = rows * cols;
518 int thisProgress = 0;
519 int lastProgress = 0;
521 QVector<double> ringX( 7 );
522 QVector<double> ringY( 7 );
523 for (
long long col = 0; col < cols; col++ )
532 const double x1 = mGridExtent.xMinimum() + ( col * mHOverlay );
533 const double x2 = x1 + ( xVertexHi - xVertexLo );
534 const double x3 = mGridExtent.xMinimum() + ( col * mHOverlay ) + mHSpacing;
535 const double x4 = x3 + ( xVertexHi - xVertexLo );
537 for (
long long row = 0; row < rows; row++ )
543 if ( ( col % 2 ) == 0 )
545 y1 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 0 ) * halfVSpacing );
546 y2 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
547 y3 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
551 y1 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
552 y2 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
553 y3 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 3 ) * halfVSpacing );
556 ringX = { x1, x2, x3, x4, x3, x2, x1 };
557 ringY = { y2, y1, y1, y2, y3, y3, y2 };
558 auto poly = std::make_unique<QgsPolygon>();
570 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
571 if ( feedback && thisProgress != lastProgress )
573 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.
void featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
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.