-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsalgorithmgrid.cpp
530 lines (424 loc) · 18.2 KB
/
qgsalgorithmgrid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/***************************************************************************
qgsalgorithmgrid.cpp
---------------------
begin : August 2019
copyright : (C) 2019 by Clemens Raffler
email : clemens dot raffler at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
//Disclaimer:This feature was developed by: Michael Minn, 2010
#include "qgsalgorithmgrid.h"
#include "qgslinestring.h"
#include "qgswkbtypes.h"
#include "qgsvectorlayer.h"
#include "qgspolygon.h"
///@cond PRIVATE
QString QgsGridAlgorithm::name() const
{
return QStringLiteral( "creategrid" );
}
QString QgsGridAlgorithm::displayName() const
{
return QObject::tr( "Create grid" );
}
QStringList QgsGridAlgorithm::tags() const
{
return QObject::tr( "grid,lines,polygons,vector,create,fishnet,diamond,hexagon" ).split( ',' );
}
QString QgsGridAlgorithm::group() const
{
return QObject::tr( "Vector creation" );
}
QString QgsGridAlgorithm::groupId() const
{
return QStringLiteral( "vectorcreation" );
}
void QgsGridAlgorithm::initAlgorithm( const QVariantMap & )
{
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 ) );
addParameter( new QgsProcessingParameterExtent( QStringLiteral( "EXTENT" ), QObject::tr( "Grid extent" ) ) );
addParameter( new QgsProcessingParameterDistance( QStringLiteral( "HSPACING" ), QObject::tr( "Horizontal spacing" ), 1, QStringLiteral( "CRS" ), false, 0, 1000000000.0 ) );
addParameter( new QgsProcessingParameterDistance( QStringLiteral( "VSPACING" ), QObject::tr( "Vertical spacing" ), 1, QStringLiteral( "CRS" ), false, 0, 1000000000.0 ) );
addParameter( new QgsProcessingParameterDistance( QStringLiteral( "HOVERLAY" ), QObject::tr( "Horizontal overlay" ), 0, QStringLiteral( "CRS" ), false, 0, 1000000000.0 ) );
addParameter( new QgsProcessingParameterDistance( QStringLiteral( "VOVERLAY" ), QObject::tr( "Vertical overlay" ), 0, QStringLiteral( "CRS" ), false, 0, 1000000000.0 ) );
addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Grid CRS" ), QStringLiteral( "ProjectCrs" ) ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Grid" ), QgsProcessing::TypeVectorAnyGeometry ) );
}
QString QgsGridAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm creates a vector layer with a grid covering a given extent. "
"Elements in the grid can be points, lines or polygons. The size and/or "
"placement of each element in the grid is defined using a horizontal and "
"vertical spacing. The CRS of the output layer must be defined. The grid extent "
"and the spacing values must be expressed in the coordinates and units of "
"this CRS. The top-left point (minX, maxY) is used as the reference point. "
"That means that, at that point, an element is guaranteed to be placed. "
"Unless the width and height of the selected extent is a multiple of the "
"selected spacing, that is not true for the other points that define that extent."
);
}
QgsGridAlgorithm *QgsGridAlgorithm::createInstance() const
{
return new QgsGridAlgorithm();
}
bool QgsGridAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
mIdx = parameterAsEnum( parameters, QStringLiteral( "TYPE" ), context );
mHSpacing = parameterAsDouble( parameters, QStringLiteral( "HSPACING" ), context );
mVSpacing = parameterAsDouble( parameters, QStringLiteral( "VSPACING" ), context );
mHOverlay = parameterAsDouble( parameters, QStringLiteral( "HOVERLAY" ), context );
mVOverlay = parameterAsDouble( parameters, QStringLiteral( "VOVERLAY" ), context );
mCrs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
mGridExtent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context, mCrs );
return true;
}
QVariantMap QgsGridAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
if ( mHSpacing <= 0 || mVSpacing <= 0 )
throw QgsProcessingException( QObject::tr( "Invalid grid spacing. horizontal: '%1', vertical: '%2'" ).arg( mHSpacing ).arg( mVSpacing ) );
if ( mGridExtent.width() < mHSpacing ) //check if grid extent is smaller than horizontal spacing
throw QgsProcessingException( QObject::tr( "Horizontal spacing is too large for the covered area." ) );
if ( mGridExtent.height() < mVSpacing ) //check if grid extent is smaller than vertical spacing
throw QgsProcessingException( QObject::tr( "Vertical spacing is too large for the covered area." ) );
if ( mHSpacing <= mHOverlay || mVSpacing <= mVOverlay )
throw QgsProcessingException( QObject::tr( "Invalid overlay: horizontal: '%1', vertical: '%2'" ).arg( mHOverlay ).arg( mVOverlay ) );
QgsFields fields = QgsFields();
fields.append( QgsField( QStringLiteral( "id" ), QVariant::LongLong ) );
fields.append( QgsField( QStringLiteral( "left" ), QVariant::Double ) );
fields.append( QgsField( QStringLiteral( "top" ), QVariant::Double ) );
fields.append( QgsField( QStringLiteral( "right" ), QVariant::Double ) );
fields.append( QgsField( QStringLiteral( "bottom" ), QVariant::Double ) );
QgsWkbTypes::Type outputWkb = QgsWkbTypes::Polygon;
switch ( mIdx )
{
case 0:
outputWkb = QgsWkbTypes::Point;
break;
case 1:
outputWkb = QgsWkbTypes::LineString;
break;
}
QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, outputWkb, mCrs ) );
if ( !sink )
throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
feedback->setProgress( 0 );
switch ( mIdx )
{
case 0: //point
createPointGrid( sink, feedback );
break;
case 1: //line
createLineGrid( sink, feedback );
break;
case 2: //rectangle
createRectangleGrid( sink, feedback );
break;
case 3: //diamond
createDiamondGrid( sink, feedback );
break;
case 4: //hexagon
createHexagonGrid( sink, feedback );
break;
}
QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
return outputs;
}
void QgsGridAlgorithm::createPointGrid( std::unique_ptr< QgsFeatureSink > &sink, QgsProcessingFeedback *feedback )
{
QgsFeature f = QgsFeature();
long long cols = static_cast<long long>( std::ceil( mGridExtent.width() / ( mHSpacing - mHOverlay ) ) );
long long rows = static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
long long id = 1;
long long cnt = 0;
long long cellcnt = rows * cols;
int thisProgress = 0;
int lastProgress = 0;
for ( long long col = 0; col < cols; col++ )
{
double x = mGridExtent.xMinimum() + ( col * mHSpacing - col * mHOverlay );
for ( long long row = 0; row < rows; row++ )
{
double y = mGridExtent.yMaximum() - ( row * mVSpacing - row * mVOverlay );
f.setGeometry( QgsGeometry( new QgsPoint( x, y ) ) );
f.setAttributes( QgsAttributes() << id << x << y << x + mHSpacing << y + mVSpacing );
sink->addFeature( f, QgsFeatureSink::FastInsert );
id++;
cnt++;
thisProgress = static_cast<int>( ( static_cast<double>( cnt ) / static_cast<double>( cellcnt ) ) * 100 );
if ( thisProgress != lastProgress )
{
lastProgress = thisProgress;
feedback->setProgress( lastProgress );
}
if ( feedback && feedback->isCanceled() )
break;
}
if ( feedback && feedback->isCanceled() )
break;
}
}
void QgsGridAlgorithm::createLineGrid( std::unique_ptr< QgsFeatureSink > &sink, QgsProcessingFeedback *feedback )
{
QgsFeature f = QgsFeature();
double hSpace[2];
if ( mHOverlay > 0 )
{
hSpace[0] = mHSpacing - mHOverlay;
hSpace[1] = mHOverlay;
}
else
{
hSpace[0] = mHSpacing;
hSpace[1] = mHSpacing;
}
double vSpace[2];
if ( mVOverlay > 0 )
{
vSpace[0] = mVSpacing - mVOverlay;
vSpace[1] = mVOverlay;
}
else
{
vSpace[0] = mVSpacing;
vSpace[1] = mVSpacing;
}
long long cnt = 0;
long long id = 1;
//latitude lines
double cntMax = mGridExtent.height() / mVSpacing;
int thisProgress = 0;
int lastProgress = 0;
double y = mGridExtent.yMaximum();
while ( y >= mGridExtent.yMinimum() )
{
if ( feedback && feedback->isCanceled() )
break;
QgsPoint pt1 = QgsPoint( mGridExtent.xMinimum(), y );
QgsPoint pt2 = QgsPoint( mGridExtent.xMaximum(), y );
f.setGeometry( QgsGeometry( new QgsLineString( pt1, pt2 ) ) );
f.setAttributes( QgsAttributes() << id << mGridExtent.xMinimum() << y << mGridExtent.xMaximum() << y );
sink->addFeature( f, QgsFeatureSink::FastInsert );
y = y - vSpace[cnt % 2];
id++;
cnt++;
//use 50 as count multiplicator because only half of the features are processed at this point
thisProgress = static_cast<int>( ( static_cast<double>( cnt ) / cntMax ) * 50 );
if ( thisProgress != lastProgress )
{
lastProgress = thisProgress;
feedback->setProgress( lastProgress );
}
}
//set progress to 50 manually in case the division doesn't amount to 50.
feedback->setProgress( 50 );
//longitude lines
cnt = 0;
//latitude lines
cntMax = mGridExtent.width() / mHSpacing;
lastProgress = 50;
double x = mGridExtent.xMinimum();
while ( x <= mGridExtent.xMaximum() )
{
if ( feedback->isCanceled() )
break;
QgsPoint pt1 = QgsPoint( x, mGridExtent.yMaximum() );
QgsPoint pt2 = QgsPoint( x, mGridExtent.yMinimum() );
f.setGeometry( QgsGeometry( new QgsLineString( pt1, pt2 ) ) );
f.setAttributes( QgsAttributes() << id << x << mGridExtent.yMaximum() << x << mGridExtent.yMinimum() );
sink->addFeature( f, QgsFeatureSink::FastInsert );
x = x + hSpace[cnt % 2];
id++;
cnt++;
thisProgress = static_cast<int>( static_cast<double>( 50 ) + ( static_cast<double>( cnt ) / cntMax ) * 100 );
if ( thisProgress != lastProgress )
{
lastProgress = thisProgress;
feedback->setProgress( lastProgress );
}
}
feedback->setProgress( 100 );
}
void QgsGridAlgorithm::createRectangleGrid( std::unique_ptr< QgsFeatureSink > &sink, QgsProcessingFeedback *feedback )
{
QgsFeature f = QgsFeature();
long long cols = static_cast<long long>( std::ceil( mGridExtent.width() / ( mHSpacing - mHOverlay ) ) );
long long rows = static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
long long id = 1;
long long cnt = 0;
long long cellcnt = rows * cols;
int thisProgress = 0;
int lastProgress = 0;
QVector< double > ringX( 5 );
QVector< double > ringY( 5 );
for ( long long col = 0; col < cols; col++ )
{
if ( feedback && feedback->isCanceled() )
break;
const double x1 = mGridExtent.xMinimum() + ( col * mHSpacing - col * mHOverlay );
const double x2 = x1 + mHSpacing;
for ( long long row = 0; row < rows; row++ )
{
const double y1 = mGridExtent.yMaximum() - ( row * mVSpacing - row * mVOverlay );
const double y2 = y1 - mVSpacing;
ringX = { x1, x2, x2, x1, x1 };
ringY = { y1, y1, y2, y2, y1 };
std::unique_ptr< QgsPolygon > poly = qgis::make_unique< QgsPolygon >();
poly->setExteriorRing( new QgsLineString( ringX, ringY ) );
f.setGeometry( std::move( poly ) );
f.setAttributes( QgsAttributes() << id << x1 << y1 << x2 << y2 );
sink->addFeature( f, QgsFeatureSink::FastInsert );
id++;
cnt++;
thisProgress = static_cast<int>( ( static_cast<double>( cnt ) / static_cast<double>( cellcnt ) ) * 100 );
if ( thisProgress != lastProgress )
{
lastProgress = thisProgress;
feedback->setProgress( lastProgress );
}
if ( feedback && feedback->isCanceled() )
break;
}
}
}
void QgsGridAlgorithm::createDiamondGrid( std::unique_ptr< QgsFeatureSink > &sink, QgsProcessingFeedback *feedback )
{
QgsFeature f = QgsFeature();
double halfHSpacing = mHSpacing / 2;
double halfVSpacing = mVSpacing / 2;
double halfHOverlay = mHOverlay / 2;
double halfVOverlay = mVOverlay / 2;
long long cols = static_cast<long long>( std::ceil( mGridExtent.width() / ( halfHSpacing - halfHOverlay ) ) );
long long rows = static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - halfVOverlay ) ) );
long long id = 1;
long long cnt = 0;
long long cellcnt = rows * cols;
int thisProgress = 0;
int lastProgress = 0;
QVector< double > ringX( 5 );
QVector< double > ringY( 5 );
for ( long long col = 0; col < cols; col++ )
{
if ( feedback && feedback->isCanceled() )
break;
double x = mGridExtent.xMinimum() - ( col * halfHOverlay );
double x1 = x + ( ( col + 0 ) * halfHSpacing );
double x2 = x + ( ( col + 1 ) * halfHSpacing );
double x3 = x + ( ( col + 2 ) * halfHSpacing );
for ( long long row = 0; row < rows; row++ )
{
double y = mGridExtent.yMaximum() + ( row * halfVOverlay );
double y1;
double y2;
double y3;
if ( ( col % 2 ) == 0 )
{
y1 = y - ( ( ( row * 2 ) + 0 ) * halfVSpacing );
y2 = y - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
y3 = y - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
}
else
{
y1 = y - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
y2 = y - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
y3 = y - ( ( ( row * 2 ) + 3 ) * halfVSpacing );
}
ringX = { x1, x2, x3, x2, x1 };
ringY = { y2, y1, y2, y3, y2 };
std::unique_ptr< QgsPolygon > poly = qgis::make_unique< QgsPolygon >();
poly->setExteriorRing( new QgsLineString( ringX, ringY ) );
f.setGeometry( std::move( poly ) );
f.setAttributes( QgsAttributes() << id << x1 << y1 << x3 << y3 );
sink->addFeature( f, QgsFeatureSink::FastInsert );
id++;
cnt++;
thisProgress = static_cast<int>( ( static_cast<double>( cnt ) / static_cast<double>( cellcnt ) ) * 100 );
if ( thisProgress != lastProgress )
{
lastProgress = thisProgress;
feedback->setProgress( lastProgress );
}
if ( feedback && feedback->isCanceled() )
break;
}
}
}
void QgsGridAlgorithm::createHexagonGrid( std::unique_ptr<QgsFeatureSink> &sink, QgsProcessingFeedback *feedback )
{
QgsFeature f = QgsFeature();
// To preserve symmetry, hspacing is fixed relative to vspacing
double xVertexLo = 0.288675134594813 * mVSpacing;
double xVertexHi = 0.577350269189626 * mVSpacing;
mHSpacing = xVertexLo + xVertexHi;
mHOverlay = mHSpacing - mHOverlay;
if ( mHOverlay < 0 )
{
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 ) );
}
double halfVSpacing = mVSpacing / 2;
long long cols = static_cast<long long>( std::ceil( mGridExtent.width() / ( mHOverlay ) ) );
long long rows = static_cast<long long>( std::ceil( mGridExtent.height() / ( mVSpacing - mVOverlay ) ) );
long long id = 1;
long long cnt = 0;
long long cellcnt = rows * cols;
int thisProgress = 0;
int lastProgress = 0;
QVector< double > ringX( 7 );
QVector< double > ringY( 7 );
for ( long long col = 0; col < cols; col++ )
{
if ( feedback && feedback->isCanceled() )
break;
// (column + 1) and (row + 1) calculation is used to maintain
// topology between adjacent shapes and avoid overlaps/holes
// due to rounding errors
double x1 = mGridExtent.xMinimum() + ( col * mHOverlay );
double x2 = x1 + ( xVertexHi - xVertexLo );
double x3 = mGridExtent.xMinimum() + ( col * mHOverlay ) + mHSpacing;
double x4 = x3 + ( xVertexHi - xVertexLo );
for ( long long row = 0; row < rows; row++ )
{
double y1;
double y2;
double y3;
if ( ( col % 2 ) == 0 )
{
y1 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 0 ) * halfVSpacing );
y2 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
y3 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
}
else
{
y1 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 1 ) * halfVSpacing );
y2 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 2 ) * halfVSpacing );
y3 = mGridExtent.yMaximum() + ( row * mVOverlay ) - ( ( ( row * 2 ) + 3 ) * halfVSpacing );
}
ringX = { x1, x2, x3, x4, x3, x2, x1 };
ringY = { y2, y1, y1, y2, y3, y3, y2 };
std::unique_ptr< QgsPolygon > poly = qgis::make_unique< QgsPolygon >();
poly->setExteriorRing( new QgsLineString( ringX, ringY ) );
f.setGeometry( std::move( poly ) );
f.setAttributes( QgsAttributes() << id << x1 << y1 << x4 << y3 );
sink->addFeature( f, QgsFeatureSink::FastInsert );
id++;
cnt++;
thisProgress = static_cast<int>( ( static_cast<double>( cnt ) / static_cast<double>( cellcnt ) ) * 100 );
if ( thisProgress != lastProgress )
{
lastProgress = thisProgress;
feedback->setProgress( lastProgress );
}
if ( feedback && feedback->isCanceled() )
break;
}
}
}
///@endcond