28QString QgsGridAlgorithm::name()
const
30 return QStringLiteral(
"creategrid" );
33QString QgsGridAlgorithm::displayName()
const
35 return QObject::tr(
"Create grid" );
38QStringList QgsGridAlgorithm::tags()
const
40 return QObject::tr(
"grid,lines,polygons,vector,create,fishnet,diamond,hexagon" ).split(
',' );
43QString QgsGridAlgorithm::group()
const
45 return QObject::tr(
"Vector creation" );
48QString QgsGridAlgorithm::groupId()
const
50 return QStringLiteral(
"vectorcreation" );
53void QgsGridAlgorithm::initAlgorithm(
const QVariantMap & )
55 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 ) );
59 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"HSPACING" ), QObject::tr(
"Horizontal spacing" ), 1, QStringLiteral(
"CRS" ),
false, 0, 1000000000.0 ) );
60 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"VSPACING" ), QObject::tr(
"Vertical spacing" ), 1, QStringLiteral(
"CRS" ),
false, 0, 1000000000.0 ) );
62 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"HOVERLAY" ), QObject::tr(
"Horizontal overlay" ), 0, QStringLiteral(
"CRS" ),
false ) );
63 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"VOVERLAY" ), QObject::tr(
"Vertical overlay" ), 0, QStringLiteral(
"CRS" ),
false ) );
65 addParameter(
new QgsProcessingParameterCrs( QStringLiteral(
"CRS" ), QObject::tr(
"Grid CRS" ), QStringLiteral(
"ProjectCrs" ) ) );
70QString QgsGridAlgorithm::shortHelpString()
const
72 return QObject::tr(
"This algorithm creates a vector layer with a grid covering a given extent. "
73 "Elements in the grid can be points, lines or polygons. The size and/or "
74 "placement of each element in the grid is defined using a horizontal and "
75 "vertical spacing. The CRS of the output layer must be defined. The grid extent "
76 "and the spacing values must be expressed in the coordinates and units of "
77 "this CRS. The top-left point (minX, maxY) is used as the reference point. "
78 "That means that, at that point, an element is guaranteed to be placed. "
79 "Unless the width and height of the selected extent is a multiple of the "
80 "selected spacing, that is not true for the other points that define that extent."
83QString QgsGridAlgorithm::shortDescription()
const
85 return QObject::tr(
"Creates a vector layer with a grid covering a given extent." );
88QgsGridAlgorithm *QgsGridAlgorithm::createInstance()
const
90 return new QgsGridAlgorithm();
95 mIdx = parameterAsEnum( parameters, QStringLiteral(
"TYPE" ), context );
96 mHSpacing = parameterAsDouble( parameters, QStringLiteral(
"HSPACING" ), context );
97 mVSpacing = parameterAsDouble( parameters, QStringLiteral(
"VSPACING" ), context );
98 mHOverlay = parameterAsDouble( parameters, QStringLiteral(
"HOVERLAY" ), context );
99 mVOverlay = parameterAsDouble( parameters, QStringLiteral(
"VOVERLAY" ), context );
100 mCrs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
101 mGridExtent = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, mCrs );
108 if ( mHSpacing <= 0 || mVSpacing <= 0 )
109 throw QgsProcessingException( QObject::tr(
"Invalid grid spacing. horizontal: '%1', vertical: '%2'" ).arg( mHSpacing ).arg( mVSpacing ) );
111 if ( mGridExtent.width() < mHSpacing )
114 if ( mGridExtent.height() < mVSpacing )
121 fields.
append(
QgsField( QStringLiteral(
"id" ), QMetaType::Type::LongLong ) );
122 fields.
append(
QgsField( QStringLiteral(
"left" ), QMetaType::Type::Double ) );
123 fields.
append(
QgsField( QStringLiteral(
"top" ), QMetaType::Type::Double ) );
124 fields.
append(
QgsField( QStringLiteral(
"right" ), QMetaType::Type::Double ) );
125 fields.
append(
QgsField( QStringLiteral(
"bottom" ), QMetaType::Type::Double ) );
132 fields.
append(
QgsField( QStringLiteral(
"row_index" ), QMetaType::Type::LongLong ) );
133 fields.
append(
QgsField( QStringLiteral(
"col_index" ), QMetaType::Type::LongLong ) );
152 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields, outputWkb, mCrs ) );
161 createPointGrid( sink, feedback );
164 createLineGrid( sink, feedback );
167 createRectangleGrid( sink, feedback );
170 createDiamondGrid( sink, feedback );
173 createHexagonGrid( sink, feedback );
179 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
183void QgsGridAlgorithm::createPointGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
187 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHSpacing - mHOverlay ) ) );
188 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
192 const long long cellcnt = rows * cols;
194 int thisProgress = 0;
195 int lastProgress = 0;
197 for (
long long col = 0; col < cols; col++ )
199 const double x = mGridExtent.xMinimum() + ( col * mHSpacing - col * mHOverlay );
201 for (
long long row = 0; row < rows; row++ )
203 const double y = mGridExtent.yMaximum() - ( row * mVSpacing - row * mVOverlay );
208 throw QgsProcessingException( writeFeatureError( sink.get(), QVariantMap(), QStringLiteral(
"OUTPUT" ) ) );
213 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
214 if ( thisProgress != lastProgress )
216 lastProgress = thisProgress;
228void QgsGridAlgorithm::createLineGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
235 hSpace[0] = mHSpacing - mHOverlay;
236 hSpace[1] = mHOverlay;
240 hSpace[0] = mHSpacing;
241 hSpace[1] = mHSpacing;
247 vSpace[0] = mVSpacing - mVOverlay;
248 vSpace[1] = mVOverlay;
252 vSpace[0] = mVSpacing;
253 vSpace[1] = mVSpacing;
260 double cntMax = mGridExtent.height() / mVSpacing;
262 int thisProgress = 0;
263 int lastProgress = 0;
265 double y = mGridExtent.yMaximum();
267 while ( y >= mGridExtent.yMinimum() )
278 throw QgsProcessingException( writeFeatureError( sink.get(), QVariantMap(), QStringLiteral(
"OUTPUT" ) ) );
279 y = y - vSpace[cnt % 2];
285 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) / cntMax ) * 50 );
286 if ( thisProgress != lastProgress )
288 lastProgress = thisProgress;
299 cntMax = mGridExtent.width() / mHSpacing;
303 double x = mGridExtent.xMinimum();
305 while ( x <= mGridExtent.xMaximum() )
315 throw QgsProcessingException( writeFeatureError( sink.get(), QVariantMap(), QStringLiteral(
"OUTPUT" ) ) );
316 x = x + hSpace[cnt % 2];
321 thisProgress =
static_cast<int>(
static_cast<double>( 50 ) + (
static_cast<double>( cnt ) / cntMax ) * 100 );
322 if ( thisProgress != lastProgress )
324 lastProgress = thisProgress;
331void QgsGridAlgorithm::createRectangleGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
335 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHSpacing - mHOverlay ) ) );
336 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
340 const long long cellcnt = rows * cols;
342 int thisProgress = 0;
343 int lastProgress = 0;
344 QVector<double> ringX( 5 );
345 QVector<double> ringY( 5 );
347 for (
long long col = 0; col < cols; col++ )
352 const double x1 = mGridExtent.xMinimum() + ( col * mHSpacing - col * mHOverlay );
353 const double x2 = x1 + mHSpacing;
355 for (
long long row = 0; row < rows; row++ )
357 const double y1 = mGridExtent.yMaximum() - ( row * mVSpacing - row * mVOverlay );
358 const double y2 = y1 - mVSpacing;
360 ringX = { x1, x2, x2, x1, x1 };
361 ringY = { y1, y1, y2, y2, y1 };
362 auto poly = std::make_unique<QgsPolygon>();
367 throw QgsProcessingException( writeFeatureError( sink.get(), QVariantMap(), QStringLiteral(
"OUTPUT" ) ) );
372 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
373 if ( thisProgress != lastProgress )
375 lastProgress = thisProgress;
385void QgsGridAlgorithm::createDiamondGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
389 const double halfHSpacing = mHSpacing / 2;
390 const double halfVSpacing = mVSpacing / 2;
392 const double halfHOverlay = mHOverlay / 2;
393 const double halfVOverlay = mVOverlay / 2;
395 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( halfHSpacing - halfHOverlay ) ) );
396 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - halfVOverlay ) ) );
400 const long long cellcnt = rows * cols;
402 int thisProgress = 0;
403 int lastProgress = 0;
404 QVector<double> ringX( 5 );
405 QVector<double> ringY( 5 );
407 for (
long long col = 0; col < cols; col++ )
412 const double x = mGridExtent.xMinimum() - ( col * halfHOverlay );
413 const double x1 = x + ( ( col + 0 ) * halfHSpacing );
414 const double x2 = x + ( ( col + 1 ) * halfHSpacing );
415 const double x3 = x + ( ( col + 2 ) * halfHSpacing );
417 for (
long long row = 0; row < rows; row++ )
419 const double y = mGridExtent.yMaximum() + ( row * halfVOverlay );
425 if ( ( col % 2 ) == 0 )
427 y1 = y - ( ( ( row * 2 ) + 0 ) * halfVSpacing );
428 y2 = y - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
429 y3 = y - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
433 y1 = y - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
434 y2 = y - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
435 y3 = y - ( ( ( row * 2 ) + 3 ) * halfVSpacing );
438 ringX = { x1, x2, x3, x2, x1 };
439 ringY = { y2, y1, y2, y3, y2 };
440 auto poly = std::make_unique<QgsPolygon>();
445 throw QgsProcessingException( writeFeatureError( sink.get(), QVariantMap(), QStringLiteral(
"OUTPUT" ) ) );
450 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
451 if ( thisProgress != lastProgress )
453 lastProgress = thisProgress;
463void QgsGridAlgorithm::createHexagonGrid( std::unique_ptr<QgsFeatureSink> &sink,
QgsProcessingFeedback *feedback )
468 const double xVertexLo = 0.288675134594813 * mVSpacing;
469 const double xVertexHi = 0.577350269189626 * mVSpacing;
471 mHSpacing = xVertexLo + xVertexHi;
473 mHOverlay = mHSpacing - mHOverlay;
477 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 ) );
480 const double halfVSpacing = mVSpacing / 2;
482 const long long cols =
static_cast<long long>( std::ceil( mGridExtent.width() / ( mHOverlay ) ) );
483 const long long rows =
static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
487 const long long cellcnt = rows * cols;
489 int thisProgress = 0;
490 int lastProgress = 0;
492 QVector<double> ringX( 7 );
493 QVector<double> ringY( 7 );
494 for (
long long col = 0; col < cols; col++ )
503 const double x1 = mGridExtent.xMinimum() + ( col * mHOverlay );
504 const double x2 = x1 + ( xVertexHi - xVertexLo );
505 const double x3 = mGridExtent.xMinimum() + ( col * mHOverlay ) + mHSpacing;
506 const double x4 = x3 + ( xVertexHi - xVertexLo );
508 for (
long long row = 0; row < rows; row++ )
514 if ( ( col % 2 ) == 0 )
516 y1 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 0 ) * halfVSpacing );
517 y2 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
518 y3 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
522 y1 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
523 y2 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
524 y3 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 3 ) * halfVSpacing );
527 ringX = { x1, x2, x3, x4, x3, x2, x1 };
528 ringY = { y2, y1, y1, y2, y3, y3, y2 };
529 auto poly = std::make_unique<QgsPolygon>();
534 throw QgsProcessingException( writeFeatureError( sink.get(), QVariantMap(), QStringLiteral(
"OUTPUT" ) ) );
539 thisProgress =
static_cast<int>( (
static_cast<double>( cnt ) /
static_cast<double>( cellcnt ) ) * 100 );
540 if ( thisProgress != lastProgress )
542 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.