29QString QgsGridAlgorithm::name()
const
31 return QStringLiteral(
"creategrid" );
34QString QgsGridAlgorithm::displayName()
const
36 return QObject::tr(
"Create grid" );
39QStringList QgsGridAlgorithm::tags()
const
41 return QObject::tr(
"grid,lines,polygons,vector,create,fishnet,diamond,hexagon" ).split(
',' );
44QString QgsGridAlgorithm::group()
const
46 return QObject::tr(
"Vector creation" );
49QString QgsGridAlgorithm::groupId()
const
51 return QStringLiteral(
"vectorcreation" );
54void QgsGridAlgorithm::initAlgorithm(
const QVariantMap & )
56 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"TYPE" ), 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 ) );
60 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"HSPACING" ), QObject::tr(
"Horizontal spacing" ), 1, QStringLiteral(
"CRS" ),
false, 0, 1000000000.0 ) );
61 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"VSPACING" ), QObject::tr(
"Vertical spacing" ), 1, QStringLiteral(
"CRS" ),
false, 0, 1000000000.0 ) );
63 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"HOVERLAY" ), QObject::tr(
"Horizontal overlay" ), 0, QStringLiteral(
"CRS" ),
false ) );
64 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"VOVERLAY" ), QObject::tr(
"Vertical overlay" ), 0, QStringLiteral(
"CRS" ),
false ) );
66 addParameter(
new QgsProcessingParameterCrs( QStringLiteral(
"CRS" ), QObject::tr(
"Grid CRS" ), QStringLiteral(
"ProjectCrs" ) ) );
71QString QgsGridAlgorithm::shortHelpString()
const
73 return QObject::tr(
"This algorithm creates a vector layer with a grid covering a given extent. "
74 "Elements in the grid can be points, lines or polygons. The size and/or "
75 "placement of each element in the grid is defined using a horizontal and "
76 "vertical spacing. The CRS of the output layer must be defined. The grid extent "
77 "and the spacing values must be expressed in the coordinates and units of "
78 "this CRS. The top-left point (minX, maxY) is used as the reference point. "
79 "That means that, at that point, an element is guaranteed to be placed. "
80 "Unless the width and height of the selected extent is a multiple of the "
81 "selected spacing, that is not true for the other points that define that extent."
84QString QgsGridAlgorithm::shortDescription()
const
86 return QObject::tr(
"Creates a vector layer with a grid covering a given extent." );
89QgsGridAlgorithm *QgsGridAlgorithm::createInstance()
const
91 return new QgsGridAlgorithm();
96 mIdx = parameterAsEnum( parameters, QStringLiteral(
"TYPE" ), context );
97 mHSpacing = parameterAsDouble( parameters, QStringLiteral(
"HSPACING" ), context );
98 mVSpacing = parameterAsDouble( parameters, QStringLiteral(
"VSPACING" ), context );
99 mHOverlay = parameterAsDouble( parameters, QStringLiteral(
"HOVERLAY" ), context );
100 mVOverlay = parameterAsDouble( parameters, QStringLiteral(
"VOVERLAY" ), context );
101 mCrs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
102 mGridExtent = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, mCrs );
109 if ( mHSpacing <= 0 || mVSpacing <= 0 )
110 throw QgsProcessingException( QObject::tr(
"Invalid grid spacing. horizontal: '%1', vertical: '%2'" ).arg( mHSpacing ).arg( mVSpacing ) );
112 if ( mGridExtent.width() < mHSpacing )
115 if ( mGridExtent.height() < mVSpacing )
122 fields.
append(
QgsField( QStringLiteral(
"id" ), QMetaType::Type::LongLong ) );
123 fields.
append(
QgsField( QStringLiteral(
"left" ), QMetaType::Type::Double ) );
124 fields.
append(
QgsField( QStringLiteral(
"top" ), QMetaType::Type::Double ) );
125 fields.
append(
QgsField( QStringLiteral(
"right" ), QMetaType::Type::Double ) );
126 fields.
append(
QgsField( QStringLiteral(
"bottom" ), QMetaType::Type::Double ) );
133 fields.
append(
QgsField( QStringLiteral(
"row_index" ), QMetaType::Type::LongLong ) );
134 fields.
append(
QgsField( QStringLiteral(
"col_index" ), QMetaType::Type::LongLong ) );
153 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields, outputWkb, mCrs ) );
162 createPointGrid( sink, feedback );
165 createLineGrid( sink, feedback );
168 createRectangleGrid( sink, feedback );
171 createDiamondGrid( sink, feedback );
174 createHexagonGrid( sink, feedback );
180 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
184void QgsGridAlgorithm::createPointGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
188 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHSpacing - mHOverlay ) ) );
189 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
193 const long long cellcnt = rows * cols;
195 int thisProgress = 0;
196 int lastProgress = 0;
198 for (
long long col = 0; col < cols; col++ )
200 const double x = mGridExtent.xMinimum() + ( col * mHSpacing - col * mHOverlay );
202 for (
long long row = 0; row < rows; row++ )
204 const double y = mGridExtent.yMaximum() - ( row * mVSpacing - row * mVOverlay );
209 throw QgsProcessingException( writeFeatureError( sink.get(), QVariantMap(), QStringLiteral(
"OUTPUT" ) ) );
214 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
215 if ( feedback && thisProgress != lastProgress )
217 lastProgress = thisProgress;
229void QgsGridAlgorithm::createLineGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
236 hSpace[0] = mHSpacing - mHOverlay;
237 hSpace[1] = mHOverlay;
241 hSpace[0] = mHSpacing;
242 hSpace[1] = mHSpacing;
248 vSpace[0] = mVSpacing - mVOverlay;
249 vSpace[1] = mVOverlay;
253 vSpace[0] = mVSpacing;
254 vSpace[1] = mVSpacing;
261 double cntMax = mGridExtent.height() / mVSpacing;
263 int thisProgress = 0;
264 int lastProgress = 0;
266 double y = mGridExtent.yMaximum();
268 while ( y >= mGridExtent.yMinimum() )
279 throw QgsProcessingException( writeFeatureError( sink.get(), QVariantMap(), QStringLiteral(
"OUTPUT" ) ) );
280 y = y - vSpace[cnt % 2];
286 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) / cntMax ) * 50 );
287 if ( feedback && thisProgress != lastProgress )
289 lastProgress = thisProgress;
301 cntMax = mGridExtent.width() / mHSpacing;
305 double x = mGridExtent.xMinimum();
307 while ( x <= mGridExtent.xMaximum() )
317 throw QgsProcessingException( writeFeatureError( sink.get(), QVariantMap(), QStringLiteral(
"OUTPUT" ) ) );
318 x = x + hSpace[cnt % 2];
323 thisProgress =
static_cast<int>(
static_cast<double>( 50 ) + (
static_cast<double>( cnt ) / cntMax ) * 100 );
324 if ( feedback && thisProgress != lastProgress )
326 lastProgress = thisProgress;
334void QgsGridAlgorithm::createRectangleGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
338 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHSpacing - mHOverlay ) ) );
339 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
343 const long long cellcnt = rows * cols;
345 int thisProgress = 0;
346 int lastProgress = 0;
347 QVector<double> ringX( 5 );
348 QVector<double> ringY( 5 );
350 for (
long long col = 0; col < cols; col++ )
355 const double x1 = mGridExtent.xMinimum() + ( col * mHSpacing - col * mHOverlay );
356 const double x2 = x1 + mHSpacing;
358 for (
long long row = 0; row < rows; row++ )
360 const double y1 = mGridExtent.yMaximum() - ( row * mVSpacing - row * mVOverlay );
361 const double y2 = y1 - mVSpacing;
363 ringX = { x1, x2, x2, x1, x1 };
364 ringY = { y1, y1, y2, y2, y1 };
365 auto poly = std::make_unique<QgsPolygon>();
370 throw QgsProcessingException( writeFeatureError( sink.get(), QVariantMap(), QStringLiteral(
"OUTPUT" ) ) );
375 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
376 if ( feedback && thisProgress != lastProgress )
378 lastProgress = thisProgress;
388void QgsGridAlgorithm::createDiamondGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
392 const double halfHSpacing = mHSpacing / 2;
393 const double halfVSpacing = mVSpacing / 2;
395 const double halfHOverlay = mHOverlay / 2;
396 const double halfVOverlay = mVOverlay / 2;
398 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( halfHSpacing - halfHOverlay ) ) );
399 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - halfVOverlay ) ) );
403 const long long cellcnt = rows * cols;
405 int thisProgress = 0;
406 int lastProgress = 0;
407 QVector<double> ringX( 5 );
408 QVector<double> ringY( 5 );
410 for (
long long col = 0; col < cols; col++ )
415 const double x = mGridExtent.xMinimum() - ( col * halfHOverlay );
416 const double x1 = x + ( ( col + 0 ) * halfHSpacing );
417 const double x2 = x + ( ( col + 1 ) * halfHSpacing );
418 const double x3 = x + ( ( col + 2 ) * halfHSpacing );
420 for (
long long row = 0; row < rows; row++ )
422 const double y = mGridExtent.yMaximum() + ( row * halfVOverlay );
428 if ( ( col % 2 ) == 0 )
430 y1 = y - ( ( ( row * 2 ) + 0 ) * halfVSpacing );
431 y2 = y - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
432 y3 = y - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
436 y1 = y - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
437 y2 = y - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
438 y3 = y - ( ( ( row * 2 ) + 3 ) * halfVSpacing );
441 ringX = { x1, x2, x3, x2, x1 };
442 ringY = { y2, y1, y2, y3, y2 };
443 auto poly = std::make_unique<QgsPolygon>();
448 throw QgsProcessingException( writeFeatureError( sink.get(), QVariantMap(), QStringLiteral(
"OUTPUT" ) ) );
453 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
454 if ( feedback && thisProgress != lastProgress )
456 lastProgress = thisProgress;
466void QgsGridAlgorithm::createHexagonGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
471 const double xVertexLo = 0.288675134594813 * mVSpacing;
472 const double xVertexHi = 0.577350269189626 * mVSpacing;
474 mHSpacing = xVertexLo + xVertexHi;
476 mHOverlay = mHSpacing - mHOverlay;
480 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 ) );
483 const double halfVSpacing = mVSpacing / 2;
485 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHOverlay ) ) );
486 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
490 const long long cellcnt = rows * cols;
492 int thisProgress = 0;
493 int lastProgress = 0;
495 QVector<double> ringX( 7 );
496 QVector<double> ringY( 7 );
497 for (
long long col = 0; col < cols; col++ )
506 const double x1 = mGridExtent.xMinimum() + ( col * mHOverlay );
507 const double x2 = x1 + ( xVertexHi - xVertexLo );
508 const double x3 = mGridExtent.xMinimum() + ( col * mHOverlay ) + mHSpacing;
509 const double x4 = x3 + ( xVertexHi - xVertexLo );
511 for (
long long row = 0; row < rows; row++ )
517 if ( ( col % 2 ) == 0 )
519 y1 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 0 ) * halfVSpacing );
520 y2 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
521 y3 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
525 y1 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
526 y2 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
527 y3 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 3 ) * halfVSpacing );
530 ringX = { x1, x2, x3, x4, x3, x2, x1 };
531 ringY = { y2, y1, y1, y2, y3, y3, y2 };
532 auto poly = std::make_unique<QgsPolygon>();
537 throw QgsProcessingException( writeFeatureError( sink.get(), QVariantMap(), QStringLiteral(
"OUTPUT" ) ) );
542 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
543 if ( feedback && thisProgress != lastProgress )
545 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.