/
whiteboxRaster.go
675 lines (596 loc) · 18.6 KB
/
whiteboxRaster.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
// Copyright 2014 the GoSpatial Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// licence that can be found in the LICENCE.txt file.
// This file was originally created by John Lindsay<jlindsay@uoguelph.ca>,
// Nov. 2014.
// Package raster provides support for reading and creating various common
// geospatial raster data formats.
package raster
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"io/ioutil"
"math"
"os"
"path/filepath"
"strconv"
"strings"
)
// Used to manipulate an Whitebox raster (.dep) file.
type whiteboxRaster struct {
dataFile string
data []float64
header whiteboxRasterHeader
minimumValue float64
maximumValue float64
config *RasterConfig
}
func (r *whiteboxRaster) InitializeRaster(fileName string,
rows int, columns int, north float64, south float64,
east float64, west float64, config *RasterConfig) (err error) {
r.config = config
// set the various rows, columns, north, etc.
r.header.columns = columns
r.header.rows = rows
r.header.numCells = rows * columns
r.header.north = north
r.header.south = south
r.header.east = east
r.header.west = west
r.header.nodata = config.NoDataValue
r.config.ByteOrder = config.ByteOrder
r.config.RasterFormat = RT_WhiteboxRaster
// set the file names; if they exist, delete them
// sort out the names of the header and data files
ext := strings.ToLower(filepath.Ext(fileName))
if ext == ".tas" {
r.dataFile = fileName
r.header.fileName = strings.Replace(fileName, ext, ".dep", -1)
} else if ext == ".dep" {
r.header.fileName = fileName
r.dataFile = strings.Replace(fileName, ext, ".tas", -1)
} else {
return errors.New("Unrecognized file type.")
}
// do the files already exist? If yes, delete them.
if err = r.deleteFiles(); err != nil {
return err
}
// initialize the data array
r.data = make([]float64, r.header.numCells)
if config.InitialValue != 0 {
for i := range r.data {
r.data[i] = config.InitialValue
}
}
r.minimumValue = math.MaxFloat64
r.maximumValue = -math.MaxFloat64
return nil
}
// Retrieve the data file name (.tas) of this Whitebox raster file.
func (r *whiteboxRaster) FileName() string {
return r.dataFile
}
// Set the data file name (.tas) of this Whitebox raster file.
func (r *whiteboxRaster) SetFileName(value string) (err error) {
r.config = NewDefaultRasterConfig()
// sort out the names of the header and data files
ext := strings.ToLower(filepath.Ext(value))
if ext == ".tas" {
r.dataFile = value
r.header.fileName = strings.Replace(value, ext, ".dep", -1)
} else if ext == ".dep" {
r.header.fileName = value
r.dataFile = strings.Replace(value, ext, ".tas", -1)
} else {
return UnsupportedRasterFormatError
}
// does the file exist?
if _, err = os.Stat(r.header.fileName); err == nil {
// yes it does; read the file
if err = r.ReadFile(); err != nil {
return err
}
} else {
return FileDoesNotExistError
}
r.minimumValue = math.MaxFloat64
r.maximumValue = -math.MaxFloat64
r.config.RasterFormat = RT_WhiteboxRaster
return nil
}
// Retrieve the RasterType of this Raster.
func (r *whiteboxRaster) RasterType() RasterType {
return RT_WhiteboxRaster
}
// Retrieve the number of rows this binary raster file.
func (r *whiteboxRaster) Rows() int {
return r.header.rows
}
// Sets the number of rows of this binary raster file.
func (r *whiteboxRaster) SetRows(value int) {
r.header.rows = value
}
// Retrieve the number of columns of this binary raster file.
func (r *whiteboxRaster) Columns() int {
return r.header.columns
}
// Sets the number of columns of this binary raster file.
func (r *whiteboxRaster) SetColumns(value int) {
r.header.columns = value
}
// Retrieve the raster's northern edge's coordinate
func (r *whiteboxRaster) North() float64 {
return r.header.north
}
// Retrieve the raster's southern edge's coordinate
func (r *whiteboxRaster) South() float64 {
return r.header.south
}
// Retrieve the raster's eastern edge's coordinate
func (r *whiteboxRaster) East() float64 {
return r.header.east
}
// Retrieve the raster's western edge's coordinate
func (r *whiteboxRaster) West() float64 {
return r.header.west
}
// Retrieve the raster's minimum value
func (r *whiteboxRaster) MinimumValue() float64 {
if r.minimumValue == math.MaxFloat64 {
r.minimumValue, r.maximumValue = r.findMinAndMaxVals()
}
return r.minimumValue
}
// Retrieve the raster's minimum value
func (r *whiteboxRaster) MaximumValue() float64 {
if r.maximumValue == -math.MaxFloat64 {
r.minimumValue, r.maximumValue = r.findMinAndMaxVals()
}
return r.maximumValue
}
func (r *whiteboxRaster) findMinAndMaxVals() (minVal float64, maxVal float64) {
if r.data != nil && len(r.data) > 0 {
minVal = math.MaxFloat64
maxVal = -math.MaxFloat64
for _, v := range r.data {
if v != r.header.nodata {
if v > maxVal {
maxVal = v
}
if v < minVal {
minVal = v
}
}
}
return minVal, maxVal
} else {
return math.MaxFloat64, -math.MaxFloat64
}
}
// Sets the raster config
func (r *whiteboxRaster) SetRasterConfig(value *RasterConfig) {
r.config = value
}
// Retrieves the raster config
func (r *whiteboxRaster) GetRasterConfig() *RasterConfig {
return r.config
}
// Retrieve the NoData value used by this binary raster file.
func (r *whiteboxRaster) NoData() float64 {
return r.header.nodata
}
// Sets the NoData value used by this binary raster file.
func (r *whiteboxRaster) SetNoData(value float64) {
r.header.nodata = value
r.config.NoDataValue = value
}
// Retrieve the byte order used by this binary raster file.
func (r *whiteboxRaster) ByteOrder() binary.ByteOrder {
return r.config.ByteOrder
}
// Sets the byte order used by this binary raster file.
func (r *whiteboxRaster) SetByteOrder(value binary.ByteOrder) {
r.config.ByteOrder = value
}
// Retrieves the metadata for this raster
func (r *whiteboxRaster) MetadataEntries() []string {
return r.config.MetadataEntries
}
// Adds a metadata entry to this raster
func (r *whiteboxRaster) AddMetadataEntry(value string) {
mde := r.config.MetadataEntries
newSlice := make([]string, len(mde)+1)
for i, val := range mde {
if len(strings.TrimSpace(val)) > 0 {
newSlice[i] = val
}
}
newSlice[len(mde)] = value
r.config.MetadataEntries = newSlice
}
// Returns the data as a slice of float64 values
func (r *whiteboxRaster) Data() ([]float64, error) {
if len(r.data) == 0 {
r.ReadFile()
}
return r.data, nil
}
// Sets the data from a slice of float64 values
func (r *whiteboxRaster) SetData(values []float64) {
// make sure that the numCells is set
if r.header.numCells == 0 {
r.header.numCells = r.header.rows * r.header.columns
}
if len(values) == r.header.numCells {
r.data = values
} else {
panic(DataSetError)
}
}
// Returns the value within data
func (r *whiteboxRaster) Value(index int) float64 {
return r.data[index]
}
// Sets the value of index within data
func (r *whiteboxRaster) SetValue(index int, value float64) {
r.data[index] = value
}
//// Returns the value within ColorData
//func (r *whiteboxRaster) GetColor(index int) color.Color {
// return r.colorData[index]
//}
//// Sets the value of index within ColorData
//func (r *whiteboxRaster) SetColor(index int, value color.Color) {
// r.colorData[index] = value
//}
// Save the file
func (r *whiteboxRaster) Save() (err error) {
// do the files exist? If yes, delete them.
if err = r.deleteFiles(); err != nil {
return err
}
// write the header file
if err = r.writeHeaderFile(); err != nil {
return err
}
// write the data file
f, err := os.Create(r.dataFile)
r.check(err)
defer f.Close()
w := bufio.NewWriter(f)
buf := new(bytes.Buffer)
switch r.config.DataType {
case DT_FLOAT64:
if err = binary.Write(buf, r.config.ByteOrder, r.data); err != nil {
return FileWritingError
}
case DT_FLOAT32:
out := make([]float32, len(r.data))
for i := 0; i < len(r.data); i++ {
out[i] = float32(r.data[i])
}
if err = binary.Write(buf, r.config.ByteOrder, out); err != nil {
return FileWritingError
}
case DT_INT16:
out := make([]int16, len(r.data))
for i := 0; i < len(r.data); i++ {
out[i] = int16(r.data[i])
}
if err = binary.Write(buf, r.config.ByteOrder, out); err != nil {
return FileWritingError
}
case DT_INT8:
out := make([]int8, len(r.data))
for i := 0; i < len(r.data); i++ {
out[i] = int8(r.data[i])
}
if err = binary.Write(buf, r.config.ByteOrder, out); err != nil {
return FileWritingError
}
default:
return FileWritingError
}
w.Write(buf.Bytes())
w.Flush()
return nil
}
// Reads the file
func (r *whiteboxRaster) ReadFile() error {
// read the header file
err := r.readHeaderFile()
if err != nil {
return FileReadingError
}
// read the data file
bytedata, err := ioutil.ReadFile(r.dataFile)
buf := bytes.NewReader(bytedata)
r.header.numCells = r.header.columns * r.header.rows
r.data = make([]float64, r.header.numCells)
switch r.config.DataType {
case DT_FLOAT64:
err = binary.Read(buf, r.config.ByteOrder, &r.data)
r.check(err)
case DT_FLOAT32:
nativeData := make([]float32, r.header.numCells)
err = binary.Read(buf, r.config.ByteOrder, &nativeData)
r.check(err)
for i, value := range nativeData {
r.data[i] = float64(value)
}
nativeData = nil
case DT_INT16:
nativeData := make([]int16, r.header.numCells)
err = binary.Read(buf, r.config.ByteOrder, &nativeData)
r.check(err)
for i, value := range nativeData {
r.data[i] = float64(value)
}
nativeData = nil
case DT_INT8:
nativeData := make([]int8, r.header.numCells)
err = binary.Read(buf, r.config.ByteOrder, &nativeData)
r.check(err)
for i, value := range nativeData {
r.data[i] = float64(value)
}
nativeData = nil
default:
return FileReadingError
}
return nil
}
type whiteboxRasterHeader struct {
fileName string
rows int
columns int
numCells int
nodata float64
north float64
south float64
east float64
west float64
}
func (r *whiteboxRaster) readHeaderFile() error {
// read the header file
if r.header.fileName == "" {
return errors.New("Whitebox GAT raster header file not set properly.")
}
content, err := ioutil.ReadFile(r.header.fileName)
r.check(err)
str := strings.Replace(string(content), "\r\n", "\n", -1)
lines := strings.Split(str, "\n")
for a := 0; a < len(lines); a++ {
str = strings.ToLower(lines[a])
s := strings.Split(lines[a], "\t")
if strings.Contains(str, "min:") && !strings.Contains(str, "display") && !strings.Contains(str, "metadata entry") {
r.minimumValue, err = strconv.ParseFloat(s[len(s)-1], 64)
r.check(err)
} else if strings.Contains(str, "max:") && !strings.Contains(str, "display") && !strings.Contains(str, "metadata entry") {
r.maximumValue, err = strconv.ParseFloat(s[len(s)-1], 64)
r.check(err)
} else if strings.Contains(str, "display min") && !strings.Contains(str, "metadata entry") {
r.config.DisplayMinimum, err = strconv.ParseFloat(s[len(s)-1], 64)
r.check(err)
} else if strings.Contains(str, "display max") && !strings.Contains(str, "metadata entry") {
r.config.DisplayMaximum, err = strconv.ParseFloat(s[len(s)-1], 64)
r.check(err)
} else if strings.Contains(str, "north") && !strings.Contains(str, "metadata entry") {
r.header.north, err = strconv.ParseFloat(s[len(s)-1], 64)
r.check(err)
} else if strings.Contains(str, "south") && !strings.Contains(str, "metadata entry") {
r.header.south, err = strconv.ParseFloat(s[len(s)-1], 64)
r.check(err)
} else if strings.Contains(str, "east") && !strings.Contains(str, "metadata entry") {
r.header.east, err = strconv.ParseFloat(s[len(s)-1], 64)
r.check(err)
} else if strings.Contains(str, "west") && !strings.Contains(str, "metadata entry") {
r.header.west, err = strconv.ParseFloat(s[len(s)-1], 64)
r.check(err)
} else if strings.Contains(str, "cols") && !strings.Contains(str, "metadata entry") {
r.header.columns, err = strconv.Atoi(s[len(s)-1])
r.check(err)
} else if strings.Contains(str, "rows") && !strings.Contains(str, "metadata entry") {
r.header.rows, err = strconv.Atoi(s[len(s)-1])
r.check(err)
} else if strings.Contains(str, "stacks") && !strings.Contains(str, "metadata entry") {
r.config.NumberOfBands, err = strconv.Atoi(s[len(s)-1])
r.check(err)
} else if strings.Contains(str, "data type") && !strings.Contains(str, "metadata entry") {
dt := strings.ToLower(strings.TrimSpace(s[len(s)-1]))
if strings.Contains(dt, "double") {
r.config.DataType = DT_FLOAT64
} else if strings.Contains(dt, "float") {
r.config.DataType = DT_FLOAT32
} else if strings.Contains(dt, "int") {
r.config.DataType = DT_INT16
} else { // byte
r.config.DataType = DT_INT8
}
} else if strings.Contains(str, "data scale") && !strings.Contains(str, "metadata entry") {
str2 := strings.ToLower(strings.TrimSpace(s[len(s)-1]))
if str2 == "continuous" {
r.config.PhotometricInterpretation = 0
} else if str2 == "categorical" {
r.config.PhotometricInterpretation = 1
} else if str2 == "bool" {
r.config.PhotometricInterpretation = 2
} else if str2 == "rgb" {
r.config.PhotometricInterpretation = 3
} else { // continous is the default
r.config.PhotometricInterpretation = 0
}
} else if strings.Contains(str, "z units") && !strings.Contains(str, "metadata entry") {
r.config.ZUnits = strings.ToLower(strings.TrimSpace(s[len(s)-1]))
} else if strings.Contains(str, "xy units") && !strings.Contains(str, "metadata entry") {
r.config.XYUnits = strings.ToLower(strings.TrimSpace(s[len(s)-1]))
} else if strings.Contains(str, "projection") && !strings.Contains(str, "metadata entry") {
r.config.CoordinateRefSystemWKT = strings.TrimPrefix(lines[a], "Projection:\t")
} else if strings.Contains(str, "preferred palette") && !strings.Contains(str, "metadata entry") {
r.config.PreferredPalette = strings.ToLower(strings.TrimSpace(s[len(s)-1]))
} else if strings.Contains(str, "byteorder") && !strings.Contains(str, "metadata entry") {
if strings.Contains(strings.ToLower(s[len(s)-1]), "LITTLE_ENDIAN") {
r.config.ByteOrder = binary.LittleEndian
} else {
r.config.ByteOrder = binary.BigEndian
}
} else if strings.Contains(str, "nodata") && !strings.Contains(str, "metadata entry") {
r.header.nodata, err = strconv.ParseFloat(s[len(s)-1], 64)
r.check(err)
} else if strings.Contains(str, "metadata entry") {
value := strings.TrimSpace(s[len(s)-1])
value = strings.Replace(value, ";", ":", -1)
r.AddMetadataEntry(value)
//r.config.MetadataEntries = append(r.config.MetadataEntries, value)
}
}
r.header.numCells = r.header.rows * r.header.columns
return nil
}
func (r *whiteboxRaster) writeHeaderFile() (err error) {
f, err := os.Create(r.header.fileName)
r.check(err)
defer f.Close()
w := bufio.NewWriter(f)
var str string
r.minimumValue, r.maximumValue = r.findMinAndMaxVals()
str = "Min:\t" + strconv.FormatFloat(r.minimumValue, 'f', -1, 64)
_, err = w.WriteString(str + "\n")
r.check(err)
str = "Max:\t" + strconv.FormatFloat(r.maximumValue, 'f', -1, 64)
_, err = w.WriteString(str + "\n")
r.check(err)
str = "North:\t" + strconv.FormatFloat(r.header.north, 'f', -1, 64)
_, err = w.WriteString(str + "\n")
r.check(err)
str = "South:\t" + strconv.FormatFloat(r.header.south, 'f', -1, 64)
_, err = w.WriteString(str + "\n")
r.check(err)
str = "East:\t" + strconv.FormatFloat(r.header.east, 'f', -1, 64)
_, err = w.WriteString(str + "\n")
r.check(err)
str = "West:\t" + strconv.FormatFloat(r.header.west, 'f', -1, 64)
_, err = w.WriteString(str + "\n")
r.check(err)
str = "Cols:\t" + strconv.Itoa(r.header.columns)
_, err = w.WriteString(str + "\n")
r.check(err)
str = "Rows:\t" + strconv.Itoa(r.header.rows)
_, err = w.WriteString(str + "\n")
r.check(err)
str = "Stacks:\t" + strconv.Itoa(r.config.NumberOfBands)
_, err = w.WriteString(str + "\n")
r.check(err)
switch r.config.DataType {
case DT_FLOAT64:
str = "Data Type:\tDOUBLE"
case DT_INT16:
str = "Data Type:\tINTEGER"
case DT_INT8:
str = "Data Type:\tBYTE"
default:
str = "Data Type:\tFLOAT"
}
_, err = w.WriteString(str + "\n")
r.check(err)
str = "Z Units:\t" + r.config.ZUnits
_, err = w.WriteString(str + "\n")
r.check(err)
str = "XY Units:\t" + r.config.XYUnits
_, err = w.WriteString(str + "\n")
r.check(err)
if r.config.CoordinateRefSystemWKT == "" {
r.config.CoordinateRefSystemWKT = "not specified"
}
str = "Projection:\t" + r.config.CoordinateRefSystemWKT
_, err = w.WriteString(str + "\n")
r.check(err)
switch r.config.PhotometricInterpretation {
case 0:
str = "Data Scale:\tcontinuous"
_, err = w.WriteString(str + "\n")
r.check(err)
case 1:
str = "Data Scale:\tcategorical"
_, err = w.WriteString(str + "\n")
r.check(err)
case 2:
str = "Data Scale:\tboolean"
_, err = w.WriteString(str + "\n")
r.check(err)
case 3:
str = "Data Scale:\trgb"
_, err = w.WriteString(str + "\n")
r.check(err)
}
if r.config.DisplayMinimum == math.MaxFloat64 {
r.config.DisplayMinimum = r.minimumValue
}
str = "Display Min:\t" + strconv.FormatFloat(r.config.DisplayMinimum, 'f', -1, 64)
_, err = w.WriteString(str + "\n")
r.check(err)
if r.config.DisplayMaximum == -math.MaxFloat64 {
r.config.DisplayMaximum = r.maximumValue
}
str = "Display Max:\t" + strconv.FormatFloat(r.config.DisplayMaximum, 'f', -1, 64)
_, err = w.WriteString(str + "\n")
r.check(err)
if r.config.PreferredPalette == "not specified" {
r.config.PreferredPalette = "grey.pal"
}
str = "Preferred Palette:\t" + r.config.PreferredPalette
_, err = w.WriteString(str + "\n")
r.check(err)
str = "NoData:\t" + strconv.FormatFloat(r.header.nodata, 'f', -1, 64)
_, err = w.WriteString(str + "\n")
r.check(err)
if r.config.ByteOrder == binary.LittleEndian {
str = "Byte Order:\tLITTLE_ENDIAN"
_, err = w.WriteString(str + "\n")
r.check(err)
} else {
str = "Byte Order:\tBIG_ENDIAN"
_, err = w.WriteString(str + "\n")
r.check(err)
}
str = "Palette Nonlinearity:\t" + strconv.FormatFloat(r.config.PaletteNonlinearity, 'f', -1, 64)
_, err = w.WriteString(str + "\n")
r.check(err)
// write the metadata entries
for _, value := range r.config.MetadataEntries {
if len(strings.TrimSpace(value)) > 0 {
str = "Metadata Entry:\t" + strings.Replace(value, ":", ";", -1)
_, err = w.WriteString(str + "\n")
r.check(err)
}
}
w.Flush()
return nil
}
func (r *whiteboxRaster) check(e error) {
if e != nil {
panic(e)
}
}
func (h *whiteboxRasterHeader) check(e error) {
if e != nil {
panic(e)
}
}
func (r *whiteboxRaster) deleteFiles() (err error) {
// do the files exist?
if _, err = os.Stat(r.header.fileName); err == nil {
if err = os.Remove(r.header.fileName); err != nil {
return FileDeletingError
}
}
if _, err = os.Stat(r.dataFile); err == nil {
if err = os.Remove(r.dataFile); err != nil {
return FileDeletingError
}
}
return nil
}