-
Notifications
You must be signed in to change notification settings - Fork 131
/
gdal.go
2135 lines (1833 loc) · 61.5 KB
/
gdal.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
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gdal
/*
#include "go_gdal.h"
#include "gdal_version.h"
*/
import "C"
import (
"errors"
"fmt"
"reflect"
"unsafe"
)
var _ = fmt.Println
func init() {
C.GDALAllRegister()
}
/* -------------------------------------------------------------------- */
/* Significant constants. */
/* -------------------------------------------------------------------- */
const (
VERSION_MAJOR = int(C.GDAL_VERSION_MAJOR)
VERSION_MINOR = int(C.GDAL_VERSION_MINOR)
VERSION_REV = int(C.GDAL_VERSION_REV)
VERSION_BUILD = int(C.GDAL_VERSION_BUILD)
VERSION_NUM = int(C.GDAL_VERSION_NUM)
RELEASE_DATE = int(C.GDAL_RELEASE_DATE)
RELEASE_NAME = string(C.GDAL_RELEASE_NAME)
GDT_Unknown = int(C.GDT_Unknown)
GDT_Byte = int(C.GDT_Byte)
GDT_UInt16 = int(C.GDT_UInt16)
GDT_Int16 = int(C.GDT_Int16)
GDT_UInt32 = int(C.GDT_UInt32)
GDT_Int32 = int(C.GDT_Int32)
GDT_Float32 = int(C.GDT_Float32)
GDT_Float64 = int(C.GDT_Float64)
GDT_CInt16 = int(C.GDT_CInt16)
GDT_CInt32 = int(C.GDT_CInt32)
GDT_CFloat32 = int(C.GDT_CFloat32)
GDT_CFloat64 = int(C.GDT_CFloat64)
GDT_TypeCount = int(C.GDT_TypeCount)
GA_ReadOnly = int(C.GA_ReadOnly)
GA_Update = int(C.GA_Update)
GF_Read = int(C.GF_Read)
GF_Write = int(C.GF_Write)
GRIORA_NearestNeighbour = int(C.GRIORA_NearestNeighbour)
GRIORA_Bilinear = int(C.GRIORA_Bilinear)
GRIORA_Cubic = int(C.GRIORA_Cubic)
GRIORA_CubicSpline = int(C.GRIORA_CubicSpline)
GRIORA_Lanczos = int(C.GRIORA_Lanczos)
GRIORA_Average = int(C.GRIORA_Average)
GRIORA_Mode = int(C.GRIORA_Mode)
GRIORA_Gauss = int(C.GRIORA_Gauss)
GCI_Undefined = int(C.GCI_Undefined)
GCI_GrayIndex = int(C.GCI_GrayIndex)
GCI_PaletteIndex = int(C.GCI_PaletteIndex)
GCI_RedBand = int(C.GCI_RedBand)
GCI_GreenBand = int(C.GCI_GreenBand)
GCI_BlueBand = int(C.GCI_BlueBand)
GCI_AlphaBand = int(C.GCI_AlphaBand)
GCI_HueBand = int(C.GCI_HueBand)
GCI_SaturationBand = int(C.GCI_SaturationBand)
GCI_LightnessBand = int(C.GCI_LightnessBand)
GCI_CyanBand = int(C.GCI_CyanBand)
GCI_MagentaBand = int(C.GCI_MagentaBand)
GCI_YellowBand = int(C.GCI_YellowBand)
GCI_BlackBand = int(C.GCI_BlackBand)
GCI_YCbCr_YBand = int(C.GCI_YCbCr_YBand)
GCI_YCbCr_CrBand = int(C.GCI_YCbCr_CrBand)
GCI_YCbCr_CbBand = int(C.GCI_YCbCr_CbBand)
GRA_Average = int(C.GRA_Average)
GRA_Mode = int(C.GRA_Mode)
GRA_Max = int(C.GRA_Max)
GRA_Min = int(C.GRA_Min)
GRA_Med = int(C.GRA_Med)
GRA_Q1 = int(C.GRA_Q1)
GRA_Q3 = int(C.GRA_Q3)
GPI_Gray = int(C.GPI_Gray)
GPI_RGB = int(C.GPI_RGB)
GPI_CMYK = int(C.GPI_CMYK)
GPI_HLS = int(C.GPI_HLS)
CXT_Element = int(C.CXT_Element)
CXT_Text = int(C.CXT_Text)
CXT_Attribute = int(C.CXT_Attribute)
CXT_Comment = int(C.CXT_Comment)
CXT_Literal = int(C.CXT_Literal)
CPLE_None = int(C.CPLE_None)
CPLE_AppDefined = int(C.CPLE_AppDefined)
CPLE_OutOfMemory = int(C.CPLE_OutOfMemory)
CPLE_FileIO = int(C.CPLE_FileIO)
CPLE_OpenFailed = int(C.CPLE_OpenFailed)
CPLE_IllegalArg = int(C.CPLE_IllegalArg)
CPLE_NotSupported = int(C.CPLE_NotSupported)
CPLE_AssertionFailed = int(C.CPLE_AssertionFailed)
CPLE_NoWriteAccess = int(C.CPLE_NoWriteAccess)
CPLE_UserInterrupt = int(C.CPLE_UserInterrupt)
CPLE_ObjectNull = int(C.CPLE_ObjectNull)
CPLE_HttpResponse = int(C.CPLE_HttpResponse)
CPLE_AWSBucketNotFound = int(C.CPLE_AWSBucketNotFound)
CPLE_AWSObjectNotFound = int(C.CPLE_AWSObjectNotFound)
CPLE_AWSAccessDenied = int(C.CPLE_AWSAccessDenied)
CPLE_AWSInvalidCredentials = int(C.CPLE_AWSInvalidCredentials)
CPLE_AWSSignatureDoesNotMatch = int(C.CPLE_AWSSignatureDoesNotMatch)
GRTT_THEMATIC = int(C.GRTT_THEMATIC)
GRTT_ATHEMATIC = int(C.GRTT_ATHEMATIC)
GMF_ALL_VALID = int(C.GMF_ALL_VALID)
GMF_PER_DATASET = int(C.GMF_PER_DATASET)
GMF_ALPHA = int(C.GMF_ALPHA)
GMF_NODATA = int(C.GMF_NODATA)
GDAL_DATA_COVERAGE_STATUS_UNIMPLEMENTED = int(C.GDAL_DATA_COVERAGE_STATUS_UNIMPLEMENTED)
GDAL_DATA_COVERAGE_STATUS_DATA = int(C.GDAL_DATA_COVERAGE_STATUS_DATA)
GDAL_DATA_COVERAGE_STATUS_EMPTY = int(C.GDAL_DATA_COVERAGE_STATUS_EMPTY)
GARIO_PENDING = int(C.GARIO_PENDING)
GARIO_UPDATE = int(C.GARIO_UPDATE)
GARIO_ERROR = int(C.GARIO_ERROR)
GARIO_COMPLETE = int(C.GARIO_COMPLETE)
GTO_TIP = int(C.GTO_TIP)
GTO_BIT = int(C.GTO_BIT)
GTO_BSQ = int(C.GTO_BSQ)
)
var (
ErrDebug = errors.New("Debug Error")
ErrWarning = errors.New("Warning Error")
ErrFailure = errors.New("Failure Error")
ErrFatal = errors.New("Fatal Error")
ErrIllegal = errors.New("Illegal Error")
ErrNotEnoughData = errors.New("Not Enough Data")
ErrNotEnoughMemory = errors.New("Not Enough Memory")
ErrUnsupportedGeometryType = errors.New("Unsupported Geometry Type")
ErrUnsupportedOperation = errors.New("Unsupported Operation")
ErrCorruptData = errors.New("Corrupt Data")
ErrUnsupportedSRS = errors.New("Unsupported SRS")
ErrInvalidHandle = errors.New("Invalid Handle")
ErrNonExistingFeature = errors.New("Non Existing Feature")
)
type CPLErr int
type CPLErrContainer struct {
ErrVal C.CPLErr
}
const (
CE_None = CPLErr(C.CE_None)
CE_Debug = CPLErr(C.CE_Debug)
CE_Warning = CPLErr(C.CE_Warning)
CE_Failure = CPLErr(C.CE_Failure)
CE_Fatal = CPLErr(C.CE_Fatal)
)
// Error handling. The following is bare-bones, and needs to be replaced with something more useful.
func (err CPLErrContainer) Err() error {
switch CPLErr(err.ErrVal) {
case CE_None:
return nil
case CE_Debug:
return ErrDebug
case CE_Warning:
return ErrWarning
case CE_Failure:
return ErrFailure
case CE_Fatal:
return ErrFailure
}
return ErrIllegal
}
type OGRErr int
type OGRErrContainer struct {
ErrVal C.OGRErr
}
const (
OGRERR_NONE = OGRErr(C.OGRERR_NONE)
OGRERR_NOT_ENOUGH_DATA = OGRErr(C.OGRERR_NOT_ENOUGH_DATA)
OGRERR_NOT_ENOUGH_MEMORY = OGRErr(C.OGRERR_NOT_ENOUGH_MEMORY)
OGRERR_UNSUPPORTED_GEOMETRY_TYPE = OGRErr(C.OGRERR_UNSUPPORTED_GEOMETRY_TYPE)
OGRERR_UNSUPPORTED_OPERATION = OGRErr(C.OGRERR_UNSUPPORTED_OPERATION)
OGRERR_CORRUPT_DATA = OGRErr(C.OGRERR_CORRUPT_DATA)
OGRERR_FAILURE = OGRErr(C.OGRERR_FAILURE)
OGRERR_UNSUPPORTED_SRS = OGRErr(C.OGRERR_UNSUPPORTED_SRS)
OGRERR_INVALID_HANDLE = OGRErr(C.OGRERR_INVALID_HANDLE)
OGRERR_NON_EXISTING_FEATURE = OGRErr(C.OGRERR_NON_EXISTING_FEATURE)
)
func (err OGRErrContainer) Err() error {
switch OGRErr(err.ErrVal) {
case OGRERR_NONE:
return nil
case OGRERR_NOT_ENOUGH_DATA:
return ErrNotEnoughData
case OGRERR_NOT_ENOUGH_MEMORY:
return ErrNotEnoughMemory
case OGRERR_UNSUPPORTED_GEOMETRY_TYPE:
return ErrUnsupportedGeometryType
case OGRERR_UNSUPPORTED_OPERATION:
return ErrUnsupportedOperation
case OGRERR_CORRUPT_DATA:
return ErrCorruptData
case OGRERR_FAILURE:
return ErrFailure
case OGRERR_UNSUPPORTED_SRS:
return ErrUnsupportedSRS
case OGRERR_INVALID_HANDLE:
return ErrInvalidHandle
case OGRERR_NON_EXISTING_FEATURE:
return ErrNonExistingFeature
}
return ErrIllegal
}
// Pixel data types
type DataType int
const (
Unknown = DataType(C.GDT_Unknown)
Byte = DataType(C.GDT_Byte)
UInt16 = DataType(C.GDT_UInt16)
Int16 = DataType(C.GDT_Int16)
UInt32 = DataType(C.GDT_UInt32)
Int32 = DataType(C.GDT_Int32)
Float32 = DataType(C.GDT_Float32)
Float64 = DataType(C.GDT_Float64)
CInt16 = DataType(C.GDT_CInt16)
CInt32 = DataType(C.GDT_CInt32)
CFloat32 = DataType(C.GDT_CFloat32)
CFloat64 = DataType(C.GDT_CFloat64)
)
// Get data type size in bits.
func (dataType DataType) Size() int {
return int(C.GDALGetDataTypeSize(C.GDALDataType(dataType)))
}
func (dataType DataType) IsComplex() int {
return int(C.GDALDataTypeIsComplex(C.GDALDataType(dataType)))
}
func (dataType DataType) Name() string {
return C.GoString(C.GDALGetDataTypeName(C.GDALDataType(dataType)))
}
func (dataType DataType) Union(dataTypeB DataType) DataType {
return DataType(
C.GDALDataTypeUnion(C.GDALDataType(dataType), C.GDALDataType(dataTypeB)),
)
}
// Safe array conversion
func IntSliceToCInt(data []int) []C.int {
sliceSz := len(data)
result := make([]C.int, sliceSz)
for i := 0; i < sliceSz; i++ {
result[i] = C.int(data[i])
}
return result
}
// Safe array conversion
func CIntSliceToInt(data []C.int) []int {
sliceSz := len(data)
result := make([]int, sliceSz)
for i := 0; i < sliceSz; i++ {
result[i] = int(data[i])
}
return result
}
func CUIntBigSliceToInt(data []C.GUIntBig) []int {
sliceSz := len(data)
result := make([]int, sliceSz)
for i := 0; i < sliceSz; i++ {
result[i] = int(data[i])
}
return result
}
// status of the asynchronous stream
type AsyncStatusType int
const (
AR_Pending = AsyncStatusType(C.GARIO_PENDING)
AR_Update = AsyncStatusType(C.GARIO_UPDATE)
AR_Error = AsyncStatusType(C.GARIO_ERROR)
AR_Complete = AsyncStatusType(C.GARIO_COMPLETE)
)
func (statusType AsyncStatusType) Name() string {
return C.GoString(C.GDALGetAsyncStatusTypeName(C.GDALAsyncStatusType(statusType)))
}
func GetAsyncStatusTypeByName(statusTypeName string) AsyncStatusType {
name := C.CString(statusTypeName)
defer C.free(unsafe.Pointer(name))
return AsyncStatusType(C.GDALGetAsyncStatusTypeByName(name))
}
// Flag indicating read/write, or read-only access to data.
type Access int
const (
// Read only (no update) access
ReadOnly = Access(C.GA_ReadOnly)
// Read/write access.
Update = Access(C.GA_Update)
)
// Read/Write flag for RasterIO() method
type RWFlag int
const (
// Read data
Read = RWFlag(C.GF_Read)
// Write data
Write = RWFlag(C.GF_Write)
)
type OpenFlag uint
const (
OFReadOnly = OpenFlag(C.GDAL_OF_READONLY)
OFUpdate = OpenFlag(C.GDAL_OF_UPDATE)
OFShared = OpenFlag(C.GDAL_OF_SHARED)
OFVector = OpenFlag(C.GDAL_OF_VECTOR)
OFRaster = OpenFlag(C.GDAL_OF_RASTER)
OFVerbose_Error = OpenFlag(C.GDAL_OF_VERBOSE_ERROR)
)
// Types of color interpretation for raster bands.
type ColorInterp int
const (
CI_Undefined = ColorInterp(C.GCI_Undefined)
CI_GrayIndex = ColorInterp(C.GCI_GrayIndex)
CI_PaletteIndex = ColorInterp(C.GCI_PaletteIndex)
CI_RedBand = ColorInterp(C.GCI_RedBand)
CI_GreenBand = ColorInterp(C.GCI_GreenBand)
CI_BlueBand = ColorInterp(C.GCI_BlueBand)
CI_AlphaBand = ColorInterp(C.GCI_AlphaBand)
CI_HueBand = ColorInterp(C.GCI_HueBand)
CI_SaturationBand = ColorInterp(C.GCI_SaturationBand)
CI_LightnessBand = ColorInterp(C.GCI_LightnessBand)
CI_CyanBand = ColorInterp(C.GCI_CyanBand)
CI_MagentaBand = ColorInterp(C.GCI_MagentaBand)
CI_YellowBand = ColorInterp(C.GCI_YellowBand)
CI_BlackBand = ColorInterp(C.GCI_BlackBand)
CI_YCbCr_YBand = ColorInterp(C.GCI_YCbCr_YBand)
CI_YCbCr_CbBand = ColorInterp(C.GCI_YCbCr_CbBand)
CI_YCbCr_CrBand = ColorInterp(C.GCI_YCbCr_CrBand)
CI_Max = ColorInterp(C.GCI_Max)
)
func (colorInterp ColorInterp) Name() string {
return C.GoString(C.GDALGetColorInterpretationName(C.GDALColorInterp(colorInterp)))
}
func GetColorInterpretationByName(name string) ColorInterp {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
return ColorInterp(C.GDALGetColorInterpretationByName(cName))
}
// Types of color interpretations for a GDALColorTable.
type PaletteInterp int
const (
// Grayscale (in GDALColorEntry.c1)
PI_Gray = PaletteInterp(C.GPI_Gray)
// Red, Green, Blue and Alpha in (in c1, c2, c3 and c4)
PI_RGB = PaletteInterp(C.GPI_RGB)
// Cyan, Magenta, Yellow and Black (in c1, c2, c3 and c4)
PI_CMYK = PaletteInterp(C.GPI_CMYK)
// Hue, Lightness and Saturation (in c1, c2, and c3)
PI_HLS = PaletteInterp(C.GPI_HLS)
)
func (paletteInterp PaletteInterp) Name() string {
return C.GoString(C.GDALGetPaletteInterpretationName(C.GDALPaletteInterp(paletteInterp)))
}
// "well known" metadata items.
const (
MD_AREA_OR_POINT = string(C.GDALMD_AREA_OR_POINT)
MD_AOP_AREA = string(C.GDALMD_AOP_AREA)
MD_AOP_POINT = string(C.GDALMD_AOP_POINT)
)
/* -------------------------------------------------------------------- */
/* Define handle types related to various internal classes. */
/* -------------------------------------------------------------------- */
type MajorObject struct {
cval C.GDALMajorObjectH
}
type Dataset struct {
cval C.GDALDatasetH
}
type RasterBand struct {
cval C.GDALRasterBandH
}
type Driver struct {
cval C.GDALDriverH
}
type ColorTable struct {
cval C.GDALColorTableH
}
type RasterAttributeTable struct {
cval C.GDALRasterAttributeTableH
}
type AsyncReader struct {
cval C.GDALAsyncReaderH
}
type ColorEntry struct {
cval C.GDALColorEntry
}
func (ce *ColorEntry) Set(c1, c2, c3, c4 uint) {
ce.cval.c1 = C.short(c1)
ce.cval.c2 = C.short(c2)
ce.cval.c3 = C.short(c3)
ce.cval.c4 = C.short(c4)
}
func (ce *ColorEntry) Get() (c1, c2, c3, c4 uint8) {
return *(*uint8)(unsafe.Pointer(&ce.cval.c1)), *(*uint8)(unsafe.Pointer(&ce.cval.c2)), *(*uint8)(unsafe.Pointer(&ce.cval.c3)), *(*uint8)(unsafe.Pointer(&ce.cval.c4))
}
type VSILFILE struct {
cval *C.VSILFILE
}
/* -------------------------------------------------------------------- */
/* Callback "progress" function. */
/* -------------------------------------------------------------------- */
type ProgressFunc func(complete float64, message string, progressArg interface{}) int
func DummyProgress(complete float64, message string, data interface{}) int {
msg := C.CString(message)
defer C.free(unsafe.Pointer(msg))
retval := C.GDALDummyProgress(C.double(complete), msg, unsafe.Pointer(nil))
return int(retval)
}
func TermProgress(complete float64, message string, data interface{}) int {
msg := C.CString(message)
defer C.free(unsafe.Pointer(msg))
retval := C.GDALTermProgress(C.double(complete), msg, unsafe.Pointer(nil))
return int(retval)
}
func ScaledProgress(complete float64, message string, data interface{}) int {
msg := C.CString(message)
defer C.free(unsafe.Pointer(msg))
retval := C.GDALScaledProgress(C.double(complete), msg, unsafe.Pointer(nil))
return int(retval)
}
func CreateScaledProgress(min, max float64, progress ProgressFunc, data unsafe.Pointer) unsafe.Pointer {
panic("not implemented!")
return nil
}
func DestroyScaledProgress(data unsafe.Pointer) {
C.GDALDestroyScaledProgress(data)
}
// -----------------------------------------------------------------------
type goGDALProgressFuncProxyArgs struct {
progresssFunc ProgressFunc
data interface{}
}
//export goGDALProgressFuncProxyA
func goGDALProgressFuncProxyA(complete C.double, message *C.char, data unsafe.Pointer) int {
arg := (*goGDALProgressFuncProxyArgs)(data)
return arg.progresssFunc(
float64(complete), C.GoString(message), arg.data,
)
}
// CPLSetConfigOption
func CPLSetConfigOption(key, val string) {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
cVal := C.CString(val)
defer C.free(unsafe.Pointer(cVal))
C.CPLSetConfigOption(cKey, cVal)
}
// CPLGetConfigOption
func CPLGetConfigOption(key, val string) string {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
cVal := C.CString(val)
defer C.free(unsafe.Pointer(cVal))
return C.GoString(C.CPLGetConfigOption(cKey, cVal))
}
/* ==================================================================== */
/* Registration/driver related. */
/* ==================================================================== */
const (
DMD_LONGNAME = string(C.GDAL_DMD_LONGNAME)
DMD_HELPTOPIC = string(C.GDAL_DMD_HELPTOPIC)
DMD_MIMETYPE = string(C.GDAL_DMD_MIMETYPE)
DMD_EXTENSION = string(C.GDAL_DMD_EXTENSION)
DMD_CREATIONOPTIONLIST = string(C.GDAL_DMD_CREATIONOPTIONLIST)
DMD_CREATIONDATATYPES = string(C.GDAL_DMD_CREATIONDATATYPES)
DCAP_CREATE = string(C.GDAL_DCAP_CREATE)
DCAP_CREATECOPY = string(C.GDAL_DCAP_CREATECOPY)
DCAP_VIRTUALIO = string(C.GDAL_DCAP_VIRTUALIO)
DCAP_OPEN = string(C.GDAL_DCAP_OPEN)
DCAP_RASTER = string(C.GDAL_DCAP_RASTER)
DCAP_VECTOR = string(C.GDAL_DCAP_VECTOR)
DCAP_NOTNULL_FIELDS = string(C.GDAL_DCAP_NOTNULL_FIELDS)
DCAP_DEFAULT_FIELDS = string(C.GDAL_DCAP_DEFAULT_FIELDS)
DCAP_NOTNULL_GEOMFIELDS = string(C.GDAL_DCAP_NOTNULL_GEOMFIELDS)
DCAP_UNIQUE_FIELDS = string(C.GDAL_DCAP_UNIQUE_FIELDS)
)
// Create a new dataset with this driver.
func (driver Driver) Create(
filename string,
xSize, ySize, bands int,
dataType DataType,
options []string,
) Dataset {
name := C.CString(filename)
defer C.free(unsafe.Pointer(name))
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))
h := C.GDALCreate(
driver.cval,
name,
C.int(xSize), C.int(ySize), C.int(bands),
C.GDALDataType(dataType),
(**C.char)(unsafe.Pointer(&opts[0])),
)
return Dataset{h}
}
// Create a copy of a dataset
func (driver Driver) CreateCopy(
filename string,
sourceDataset Dataset,
strict int,
options []string,
progress ProgressFunc,
data interface{},
) Dataset {
name := C.CString(filename)
defer C.free(unsafe.Pointer(name))
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))
var h C.GDALDatasetH
if progress == nil {
h = C.GDALCreateCopy(
driver.cval, name,
sourceDataset.cval,
C.int(strict),
(**C.char)(unsafe.Pointer(&opts[0])),
nil,
nil,
)
} else {
arg := &goGDALProgressFuncProxyArgs{
progress, data,
}
h = C.GDALCreateCopy(
driver.cval, name,
sourceDataset.cval,
C.int(strict), (**C.char)(unsafe.Pointer(&opts[0])),
C.goGDALProgressFuncProxyB(),
unsafe.Pointer(arg),
)
}
return Dataset{h}
}
// Return the driver needed to access the provided dataset name.
func IdentifyDriver(filename string, filenameList []string) Driver {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
length := len(filenameList)
cFilenameList := make([]*C.char, length+1)
for i := 0; i < length; i++ {
cFilenameList[i] = C.CString(filenameList[i])
defer C.free(unsafe.Pointer(cFilenameList[i]))
}
cFilenameList[length] = (*C.char)(unsafe.Pointer(nil))
driver := C.GDALIdentifyDriver(cFilename, (**C.char)(unsafe.Pointer(&cFilenameList[0])))
return Driver{driver}
}
// Open an existing dataset
func Open(filename string, access Access) (Dataset, error) {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
dataset := C.GDALOpen(cFilename, C.GDALAccess(access))
if dataset == nil {
return Dataset{nil}, fmt.Errorf("Error: dataset '%s' open error", filename)
}
return Dataset{dataset}, nil
}
// Open an existing dataset
func OpenEx(filename string, flags OpenFlag, allowedDrivers []string,
openOptions []string, siblingFiles []string) (Dataset, error) {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
var driversA, ooptionsA, siblingsA **C.char
if allowedDrivers != nil {
length := len(allowedDrivers)
drivers := make([]*C.char, length+1)
for i := 0; i < length; i++ {
drivers[i] = C.CString(allowedDrivers[i])
defer C.free(unsafe.Pointer(drivers[i]))
}
drivers[length] = (*C.char)(unsafe.Pointer(nil))
driversA = (**C.char)(unsafe.Pointer(&drivers[0]))
}
if openOptions != nil {
length := len(openOptions)
ooptions := make([]*C.char, length+1)
for i := 0; i < length; i++ {
ooptions[i] = C.CString(openOptions[i])
defer C.free(unsafe.Pointer(ooptions[i]))
}
ooptions[length] = (*C.char)(unsafe.Pointer(nil))
ooptionsA = (**C.char)(unsafe.Pointer(&ooptions[0]))
}
if siblingFiles != nil {
length := len(siblingFiles)
siblings := make([]*C.char, length+1)
for i := 0; i < length; i++ {
siblings[i] = C.CString(siblingFiles[i])
defer C.free(unsafe.Pointer(siblings[i]))
}
siblings[length] = (*C.char)(unsafe.Pointer(nil))
siblingsA = (**C.char)(unsafe.Pointer(&siblings[0]))
}
dataset := C.GDALOpenEx(cFilename, C.uint(flags), driversA, ooptionsA, siblingsA)
if dataset == nil {
return Dataset{nil}, fmt.Errorf("Error: dataset '%s' openEx error", filename)
}
return Dataset{dataset}, nil
}
// Open a shared existing dataset
func OpenShared(filename string, access Access) Dataset {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
dataset := C.GDALOpenShared(cFilename, C.GDALAccess(access))
return Dataset{dataset}
}
// Unimplemented: DumpOpenDatasets
// Return the driver by short name
func GetDriverByName(driverName string) (Driver, error) {
cName := C.CString(driverName)
defer C.free(unsafe.Pointer(cName))
driver := C.GDALGetDriverByName(cName)
if driver == nil {
return Driver{driver}, fmt.Errorf("Error: driver '%s' not found", driverName)
}
return Driver{driver}, nil
}
// Fetch the number of registered drivers.
func GetDriverCount() int {
nDrivers := C.GDALGetDriverCount()
return int(nDrivers)
}
// Fetch driver by index
func GetDriver(index int) Driver {
driver := C.GDALGetDriver(C.int(index))
return Driver{driver}
}
// Destroy a GDAL driver
func (driver Driver) Destroy() {
C.GDALDestroyDriver(driver.cval)
}
// Registers a driver for use
func (driver Driver) Register() int {
index := C.GDALRegisterDriver(driver.cval)
return int(index)
}
// Reregister the driver
func (driver Driver) Deregister() {
C.GDALDeregisterDriver(driver.cval)
}
// Destroy the driver manager
func DestroyDriverManager() {
C.GDALDestroyDriverManager()
}
// Delete named dataset
func (driver Driver) DeleteDataset(name string) error {
cDriver := driver.cval
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
cErr := C.GDALDeleteDataset(cDriver, cName)
return CPLErrContainer{ErrVal: cErr}.Err()
}
// Rename named dataset
func (driver Driver) RenameDataset(newName, oldName string) error {
cDriver := driver.cval
cNewName := C.CString(newName)
defer C.free(unsafe.Pointer(cNewName))
cOldName := C.CString(oldName)
defer C.free(unsafe.Pointer(cOldName))
cErr := C.GDALRenameDataset(cDriver, cNewName, cOldName)
return CPLErrContainer{ErrVal: cErr}.Err()
}
// Copy all files associated with the named dataset
func (driver Driver) CopyDatasetFiles(newName, oldName string) error {
cDriver := driver.cval
cNewName := C.CString(newName)
defer C.free(unsafe.Pointer(cNewName))
cOldName := C.CString(oldName)
defer C.free(unsafe.Pointer(cOldName))
cErr := C.GDALCopyDatasetFiles(cDriver, cNewName, cOldName)
return CPLErrContainer{ErrVal: cErr}.Err()
}
// Get the short name associated with this driver
func (driver Driver) ShortName() string {
cDriver := driver.cval
return C.GoString(C.GDALGetDriverShortName(cDriver))
}
// Get the long name associated with this driver
func (driver Driver) LongName() string {
cDriver := driver.cval
return C.GoString(C.GDALGetDriverLongName(cDriver))
}
/* ==================================================================== */
/* GDAL_GCP */
/* ==================================================================== */
// Unimplemented: InitGCPs
// Unimplemented: DeinitGCPs
// Unimplemented: DuplicateGCPs
// Unimplemented: GCPsToGeoTransform
// Unimplemented: ApplyGeoTransform
/* ==================================================================== */
/* major objects (dataset, and, driver, drivermanager). */
/* ==================================================================== */
// Fetch object description
func (object MajorObject) Description() string {
cObject := object.cval
desc := C.GoString(C.GDALGetDescription(cObject))
return desc
}
// Set object description
func (object MajorObject) SetDescription(desc string) {
cObject := object.cval
cDesc := C.CString(desc)
defer C.free(unsafe.Pointer(cDesc))
C.GDALSetDescription(cObject, cDesc)
}
// Fetch metadata
func (object MajorObject) Metadata(domain string) []string {
panic("not implemented!")
return nil
}
func (dataset *Dataset) Metadata(domain string) []string {
cDomain := C.CString(domain)
defer C.free(unsafe.Pointer(cDomain))
p := C.GDALGetMetadata(
C.GDALMajorObjectH(unsafe.Pointer(dataset.cval)),
cDomain,
)
var strings []string
q := uintptr(unsafe.Pointer(p))
for {
p = (**C.char)(unsafe.Pointer(q))
if p == nil || *p == nil {
break
}
strings = append(strings, C.GoString(*p))
q += unsafe.Sizeof(q)
}
return strings
}
// Set metadata
func (object MajorObject) SetMetadata(metadata []string, domain string) {
panic("not implemented!")
return
}
// Fetch a single metadata item
func (object MajorObject) MetadataItem(name, domain string) string {
panic("not implemented!")
return ""
}
// Set a single metadata item
func (object MajorObject) SetMetadataItem(name, value, domain string) {
panic("not implemented!")
return
}
// TODO: Make correct class hirerarchy via interfaces
func (rasterBand *RasterBand) SetMetadataItem(name, value, domain string) error {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
c_value := C.CString(value)
defer C.free(unsafe.Pointer(c_value))
c_domain := C.CString(domain)
defer C.free(unsafe.Pointer(c_domain))
cErr := C.GDALSetMetadataItem(
C.GDALMajorObjectH(unsafe.Pointer(rasterBand.cval)),
c_name, c_value, c_domain,
)
return CPLErrContainer{ErrVal: cErr}.Err()
}
// TODO: Make korrekt class hirerarchy via interfaces
func (object *Dataset) SetMetadataItem(name, value, domain string) error {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
c_value := C.CString(value)
defer C.free(unsafe.Pointer(c_value))
c_domain := C.CString(domain)
defer C.free(unsafe.Pointer(c_domain))
cErr := C.GDALSetMetadataItem(
C.GDALMajorObjectH(unsafe.Pointer(object.cval)),
c_name, c_value, c_domain,
)
return CPLErrContainer{ErrVal: cErr}.Err()
}
// Fetch single metadata item.
func (object *Driver) MetadataItem(name, domain string) string {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
c_domain := C.CString(domain)
defer C.free(unsafe.Pointer(c_domain))
return C.GoString(
C.GDALGetMetadataItem(
C.GDALMajorObjectH(unsafe.Pointer(object.cval)),
c_name, c_domain,
),
)
}
func (object *Dataset) MetadataItem(name, domain string) string {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
c_domain := C.CString(domain)
defer C.free(unsafe.Pointer(c_domain))
return C.GoString(
C.GDALGetMetadataItem(
C.GDALMajorObjectH(unsafe.Pointer(object.cval)),
c_name, c_domain,
),
)
}
func (object *RasterBand) MetadataItem(name, domain string) string {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
c_domain := C.CString(domain)
defer C.free(unsafe.Pointer(c_domain))
return C.GoString(
C.GDALGetMetadataItem(
C.GDALMajorObjectH(unsafe.Pointer(object.cval)),
c_name, c_domain,
),
)
}
/* ==================================================================== */
/* GDALDataset class ... normally this represents one file. */
/* ==================================================================== */
// Get the driver to which this dataset relates
func (dataset Dataset) Driver() Driver {
driver := Driver{C.GDALGetDatasetDriver(dataset.cval)}
return driver
}
// Fetch files forming the dataset.
func (dataset Dataset) FileList() []string {
p := C.GDALGetFileList(dataset.cval)
var strings []string
q := uintptr(unsafe.Pointer(p))
for {
p = (**C.char)(unsafe.Pointer(q))
if *p == nil {
break
}
strings = append(strings, C.GoString(*p))
q += unsafe.Sizeof(q)
}
return strings
}
// Close the dataset
func (dataset Dataset) Close() {
C.GDALClose(dataset.cval)
return
}
// Fetch X size of raster
func (dataset Dataset) RasterXSize() int {
xSize := int(C.GDALGetRasterXSize(dataset.cval))
return xSize
}
// Fetch Y size of raster
func (dataset Dataset) RasterYSize() int {
ySize := int(C.GDALGetRasterYSize(dataset.cval))
return ySize
}
// Fetch the number of raster bands in the dataset
func (dataset Dataset) RasterCount() int {
count := int(C.GDALGetRasterCount(dataset.cval))
return count
}
// Fetch a raster band object from a dataset