-
Notifications
You must be signed in to change notification settings - Fork 131
/
algorithms.go
600 lines (544 loc) · 18.7 KB
/
algorithms.go
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
package gdal
/*
#include "go_gdal.h"
#include "gdal_version.h"
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
var _ = fmt.Println
/* --------------------------------------------- */
/* Misc functions */
/* --------------------------------------------- */
// Compute optimal PCT for RGB image
func ComputeMedianCutPCT(
red, green, blue RasterBand,
colors int,
ct ColorTable,
progress ProgressFunc,
data interface{},
) int {
arg := &goGDALProgressFuncProxyArgs{
progress, data,
}
err := C.GDALComputeMedianCutPCT(
red.cval,
green.cval,
blue.cval,
nil,
C.int(colors),
ct.cval,
C.goGDALProgressFuncProxyB(),
unsafe.Pointer(arg),
)
return int(err)
}
// 24bit to 8bit conversion with dithering
func DitherRGB2PCT(
red, green, blue, target RasterBand,
ct ColorTable,
progress ProgressFunc,
data interface{},
) int {
arg := &goGDALProgressFuncProxyArgs{
progress, data,
}
err := C.GDALDitherRGB2PCT(
red.cval,
green.cval,
blue.cval,
target.cval,
ct.cval,
C.goGDALProgressFuncProxyB(),
unsafe.Pointer(arg),
)
return int(err)
}
// Compute checksum for image region
func (rb RasterBand) Checksum(xOff, yOff, xSize, ySize int) int {
sum := C.GDALChecksumImage(rb.cval, C.int(xOff), C.int(yOff), C.int(xSize), C.int(ySize))
return int(sum)
}
// Compute the proximity of all pixels in the image to a set of pixels in the source image
func (src RasterBand) ComputeProximity(
dest RasterBand,
options []string,
progress ProgressFunc,
data interface{},
) error {
arg := &goGDALProgressFuncProxyArgs{
progress, data,
}
length := len(options)
opts := make([]*C.char, length+1)
for i := 0; i < length; i++ {
opts[i] = C.CString(options[i])
defer C.free(unsafe.Pointer(opts[i]))
}
opts[length] = (*C.char)(unsafe.Pointer(nil))
cErr := C.GDALComputeProximity(
src.cval,
dest.cval,
(**C.char)(unsafe.Pointer(&opts[0])),
C.goGDALProgressFuncProxyB(),
unsafe.Pointer(arg),
)
return CPLErrContainer{ErrVal: cErr}.Err()
}
// Fill selected raster regions by interpolation from the edges
func (src RasterBand) FillNoData(
mask RasterBand,
distance float64,
iterations int,
options []string,
progress ProgressFunc,
data interface{},
) error {
arg := &goGDALProgressFuncProxyArgs{
progress, data,
}
length := len(options)
opts := make([]*C.char, length+1)
for i := 0; i < length; i++ {
opts[i] = C.CString(options[i])
defer C.free(unsafe.Pointer(opts[i]))
}
opts[length] = (*C.char)(unsafe.Pointer(nil))
cErr := C.GDALFillNodata(
src.cval,
mask.cval,
C.double(distance),
0,
C.int(iterations),
(**C.char)(unsafe.Pointer(&opts[0])),
C.goGDALProgressFuncProxyB(),
unsafe.Pointer(arg),
)
return CPLErrContainer{ErrVal: cErr}.Err()
}
// Create polygon coverage from raster data using an integer buffer
func (src RasterBand) Polygonize(
mask RasterBand,
layer Layer,
fieldIndex int,
options []string,
progress ProgressFunc,
data interface{},
) error {
arg := &goGDALProgressFuncProxyArgs{
progress, data,
}
length := len(options)
opts := make([]*C.char, length+1)
for i := 0; i < length; i++ {
opts[i] = C.CString(options[i])
defer C.free(unsafe.Pointer(opts[i]))
}
opts[length] = (*C.char)(unsafe.Pointer(nil))
cErr := C.GDALPolygonize(
src.cval,
mask.cval,
layer.cval,
C.int(fieldIndex),
(**C.char)(unsafe.Pointer(&opts[0])),
C.goGDALProgressFuncProxyB(),
unsafe.Pointer(arg),
)
return CPLErrContainer{ErrVal: cErr}.Err()
}
// Create polygon coverage from raster data using a floating point buffer
func (src RasterBand) FPolygonize(
mask RasterBand,
layer Layer,
fieldIndex int,
options []string,
progress ProgressFunc,
data interface{},
) error {
arg := &goGDALProgressFuncProxyArgs{
progress, data,
}
length := len(options)
opts := make([]*C.char, length+1)
for i := 0; i < length; i++ {
opts[i] = C.CString(options[i])
defer C.free(unsafe.Pointer(opts[i]))
}
opts[length] = (*C.char)(unsafe.Pointer(nil))
cErr := C.GDALFPolygonize(
src.cval,
mask.cval,
layer.cval,
C.int(fieldIndex),
(**C.char)(unsafe.Pointer(&opts[0])),
C.goGDALProgressFuncProxyB(),
unsafe.Pointer(arg),
)
return CPLErrContainer{ErrVal: cErr}.Err()
}
// Removes small raster polygons
func (src RasterBand) SieveFilter(
mask, dest RasterBand,
threshold, connectedness int,
options []string,
progress ProgressFunc,
data interface{},
) error {
arg := &goGDALProgressFuncProxyArgs{
progress, data,
}
length := len(options)
opts := make([]*C.char, length+1)
for i := 0; i < length; i++ {
opts[i] = C.CString(options[i])
defer C.free(unsafe.Pointer(opts[i]))
}
opts[length] = (*C.char)(unsafe.Pointer(nil))
cErr := C.GDALSieveFilter(
src.cval,
mask.cval,
dest.cval,
C.int(threshold),
C.int(connectedness),
(**C.char)(unsafe.Pointer(&opts[0])),
C.goGDALProgressFuncProxyB(),
unsafe.Pointer(arg),
)
return CPLErrContainer{ErrVal: cErr}.Err()
}
/* --------------------------------------------- */
/* Warp functions */
/* --------------------------------------------- */
//Unimplemented: CreateGenImgProjTransformer
//Unimplemented: CreateGenImgProjTransformer2
//Unimplemented: CreateGenImgProjTransformer3
//Unimplemented: SetGenImgProjTransformerDstGeoTransform
//Unimplemented: DestroyGenImgProjTransformer
//Unimplemented: GenImgProjTransform
//Unimplemented: CreateReprojectionTransformer
//Unimplemented: DestroyReprojection
//Unimplemented: ReprojectionTransform
//Unimplemented: CreateGCPTransformer
//Unimplemented: CreateGCPRefineTransformer
//Unimplemented: DestroyGCPTransformer
//Unimplemented: GCPTransform
//Unimplemented: CreateTPSTransformer
//Unimplemented: DestroyTPSTransformer
//Unimplemented: TPSTransform
//Unimplemented: CreateRPCTransformer
//Unimplemented: DestroyRPCTransformer
//Unimplemented: RPCTransform
//Unimplemented: CreateGeoLocTransformer
//Unimplemented: DestroyGeoLocTransformer
//Unimplemented: GeoLocTransform
//Unimplemented: CreateApproxTransformer
//Unimplemented: DestroyApproxTransformer
//Unimplemented: ApproxTransform
//Unimplemented: SimpleImageWarp
//Unimplemented: SuggestedWarpOutput
//Unimplemented: SuggsetedWarpOutput2
//Unimplemented: SerializeTransformer
//Unimplemented: DeserializeTransformer
//Unimplemented: TransformGeolocations
/* --------------------------------------------- */
/* Contour line functions */
/* --------------------------------------------- */
//Unimplemented: CreateContourGenerator
//Unimplemented: FeedLine
//Unimplemented: Destroy
//Unimplemented: ContourWriter
// ContourGenerate creates vector contours in intervals relative to base from raster DEM band.
// If fixedLevels are defined, the contours are generated at the specified levels instead.
// The contours are written to the provided layer using idField- and elevationFieldIndex.
func (src RasterBand) ContourGenerate(
interval, base float64,
fixedLevels []float64,
useNoDataValue int,
noDataValue float64,
layer Layer,
idFieldIndex int,
elevationFieldIndex int,
progress ProgressFunc,
data interface{},
) error {
arg := &goGDALProgressFuncProxyArgs{
progress, data,
}
fixedLevels_p := (*C.double)(unsafe.Pointer(nil))
if len(fixedLevels) > 0 {
fixedLevels_p = (*C.double)(unsafe.Pointer(&fixedLevels[0]))
}
return CPLErr(C.GDALContourGenerate(
src.cval,
C.double(interval),
C.double(base),
C.int(len(fixedLevels)),
fixedLevels_p,
C.int(useNoDataValue),
C.double(noDataValue),
unsafe.Pointer(layer.cval),
C.int(idFieldIndex),
C.int(elevationFieldIndex),
C.goGDALProgressFuncProxyB(),
unsafe.Pointer(arg),
)).Err()
}
/* --------------------------------------------- */
/* Rasterizer functions */
/* --------------------------------------------- */
// Burn geometries into raster
//Unimplmemented: RasterizeGeometries
// Burn geometries from the specified list of layers into the raster
//Unimplemented: RasterizeLayers
// Burn geometries from the specified list of layers into the raster
//Unimplemented: RasterizeLayersBuf
/* --------------------------------------------- */
/* Gridding functions */
/* --------------------------------------------- */
// GridAlgorithm represents Grid Algorithm code
type GridAlgorithm int
const (
GA_InverseDistancetoAPower = GridAlgorithm(C.GGA_InverseDistanceToAPower)
GA_MovingAverage = GridAlgorithm(C.GGA_MovingAverage)
GA_NearestNeighbor = GridAlgorithm(C.GGA_NearestNeighbor)
GA_MetricMinimum = GridAlgorithm(C.GGA_MetricMinimum)
GA_MetricMaximum = GridAlgorithm(C.GGA_MetricMaximum)
GA_MetricRange = GridAlgorithm(C.GGA_MetricRange)
GA_MetricCount = GridAlgorithm(C.GGA_MetricCount)
GA_MetricAverageDistance = GridAlgorithm(C.GGA_MetricAverageDistance)
GA_MetricAverageDistancePts = GridAlgorithm(C.GGA_MetricAverageDistancePts)
GA_Linear = GridAlgorithm(C.GGA_Linear)
GA_InverseDistanceToAPowerNearestNeighbor = GridAlgorithm(C.GGA_InverseDistanceToAPowerNearestNeighbor)
)
// GridLinearOptions: Linear method control options.
type GridLinearOptions struct {
// SizeOfStructure: Added in GDAL 3.6 to detect potential ABI issues. Should be set to sizeof(GDALGridLinearOptions)
SizeOfStructure uintptr
// Radius: in case the point to be interpolated does not fit into a triangle of the Delaunay triangulation,
// use that maximum distance to search a nearest neighbour, or use nodata otherwise. If set to -1, the search
// distance is infinite. If set to 0, nodata value will be always used.
Radius float64
// NoDataValue: no data marker to fill empty points.
NoDataValue float64
}
// GridInverseDistanceToAPowerOptions: Inverse distance to a power method control options.
type GridInverseDistanceToAPowerOptions struct {
// SizeOfStructure: Added in GDAL 3.6 to detect potential ABI issues. Should be set to sizeof(GridInverseDistanceToAPowerOptions)
SizeOfStructure uintptr
// Power: Weighting power
Power float64
// Smoothing: Smoothing parameter
Smoothing float64
// AnisotropyRatio: Reserved for future use
AnisotropyRatio float64
// AnisotropyAngle: Reserved for future use
AnisotropyAngle float64
// Radius1: The first radius (X axis if rotation angle is 0) of search ellipse.
Radius1 float64
// Radius2: The second radius (Y axis if rotation angle is 0) of search ellipse.
Radius2 float64
// Angle: Angle of ellipse rotation in degrees. Ellipse rotated counter clockwise.
Angle float64
// MaxPoints: Maximum number of data points to use.
// Do not search for more points than this number. If less amount of points found the grid node
// considered empty and will be filled with NODATA marker.
MaxPoints uint32
// MinPoints: Minimum number of data points to use.
// If less amount of points found the grid node considered empty and will be filled with NODATA marker.
MinPoints uint32
// NoDataValue: No data marker to fill empty points.
NoDataValue float64
}
// GridInverseDistanceToAPowerNearestNeighborOptions: Inverse distance to a power, with nearest neighbour search,
// control options
type GridInverseDistanceToAPowerNearestNeighborOptions struct {
// SizeOfStructure: Added in GDAL 3.6 to detect potential ABI issues. Should be set to sizeof(GridInverseDistanceToAPowerNearestNeighborOptions)
SizeOfStructure uintptr
// Power: Weighting power
Power float64
// Radius: The radius of search circle
Radius float64
// Smoothing: Smoothing parameter
Smoothing float64
// MaxPoints: Maximum number of data points to use.
// Do not search for more points than this number. If less amount of points found the grid node
// considered empty and will be filled with NODATA marker.
MaxPoints uint32
// MinPoints: Minimum number of data points to use.
// If less amount of points found the grid node considered empty and will be filled with NODATA marker.
MinPoints uint32
// NoDataValue: No data marker to fill empty points.
NoDataValue float64
}
// GridMovingAverageOptions: Moving average method control options
type GridMovingAverageOptions struct {
// SizeOfStructure: Added in GDAL 3.6 to detect potential ABI issues. Should be set to sizeof(GridMovingAverageOptions)
SizeOfStructure uintptr
// Radius1: The first radius (X axis if rotation angle is 0) of search ellipse.
Radius1 float64
// Radius2: The second radius (Y axis if rotation angle is 0) of search ellipse.
Radius2 float64
// Angle: Angle of ellipse rotation in degrees. Ellipse rotated counter clockwise.
Angle float64
// MinPoints: Minimum number of data points to use.
// If less amount of points found the grid node considered empty and will be filled with NODATA marker.
MinPoints uint32
// NoDataValue: No data marker to fill empty points.
NoDataValue float64
}
// GridNearestNeighborOptions: Nearest neighbor method control options.
type GridNearestNeighborOptions struct {
// SizeOfStructure: Added in GDAL 3.6 to detect potential ABI issues. Should be set to sizeof(GridNearestNeighborOptions)
SizeOfStructure uintptr
// Radius1: The first radius (X axis if rotation angle is 0) of search ellipse.
Radius1 float64
// Radius2: The second radius (Y axis if rotation angle is 0) of search ellipse.
Radius2 float64
// Angle: Angle of ellipse rotation in degrees. Ellipse rotated counter clockwise.
Angle float64
// NoDataValue: No data marker to fill empty points.
NoDataValue float64
}
// GridDataMetricsOptions: Data metrics method control options
type GridDataMetricsOptions struct {
// SizeOfStructure: Added in GDAL 3.6 to detect potential ABI issues. Should be set to sizeof(GridDataMetricsOptions)
SizeOfStructure uintptr
// Radius1: The first radius (X axis if rotation angle is 0) of search ellipse.
Radius1 float64
// Radius2: The second radius (Y axis if rotation angle is 0) of search ellipse.
Radius2 float64
// Angle: Angle of ellipse rotation in degrees. Ellipse rotated counter clockwise.
Angle float64
// MinPoints: Minimum number of data points to use.
// If less amount of points found the grid node considered empty and will be filled with NODATA marker.
MinPoints uint32
// NoDataValue: No data marker to fill empty points.
NoDataValue float64
}
var errInvalidOptionsTypeWasPassed = errors.New("invalid options type was passed")
// GridCreate: Create regular grid from the scattered data.
// This function takes the arrays of X and Y coordinates and corresponding Z values as input and computes
// regular grid (or call it a raster) from these scattered data. You should supply geometry and extent of the
// output grid.
func GridCreate(
algorithm GridAlgorithm,
options interface{},
x, y, z []float64,
xMin, xMax, yMin, yMax float64,
nX, nY uint,
progress ProgressFunc,
data interface{},
) ([]float64, error) {
if len(x) != len(y) || len(x) != len(z) {
return nil, errors.New("lengths of x, y, z should equal")
}
poptions := unsafe.Pointer(nil)
switch algorithm {
case GA_InverseDistancetoAPower:
soptions, ok := options.(GridInverseDistanceToAPowerOptions)
if !ok {
return nil, errInvalidOptionsTypeWasPassed
}
poptions = unsafe.Pointer(&C.GDALGridInverseDistanceToAPowerOptions{
nSizeOfStructure: C.size_t(unsafe.Sizeof(soptions)),
dfPower: C.double(soptions.Power),
dfSmoothing: C.double(soptions.Smoothing),
dfAnisotropyRatio: C.double(soptions.AnisotropyRatio),
dfAnisotropyAngle: C.double(soptions.AnisotropyAngle),
dfRadius1: C.double(soptions.Radius1),
dfRadius2: C.double(soptions.Radius2),
dfAngle: C.double(soptions.Angle),
nMaxPoints: C.uint(soptions.MaxPoints),
nMinPoints: C.uint(soptions.MinPoints),
dfNoDataValue: C.double(soptions.NoDataValue),
})
case GA_InverseDistanceToAPowerNearestNeighbor:
soptions, ok := options.(GridInverseDistanceToAPowerNearestNeighborOptions)
if !ok {
return nil, errInvalidOptionsTypeWasPassed
}
poptions = unsafe.Pointer(&C.GDALGridInverseDistanceToAPowerNearestNeighborOptions{
nSizeOfStructure: C.size_t(unsafe.Sizeof(soptions)),
dfPower: C.double(soptions.Power),
dfRadius: C.double(soptions.Radius),
dfSmoothing: C.double(soptions.Smoothing),
nMaxPoints: C.uint(soptions.MaxPoints),
nMinPoints: C.uint(soptions.MinPoints),
dfNoDataValue: C.double(soptions.NoDataValue),
})
case GA_MovingAverage:
soptions, ok := options.(GridMovingAverageOptions)
if !ok {
return nil, errInvalidOptionsTypeWasPassed
}
poptions = unsafe.Pointer(&C.GDALGridMovingAverageOptions{
nSizeOfStructure: C.size_t(unsafe.Sizeof(soptions)),
dfRadius1: C.double(soptions.Radius1),
dfRadius2: C.double(soptions.Radius2),
dfAngle: C.double(soptions.Angle),
nMinPoints: C.uint(soptions.MinPoints),
dfNoDataValue: C.double(soptions.NoDataValue),
})
case GA_NearestNeighbor:
soptions, ok := options.(GridNearestNeighborOptions)
if !ok {
return nil, errInvalidOptionsTypeWasPassed
}
poptions = unsafe.Pointer(&C.GDALGridNearestNeighborOptions{
nSizeOfStructure: C.size_t(unsafe.Sizeof(soptions)),
dfRadius1: C.double(soptions.Radius1),
dfRadius2: C.double(soptions.Radius2),
dfAngle: C.double(soptions.Angle),
dfNoDataValue: C.double(soptions.NoDataValue),
})
case GA_MetricMinimum, GA_MetricMaximum, GA_MetricCount, GA_MetricRange,
GA_MetricAverageDistance, GA_MetricAverageDistancePts:
soptions, ok := options.(GridDataMetricsOptions)
if !ok {
return nil, errInvalidOptionsTypeWasPassed
}
poptions = unsafe.Pointer(&C.GDALGridDataMetricsOptions{
nSizeOfStructure: C.size_t(unsafe.Sizeof(soptions)),
dfRadius1: C.double(soptions.Radius1),
dfRadius2: C.double(soptions.Radius2),
dfAngle: C.double(soptions.Angle),
nMinPoints: C.uint(soptions.MinPoints),
dfNoDataValue: C.double(soptions.NoDataValue),
})
case GA_Linear:
soptions, ok := options.(GridLinearOptions)
if !ok {
return nil, errInvalidOptionsTypeWasPassed
}
poptions = unsafe.Pointer(&C.GDALGridLinearOptions{
nSizeOfStructure: C.size_t(unsafe.Sizeof(soptions)),
dfRadius: C.double(soptions.Radius),
dfNoDataValue: C.double(soptions.NoDataValue),
})
}
buffer := make([]float64, nX*nY)
arg := &goGDALProgressFuncProxyArgs{progress, data}
cErr := C.GDALGridCreate(
C.GDALGridAlgorithm(algorithm),
poptions,
C.uint(uint(len(x))),
(*C.double)(unsafe.Pointer(&x[0])),
(*C.double)(unsafe.Pointer(&y[0])),
(*C.double)(unsafe.Pointer(&z[0])),
C.double(xMin),
C.double(xMax),
C.double(yMin),
C.double(yMax),
C.uint(nX),
C.uint(nY),
C.GDALDataType(Float64),
unsafe.Pointer(&buffer[0]),
C.goGDALProgressFuncProxyB(),
unsafe.Pointer(arg),
)
return buffer, CPLErrContainer{ErrVal: cErr}.Err()
}
//Unimplemented: ComputeMatchingPoints