forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontour.cpp
750 lines (658 loc) · 27.6 KB
/
contour.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/******************************************************************************
*
* Project: Contour Generation
* Purpose: Core algorithm implementation for contour line generation.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 2003, Frank Warmerdam <[email protected]>
* Copyright (c) 2003, Applied Coherent Technology Corporation, www.actgate.com
* Copyright (c) 2007-2013, Even Rouault <even dot rouault at spatialys.com>
* Copyright (c) 2018, Oslandia <infos at oslandia dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "level_generator.h"
#include "polygon_ring_appender.h"
#include "utility.h"
#include "contour_generator.h"
#include "segment_merger.h"
#include "gdal.h"
#include "gdal_alg.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "ogr_api.h"
#include "ogr_srs_api.h"
#include "ogr_geometry.h"
static CPLErr OGRPolygonContourWriter( double dfLevelMin, double dfLevelMax,
const OGRMultiPolygon& multipoly,
void *pInfo )
{
OGRContourWriterInfo *poInfo = static_cast<OGRContourWriterInfo *>(pInfo);
OGRFeatureDefnH hFDefn =
OGR_L_GetLayerDefn( static_cast<OGRLayerH>(poInfo->hLayer) );
OGRFeatureH hFeat = OGR_F_Create( hFDefn );
if( poInfo->nIDField != -1 )
OGR_F_SetFieldInteger( hFeat, poInfo->nIDField, poInfo->nNextID++ );
if( poInfo->nElevFieldMin != -1 )
OGR_F_SetFieldDouble( hFeat, poInfo->nElevFieldMin, dfLevelMin );
if( poInfo->nElevFieldMax != -1 )
OGR_F_SetFieldDouble( hFeat, poInfo->nElevFieldMax, dfLevelMax );
const bool bHasZ = wkbHasZ(OGR_FD_GetGeomType(hFDefn));
OGRGeometryH hGeom = OGR_G_CreateGeometry(
bHasZ ? wkbMultiPolygon25D : wkbMultiPolygon );
for ( int iPart = 0; iPart < multipoly.getNumGeometries(); iPart++ )
{
OGRPolygon* poNewPoly = new OGRPolygon();
const OGRPolygon* poPolygon = static_cast<const OGRPolygon*>(multipoly.getGeometryRef(iPart));
for ( int iRing = 0; iRing < poPolygon->getNumInteriorRings() + 1; iRing++ )
{
const OGRLinearRing* poRing = iRing == 0 ?
poPolygon->getExteriorRing()
: poPolygon->getInteriorRing(iRing - 1);
OGRLinearRing* poNewRing = new OGRLinearRing();
for ( int iPoint = 0; iPoint < poRing->getNumPoints(); iPoint++ )
{
const double dfX = poInfo->adfGeoTransform[0]
+ poInfo->adfGeoTransform[1] * poRing->getX(iPoint)
+ poInfo->adfGeoTransform[2] * poRing->getY(iPoint);
const double dfY = poInfo->adfGeoTransform[3]
+ poInfo->adfGeoTransform[4] * poRing->getX(iPoint)
+ poInfo->adfGeoTransform[5] * poRing->getY(iPoint);
if( bHasZ )
OGR_G_SetPoint( OGRGeometry::ToHandle( poNewRing ), iPoint, dfX, dfY, dfLevelMax );
else
OGR_G_SetPoint_2D( OGRGeometry::ToHandle( poNewRing ), iPoint, dfX, dfY );
}
poNewPoly->addRingDirectly( poNewRing );
}
OGR_G_AddGeometryDirectly( hGeom, OGRGeometry::ToHandle( poNewPoly ) );
}
OGR_F_SetGeometryDirectly( hFeat, hGeom );
const OGRErr eErr =
OGR_L_CreateFeature(static_cast<OGRLayerH>(poInfo->hLayer), hFeat);
OGR_F_Destroy( hFeat );
return eErr == OGRERR_NONE ? CE_None : CE_Failure;
}
struct PolygonContourWriter
{
CPL_DISALLOW_COPY_ASSIGN(PolygonContourWriter)
explicit PolygonContourWriter( OGRContourWriterInfo* poInfo, double minLevel ) : poInfo_( poInfo ), previousLevel_( minLevel ) {}
void startPolygon( double level )
{
previousLevel_ = currentLevel_;
currentGeometry_.reset( new OGRMultiPolygon() );
currentLevel_ = level;
}
void endPolygon()
{
if ( currentPart_ )
{
currentGeometry_->addGeometryDirectly(currentPart_);
}
OGRPolygonContourWriter( previousLevel_, currentLevel_, *currentGeometry_, poInfo_ );
currentGeometry_.reset( nullptr );
currentPart_ = nullptr;
}
void addPart( const marching_squares::LineString& ring )
{
if ( currentPart_ )
{
currentGeometry_->addGeometryDirectly(currentPart_);
}
OGRLinearRing* poNewRing = new OGRLinearRing();
poNewRing->setNumPoints( int(ring.size()) );
int iPoint = 0;
for ( const auto& p : ring )
{
poNewRing->setPoint( iPoint, p.x, p.y );
iPoint++;
}
currentPart_ = new OGRPolygon();
currentPart_->addRingDirectly(poNewRing);
}
void addInteriorRing( const marching_squares::LineString& ring )
{
OGRLinearRing* poNewRing = new OGRLinearRing();
for ( const auto& p : ring )
{
poNewRing->addPoint( p.x, p.y );
}
currentPart_->addRingDirectly(poNewRing);
}
std::unique_ptr<OGRMultiPolygon> currentGeometry_ = {};
OGRPolygon* currentPart_ = nullptr;
OGRContourWriterInfo* poInfo_ = nullptr;
double currentLevel_ = 0;
double previousLevel_;
};
struct GDALRingAppender
{
CPL_DISALLOW_COPY_ASSIGN(GDALRingAppender)
GDALRingAppender(GDALContourWriter write, void *data)
: write_( write )
, data_( data )
{}
void addLine( double level, marching_squares::LineString& ls, bool /*closed*/ )
{
const size_t sz = ls.size();
std::vector<double> xs( sz ), ys ( sz );
size_t i = 0;
for ( const auto& pt : ls ) {
xs[i] = pt.x;
ys[i] = pt.y;
i++;
}
if ( write_(level, int(sz), &xs[0], &ys[0], data_) != CE_None )
CPLError( CE_Failure, CPLE_AppDefined, "cannot write linestring" );
}
private:
GDALContourWriter write_;
void *data_;
};
/************************************************************************/
/* ==================================================================== */
/* Additional C Callable Functions */
/* ==================================================================== */
/************************************************************************/
/************************************************************************/
/* OGRContourWriter() */
/************************************************************************/
CPLErr OGRContourWriter( double dfLevel,
int nPoints, double *padfX, double *padfY,
void *pInfo )
{
OGRContourWriterInfo *poInfo = static_cast<OGRContourWriterInfo *>(pInfo);
OGRFeatureDefnH hFDefn =
OGR_L_GetLayerDefn( static_cast<OGRLayerH>(poInfo->hLayer) );
OGRFeatureH hFeat = OGR_F_Create( hFDefn );
if( poInfo->nIDField != -1 )
OGR_F_SetFieldInteger( hFeat, poInfo->nIDField, poInfo->nNextID++ );
if( poInfo->nElevField != -1 )
OGR_F_SetFieldDouble( hFeat, poInfo->nElevField, dfLevel );
const bool bHasZ = wkbHasZ(OGR_FD_GetGeomType(hFDefn));
OGRGeometryH hGeom = OGR_G_CreateGeometry(
bHasZ ? wkbLineString25D : wkbLineString );
for( int iPoint = nPoints - 1; iPoint >= 0; iPoint-- )
{
const double dfX = poInfo->adfGeoTransform[0]
+ poInfo->adfGeoTransform[1] * padfX[iPoint]
+ poInfo->adfGeoTransform[2] * padfY[iPoint];
const double dfY = poInfo->adfGeoTransform[3]
+ poInfo->adfGeoTransform[4] * padfX[iPoint]
+ poInfo->adfGeoTransform[5] * padfY[iPoint];
if( bHasZ )
OGR_G_SetPoint( hGeom, iPoint, dfX, dfY, dfLevel );
else
OGR_G_SetPoint_2D( hGeom, iPoint, dfX, dfY );
}
OGR_F_SetGeometryDirectly( hFeat, hGeom );
const OGRErr eErr =
OGR_L_CreateFeature(static_cast<OGRLayerH>(poInfo->hLayer), hFeat);
OGR_F_Destroy( hFeat );
return eErr == OGRERR_NONE ? CE_None : CE_Failure;
}
/************************************************************************/
/* GDALContourGenerate() */
/************************************************************************/
/**
* Create vector contours from raster DEM.
*
* This function is kept for compatibility reason and will call the new
* variant GDALContourGenerateEx that is more extensible and provide more
* options.
*
* Details about the algorithm are also given in the documentation of the
* new GDALContourenerateEx function.
*
* @param hBand The band to read raster data from. The whole band will be
* processed.
*
* @param dfContourInterval The elevation interval between contours generated.
*
* @param dfContourBase The "base" relative to which contour intervals are
* applied. This is normally zero, but could be different. To generate 10m
* contours at 5, 15, 25, ... the ContourBase would be 5.
*
* @param nFixedLevelCount The number of fixed levels. If this is greater than
* zero, then fixed levels will be used, and ContourInterval and ContourBase
* are ignored.
*
* @param padfFixedLevels The list of fixed contour levels at which contours
* should be generated. It will contain FixedLevelCount entries, and may be
* NULL if fixed levels are disabled (FixedLevelCount = 0).
*
* @param bUseNoData If TRUE the dfNoDataValue will be used.
*
* @param dfNoDataValue The value to use as a "nodata" value. That is, a
* pixel value which should be ignored in generating contours as if the value
* of the pixel were not known.
*
* @param hLayer The layer to which new contour vectors will be written.
* Each contour will have a LINESTRING geometry attached to it. This
* is really of type OGRLayerH, but void * is used to avoid pulling the
* ogr_api.h file in here.
*
* @param iIDField If not -1 this will be used as a field index to indicate
* where a unique id should be written for each feature (contour) written.
*
* @param iElevField If not -1 this will be used as a field index to indicate
* where the elevation value of the contour should be written.
*
* @param pfnProgress A GDALProgressFunc that may be used to report progress
* to the user, or to interrupt the algorithm. May be NULL if not required.
*
* @param pProgressArg The callback data for the pfnProgress function.
*
* @return CE_None on success or CE_Failure if an error occurs.
*/
CPLErr GDALContourGenerate( GDALRasterBandH hBand,
double dfContourInterval, double dfContourBase,
int nFixedLevelCount, double *padfFixedLevels,
int bUseNoData, double dfNoDataValue,
void *hLayer, int iIDField, int iElevField,
GDALProgressFunc pfnProgress, void *pProgressArg )
{
char** options = nullptr;
if ( nFixedLevelCount > 0 ) {
std::string values = "FIXED_LEVELS=";
for ( int i = 0; i < nFixedLevelCount; i++ ) {
const int sz = 32;
char* newValue = new char[sz+1];
if ( i == nFixedLevelCount - 1 ) {
CPLsnprintf( newValue, sz+1, "%f", padfFixedLevels[i] );
}
else {
CPLsnprintf( newValue, sz+1, "%f,", padfFixedLevels[i] );
}
values = values + std::string( newValue );
delete[] newValue;
}
options = CSLAddString( options, values.c_str() );
}
else if ( dfContourInterval != 0.0 ) {
options = CSLAppendPrintf( options, "LEVEL_INTERVAL=%f", dfContourInterval );
}
if ( dfContourBase != 0.0 ) {
options = CSLAppendPrintf( options, "LEVEL_BASE=%f", dfContourBase );
}
if ( bUseNoData ) {
options = CSLAppendPrintf( options, "NODATA=%.19g", dfNoDataValue );
}
if ( iIDField != -1 ) {
options = CSLAppendPrintf( options, "ID_FIELD=%d", iIDField );
}
if ( iElevField != -1 ) {
options = CSLAppendPrintf( options, "ELEV_FIELD=%d", iElevField );
}
CPLErr err = GDALContourGenerateEx( hBand, hLayer, options, pfnProgress, pProgressArg );
CSLDestroy( options );
return err;
}
/**
* Create vector contours from raster DEM.
*
* This algorithm is an implementation of "Marching squares" [1] that will
* generate contour vectors for the input raster band on the requested set
* of contour levels. The vector contours are written to the passed in OGR
* vector layer. Also, a NODATA value may be specified to identify pixels
* that should not be considered in contour line generation.
*
* The gdal/apps/gdal_contour.cpp mainline can be used as an example of
* how to use this function.
*
* [1] see https://en.wikipedia.org/wiki/Marching_squares
*
* ALGORITHM RULES
For contouring purposes raster pixel values are assumed to represent a point
value at the center of the corresponding pixel region. For the purpose of
contour generation we virtually connect each pixel center to the values to
the left, right, top and bottom. We assume that the pixel value is linearly
interpolated between the pixel centers along each line, and determine where
(if any) contour lines will appear along these line segments. Then the
contour crossings are connected.
This means that contour lines' nodes will not actually be on pixel edges, but
rather along vertical and horizontal lines connecting the pixel centers.
\verbatim
General Case:
5 | | 3
-- + ---------------- + --
| |
| |
| |
| |
10 + |
|\ |
| \ |
-- + -+-------------- + --
12 | 10 | 1
Saddle Point:
5 | | 12
-- + -------------+-- + --
| \ |
| \|
| +
| |
+ |
|\ |
| \ |
-- + -+-------------- + --
12 | | 1
or:
5 | | 12
-- + -------------+-- + --
| __/ |
| ___/ |
| ___/ __+
| / __/ |
+' __/ |
| __/ |
| ,__/ |
-- + -+-------------- + --
12 | | 1
\endverbatim
Nodata:
In the "nodata" case we treat the whole nodata pixel as a no-mans land.
We extend the corner pixels near the nodata out to half way and then
construct extra lines from those points to the center which is assigned
an averaged value from the two nearby points (in this case (12+3+5)/3).
\verbatim
5 | | 3
-- + ---------------- + --
| |
| |
| 6.7 |
| +---------+ 3
10 +___ |
| \____+ 10
| |
-- + -------+ +
12 | 12 (nodata)
\endverbatim
*
* @param hBand The band to read raster data from. The whole band will be
* processed.
*
* @param hLayer The layer to which new contour vectors will be written.
* Each contour will have a LINESTRING geometry attached to it
* (or POLYGON if POLYGONIZE=YES). This is really of type OGRLayerH, but
* void * is used to avoid pulling the ogr_api.h file in here.
*
* @param pfnProgress A GDALProgressFunc that may be used to report progress
* to the user, or to interrupt the algorithm. May be NULL if not required.
*
* @param pProgressArg The callback data for the pfnProgress function.
*
* @param options List of options
*
* Options:
*
* LEVEL_INTERVAL=f
*
* The elevation interval between contours generated.
*
* LEVEL_BASE=f
*
* The "base" relative to which contour intervals are
* applied. This is normally zero, but could be different. To generate 10m
* contours at 5, 15, 25, ... the LEVEL_BASE would be 5.
*
* LEVEL_EXP_BASE=f
*
* If greater than 0, contour levels are generated on an
* exponential scale. Levels will then be generated by LEVEL_EXP_BASE^k
* where k is a positive integer.
*
* FIXED_LEVELS=f[,f]*
*
* The list of fixed contour levels at which contours should be generated.
* This option has precedence on LEVEL_INTERVAL
*
* NODATA=f
*
* The value to use as a "nodata" value. That is, a pixel value which
* should be ignored in generating contours as if the value of the pixel
* were not known.
*
* ID_FIELD=d
*
* This will be used as a field index to indicate where a unique id should
* be written for each feature (contour) written.
*
* ELEV_FIELD=d
*
* This will be used as a field index to indicate where the elevation value
* of the contour should be written. Only used in line contouring mode.
*
* ELEV_FIELD_MIN=d
*
* This will be used as a field index to indicate where the minimum elevation value
* of the polygon contour should be written. Only used in polygonal contouring mode.
*
* ELEV_FIELD_MAX=d
*
* This will be used as a field index to indicate where the maximum elevation value
* of the polygon contour should be written. Only used in polygonal contouring mode.
*
* POLYGONIZE=YES|NO
*
* If YES, contour polygons will be created, rather than polygon lines.
*
*
* @return CE_None on success or CE_Failure if an error occurs.
*/
CPLErr GDALContourGenerateEx( GDALRasterBandH hBand, void *hLayer,
CSLConstList options,
GDALProgressFunc pfnProgress, void *pProgressArg )
{
VALIDATE_POINTER1( hBand, "GDALContourGenerateEx", CE_Failure );
if( pfnProgress == nullptr )
pfnProgress = GDALDummyProgress;
double contourInterval = 0.0;
const char* opt = CSLFetchNameValue( options, "LEVEL_INTERVAL" );
if ( opt ) {
contourInterval = CPLAtof( opt );
}
double contourBase = 0.0;
opt = CSLFetchNameValue( options, "LEVEL_BASE" );
if ( opt ) {
contourBase = CPLAtof( opt );
}
double expBase = 0.0;
opt = CSLFetchNameValue( options, "LEVEL_EXP_BASE" );
if ( opt ) {
expBase = CPLAtof( opt );
}
std::vector<double> fixedLevels;
opt = CSLFetchNameValue( options, "FIXED_LEVELS" );
if ( opt ) {
char** values = CSLTokenizeStringComplex( opt, ",", FALSE, FALSE );
fixedLevels.resize( CSLCount( values ) );
for ( size_t i = 0; i < fixedLevels.size(); i++ ) {
fixedLevels[i] = CPLAtof(values[i]);
}
CSLDestroy( values );
}
bool useNoData = false;
double noDataValue = 0.0;
opt = CSLFetchNameValue( options, "NODATA" );
if ( opt ) {
useNoData = true;
noDataValue = CPLAtof( opt );
if( GDALGetRasterDataType(hBand) == GDT_Float32 )
{
int bClamped = FALSE;
int bRounded = FALSE;
noDataValue = GDALAdjustValueToDataType(GDT_Float32,
noDataValue,
&bClamped, &bRounded );
}
}
int idField = -1;
opt = CSLFetchNameValue( options, "ID_FIELD" );
if ( opt ) {
idField = atoi( opt );
}
int elevField = -1;
opt = CSLFetchNameValue( options, "ELEV_FIELD" );
if ( opt ) {
elevField = atoi( opt );
}
int elevFieldMin = -1;
opt = CSLFetchNameValue( options, "ELEV_FIELD_MIN" );
if ( opt ) {
elevFieldMin = atoi( opt );
}
int elevFieldMax = -1;
opt = CSLFetchNameValue( options, "ELEV_FIELD_MAX" );
if ( opt ) {
elevFieldMax = atoi( opt );
}
bool polygonize = CPLFetchBool( options, "POLYGONIZE", false );
using namespace marching_squares;
OGRContourWriterInfo oCWI;
oCWI.hLayer = static_cast<OGRLayerH>(hLayer);
oCWI.nElevField = elevField;
oCWI.nElevFieldMin = elevFieldMin;
oCWI.nElevFieldMax = elevFieldMax;
oCWI.nIDField = idField;
oCWI.adfGeoTransform[0] = 0.0;
oCWI.adfGeoTransform[1] = 1.0;
oCWI.adfGeoTransform[2] = 0.0;
oCWI.adfGeoTransform[3] = 0.0;
oCWI.adfGeoTransform[4] = 0.0;
oCWI.adfGeoTransform[5] = 1.0;
GDALDatasetH hSrcDS = GDALGetBandDataset( hBand );
if( hSrcDS != nullptr )
GDALGetGeoTransform( hSrcDS, oCWI.adfGeoTransform );
oCWI.nNextID = 0;
bool ok = false;
try
{
if ( polygonize )
{
int bSuccess;
PolygonContourWriter w( &oCWI, GDALGetRasterMinimum( hBand, &bSuccess ) );
typedef PolygonRingAppender<PolygonContourWriter> RingAppender;
RingAppender appender( w );
if ( ! fixedLevels.empty() ) {
FixedLevelRangeIterator levels( &fixedLevels[0], fixedLevels.size(), GDALGetRasterMaximum( hBand, &bSuccess ) );
SegmentMerger<RingAppender, FixedLevelRangeIterator> writer(appender, levels, /* polygonize */ true);
ContourGeneratorFromRaster<decltype(writer), FixedLevelRangeIterator> cg( hBand, useNoData, noDataValue, writer, levels );
ok = cg.process( pfnProgress, pProgressArg );
}
else if ( expBase > 0.0 ) {
ExponentialLevelRangeIterator levels( expBase );
SegmentMerger<RingAppender, ExponentialLevelRangeIterator> writer(appender, levels, /* polygonize */ true);
ContourGeneratorFromRaster<decltype(writer), ExponentialLevelRangeIterator> cg( hBand, useNoData, noDataValue, writer, levels );
ok = cg.process( pfnProgress, pProgressArg );
}
else {
IntervalLevelRangeIterator levels( contourBase, contourInterval );
SegmentMerger<RingAppender, IntervalLevelRangeIterator> writer(appender, levels, /* polygonize */ true);
ContourGeneratorFromRaster<decltype(writer), IntervalLevelRangeIterator> cg( hBand, useNoData, noDataValue, writer, levels );
ok = cg.process( pfnProgress, pProgressArg );
}
}
else
{
GDALRingAppender appender(OGRContourWriter, &oCWI);
if ( ! fixedLevels.empty() ) {
FixedLevelRangeIterator levels( &fixedLevels[0], fixedLevels.size() );
SegmentMerger<GDALRingAppender, FixedLevelRangeIterator> writer(appender, levels, /* polygonize */ false);
ContourGeneratorFromRaster<decltype(writer), FixedLevelRangeIterator> cg( hBand, useNoData, noDataValue, writer, levels );
ok = cg.process( pfnProgress, pProgressArg );
}
else if ( expBase > 0.0 ) {
ExponentialLevelRangeIterator levels( expBase );
SegmentMerger<GDALRingAppender, ExponentialLevelRangeIterator> writer(appender, levels, /* polygonize */ false);
ContourGeneratorFromRaster<decltype(writer), ExponentialLevelRangeIterator> cg( hBand, useNoData, noDataValue, writer, levels );
ok = cg.process( pfnProgress, pProgressArg );
}
else {
IntervalLevelRangeIterator levels( contourBase, contourInterval );
SegmentMerger<GDALRingAppender, IntervalLevelRangeIterator> writer(appender, levels, /* polygonize */ false);
ContourGeneratorFromRaster<decltype(writer), IntervalLevelRangeIterator> cg( hBand, useNoData, noDataValue, writer, levels );
ok = cg.process( pfnProgress, pProgressArg );
}
}
}
catch (const std::exception & e)
{
CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
return CE_Failure;
}
return ok ? CE_None : CE_Failure;
}
/************************************************************************/
/* GDAL_CG_Create() */
/************************************************************************/
namespace marching_squares {
// Opaque type used by the C API
struct ContourGeneratorOpaque
{
typedef SegmentMerger<GDALRingAppender, IntervalLevelRangeIterator> SegmentMergerT;
typedef ContourGenerator<SegmentMergerT, IntervalLevelRangeIterator> ContourGeneratorT;
ContourGeneratorOpaque( int nWidth, int nHeight, int bNoDataSet, double dfNoDataValue,
double dfContourInterval, double dfContourBase,
GDALContourWriter pfnWriter, void *pCBData )
: levels( dfContourBase, dfContourInterval )
, writer( pfnWriter, pCBData )
, merger( writer, levels, /* polygonize */ false )
, contourGenerator( nWidth, nHeight, bNoDataSet != 0, dfNoDataValue, merger, levels )
{}
IntervalLevelRangeIterator levels;
GDALRingAppender writer;
SegmentMergerT merger;
ContourGeneratorT contourGenerator;
};
}
/** Create contour generator */
GDALContourGeneratorH
GDAL_CG_Create( int nWidth, int nHeight, int bNoDataSet, double dfNoDataValue,
double dfContourInterval, double dfContourBase,
GDALContourWriter pfnWriter, void *pCBData )
{
auto cg = new marching_squares::ContourGeneratorOpaque( nWidth,
nHeight,
bNoDataSet,
dfNoDataValue,
dfContourInterval,
dfContourBase,
pfnWriter,
pCBData );
return reinterpret_cast<GDALContourGeneratorH>(cg);
}
/************************************************************************/
/* GDAL_CG_FeedLine() */
/************************************************************************/
/** Feed a line to the contour generator */
CPLErr GDAL_CG_FeedLine( GDALContourGeneratorH hCG, double *padfScanline )
{
VALIDATE_POINTER1( hCG, "GDAL_CG_FeedLine", CE_Failure );
return reinterpret_cast<marching_squares::ContourGeneratorOpaque*>(hCG)->contourGenerator.feedLine( padfScanline );
}
/************************************************************************/
/* GDAL_CG_Destroy() */
/************************************************************************/
/** Destroy contour generator */
void GDAL_CG_Destroy( GDALContourGeneratorH hCG )
{
delete reinterpret_cast<marching_squares::ContourGeneratorOpaque*>(hCG);
}