-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathraster.nim
549 lines (478 loc) · 17.9 KB
/
raster.nim
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
#[
Author: Dr. John Lindsay
Created: January 18, 2018
Last Modified: January 21, 2018
License: MIT
Notes: Module for reading and writing Whitebox and ArcGIS ASCII raster formats.
]#
import byte_order_reader, byte_order_writer, strutils
import system, endians, os, math, sequtils
##########
# Errors #
##########
type FileIOError = object of Exception
############
# DataType #
############
type DataType = enum
f64, f32, i32, i16, i8, none
#############
# DataScale #
#############
type DataScale = enum
continuous, categorical, boolean
# type ByteOrder = enum
# lsbf, msbf
type Raster* = object
filename*: string
minimum*: float64
maximum*: float64
rows*: int
columns*: int
stacks*: int
north*: float64
south*: float64
east*: float64
west*: float64
dataType*: DataType
zUnits*: string
xyUnits*: string
projection*: string
dataScale*: DataScale
displayMinimum*: float64
displayMaximum*: float64
palette*: string
nodata*: float64
byteOrder*: Endianness
paletteNonlinearity*: float64
resolutionX*: float64
resolutionY*: float64
metadata*: seq[string]
values: seq[float64]
proc `$`*(self: Raster): string =
result = "Min:\t$1\n".format(self.minimum)
result.add("Max:\t$1\n".format(self.maximum))
result.add("Rows:\t$1\n".format(self.rows))
result.add("Cols:\t$1\n".format(self.columns))
result.add("Stacks:\t$1\n".format(self.stacks))
result.add("North:\t$1\n".format(self.north))
result.add("South:\t$1\n".format(self.south))
result.add("East:\t$1\n".format(self.east))
result.add("West:\t$1\n".format(self.west))
case self.dataType:
of DataType.f64:
result.add("Data Type:\tdouble\n")
of DataType.f32:
result.add("Data Type:\tfloat\n")
of DataType.i32:
result.add("Data Type:\ti32\n")
of DataType.i16:
result.add("Data Type:\tinteger\n")
else:
result.add("Data Type:\tbyte\n")
result.add("Z Units:\t$1\n".format(self.zUnits))
result.add("XY Units:\t$1\n".format(self.xyUnits))
result.add("Projection:\t$1\n".format(self.projection))
result.add("Data Scale:\t$1\n".format(self.dataScale))
result.add("Display Min:\t$1\n".format(self.displayMinimum))
result.add("Display Max:\t$1\n".format(self.displayMaximum))
result.add("Preferred Palette:\t$1\n".format(self.palette))
result.add("NoData:\t$1\n".format(self.nodata))
if self.byteOrder == littleEndian:
result.add("Byte Order:\tLITTLE_ENDIAN\n")
else:
result.add("Byte Order:\tBIG_ENDIAN\n")
result.add("Palette Nonlinearity:\t$1\n".format(self.paletteNonlinearity))
for v in self.metadata:
result.add("Metadata Entry:\t$1\n".format(v.replace(":", ";")))
proc `[]=`*(self: var Raster, row, column: int, value: float64) {.inline.} =
if row >= 0 and row < self.rows and column >= 0 and column < self.columns:
self.values[row * self.columns + column] = value
proc `[]`*(self: Raster, row, column: int): float64 {.inline.} =
if row < 0 or column < 0: return self.nodata
if row >= self.rows or column >= self.columns: return self.nodata
result = self.values[row * self.columns + column]
proc `+=`*(self: var Raster, other: Raster) {.inline.} =
if self.rows != other.rows or self.columns != other.columns:
raise newException(FileIOError, "Rasters must have the same dimensions")
for i in 0..<self.rows*self.columns:
if self.values[i] != self.nodata and other.values[i] != other.nodata:
self.values[i] += other.values[i]
proc `+=`*(self: var Raster, other: SomeNumber) {.inline.} =
for i in 0..<self.rows*self.columns:
if self.values[i] != self.nodata:
self.values[i] += float64(other)
proc `-=`*(self: var Raster, other: Raster) {.inline.} =
if self.rows != other.rows or self.columns != other.columns:
raise newException(FileIOError, "Rasters must have the same dimensions")
for i in 0..<self.rows*self.columns:
if self.values[i] != self.nodata and other.values[i] != other.nodata:
self.values[i] -= other.values[i]
proc `-=`*(self: var Raster, other: SomeNumber) {.inline.} =
for i in 0..<self.rows*self.columns:
if self.values[i] != self.nodata:
self.values[i] -= float64(other)
proc `*=`*(self: var Raster, other: Raster) {.inline.} =
if self.rows != other.rows or self.columns != other.columns:
raise newException(FileIOError, "Rasters must have the same dimensions")
for i in 0..<self.rows*self.columns:
if self.values[i] != self.nodata and other.values[i] != other.nodata:
self.values[i] *= other.values[i]
proc `*=`*(self: var Raster, other: SomeNumber) {.inline.} =
for i in 0..<self.rows*self.columns:
if self.values[i] != self.nodata:
self.values[i] *= float64(other)
proc `/=`*(self: var Raster, other: Raster) {.inline.} =
if self.rows != other.rows or self.columns != other.columns:
raise newException(FileIOError, "Rasters must have the same dimensions")
for i in 0..<self.rows*self.columns:
if self.values[i] != self.nodata and other.values[i] != other.nodata:
self.values[i] /= other.values[i]
proc `/=`*(self: var Raster, other: SomeNumber) {.inline.} =
for i in 0..<self.rows*self.columns:
if self.values[i] != self.nodata:
self.values[i] /= float64(other)
proc readWhiteboxRaster*(self: var Raster) =
self.metadata = newSeq[string]()
var fn =
if self.filename.toLowerAscii.contains(".dep"):
self.filename
else:
self.filename.replace(".tas", ".dep")
var f = open(fn)
defer: close(f)
for line in f.lines:
# echo line
let lcLine = line.toLowerAscii
if "min:" in lcLine and not lcLine.contains("display"):
self.minimum = parseFloat(line.split(":")[1].strip)
elif "max:" in lcLine and not lcLine.contains("display"):
self.maximum = parseFloat(line.split(":")[1].strip)
elif "rows:" in lcLine:
self.rows = parseInt(line.split(":")[1].strip)
elif "cols:" in lcLine:
self.columns = parseInt(line.split(":")[1].strip)
elif "stacks:" in lcLine:
self.stacks = parseInt(line.split(":")[1].strip)
elif "north:" in lcLine:
self.north = parseFloat(line.split(":")[1].strip)
elif "south:" in lcLine:
self.south = parseFloat(line.split(":")[1].strip)
elif "east:" in lcLine:
self.east = parseFloat(line.split(":")[1].strip)
elif "west:" in lcLine:
self.west = parseFloat(line.split(":")[1].strip)
elif "data type:" in lcLine:
let v = line.split(":")[1].strip.toLowerAscii
if "float" in v:
self.dataType = DataType.f32
elif "double" in v:
self.dataType = DataType.f64
elif "i32" in v:
self.dataType = DataType.i32
elif "short" in v or "integer" in v:
self.dataType = DataType.i16
elif "byte" in v:
self.dataType = DataType.i8
elif "z units:" in lcLine:
self.zUnits = line.split(":")[1].strip
elif "xy units:" in lcLine:
self.xyUnits = line.split(":")[1].strip
elif "projection:" in lcLine:
self.projection = line.split(":")[1].strip
elif "data scale:" in lcLine:
let v = line.split(":")[1].strip.toLowerAscii
if "continuous" in v:
self.dataScale = DataScale.continuous
elif "categorical" in v:
self.dataScale = DataScale.categorical
elif "bool" in v:
self.dataScale = DataScale.boolean
elif "display min:" in lcLine:
self.displayMinimum = parseFloat(line.split(":")[1].strip)
elif "display max:" in lcLine:
self.displayMaximum = parseFloat(line.split(":")[1].strip)
elif "preferred palette:" in lcLine:
self.palette = line.split(":")[1].strip
elif "nodata:" in lcLine:
self.nodata = parseFloat(line.split(":")[1].strip)
elif "data scale:" in lcLine:
let v = line.split(":")[1].strip.toLowerAscii
if "LITTLE" in v:
self.byteOrder = Endianness.littleEndian
else:
self.byteOrder = Endianness.bigEndian
elif "palette nonlinearity:" in lcLine:
self.paletteNonlinearity = parseFloat(line.split(":")[1].strip)
elif "metadata" in lcLine:
self.metadata.add(line.split(":")[1].strip)
self.resolutionX = (self.east - self.west) / float(self.columns)
self.resolutionY = (self.north - self.south) / float(self.rows)
self.values = newSeq[float64](self.rows * self.columns)
# for i in 0..<self.values.len:
# self.values[i] = self.nodata
fn =
if self.filename.toLowerAscii.contains(".dep"):
self.filename.replace(".dep", ".tas")
else:
self.filename
var fileSize = getFileSize(fn)
var buf = newSeq[uint8](fileSize)
var df = open(fn, fmRead)
defer: df.close()
discard df.readBytes(buf, Natural(0), Natural(fileSize))
var bor: ByteOrderReader = newByteOrderReader(buf, self.byteOrder)
let numPoints = self.rows * self.columns
case self.dataType:
of DataType.f64:
for i in 0..<numPoints:
self.values[i] = bor.readFloat64
of DataType.f32:
for i in 0..<numPoints:
self.values[i] = float64(bor.readFloat32)
of DataType.i32:
for i in 0..<numPoints:
self.values[i] = float64(bor.readInt32)
of DataType.i16:
for i in 0..<numPoints:
self.values[i] = float64(bor.readInt16)
else:
for i in 0..<numPoints:
self.values[i] = float64(bor.readInt8)
proc calculateMinAndMax(self: var Raster) =
for i in 0..<self.rows * self.columns:
let z = self.values[i]
if z != self.nodata:
if z < self.minimum:
self.minimum = z
if z > self.maximum:
self.maximum = z
proc writeWhiteboxRaster*(self: var Raster) =
try:
self.calculateMinAndMax()
if self.displayMaximum.classify == fcNegInf:
self.displayMaximum = self.maximum
if self.displayMinimum.classify == fcInf:
self.displayMinimum = self.minimum
# write the header file
var fn =
if self.filename.toLowerAscii.contains(".dep"):
self.filename
else:
self.filename.replace(".tas", ".dep")
let o = open(fn, fmWrite)
defer: o.close
o.writeLine(self)
# write the data file
let dataSize =
if self.dataType == DataType.f64:
8
elif self.dataType == DataType.f32 or self.dataType == DataType.i32:
4
elif self.dataType == DataType.i16:
2
else:
1
var fileSize = dataSize * self.rows * self.columns
fn =
if self.filename.toLowerAscii.contains(".dep"):
self.filename.replace(".dep", ".tas")
else:
self.filename
var df = open(fn, fmWrite)
defer: df.close()
var bow: ByteOrderWriter = newByteOrderWriter(fileSize, self.byteOrder)
let numPoints = self.rows * self.columns
case self.dataType:
of DataType.f64:
for i in 0..<numPoints:
bow.writeFloat64(self.values[i])
of DataType.f32:
var val: float32
for i in 0..<numPoints:
val = float32(self.values[i])
bow.writeFloat32(val)
of DataType.i32:
var val: int32
for i in 0..<numPoints:
val = int32(self.values[i])
bow.writeInt32(val)
of DataType.i16:
var val: int16
for i in 0..<numPoints:
val = int16(self.values[i])
bow.writeInt16(val)
else:
var val: int8
for i in 0..<numPoints:
val = int8(self.values[i])
bow.writeInt8(val)
discard df.writeBytes(bow.data, 0, fileSize)
except:
echo "Got exception ", repr(getCurrentException()), " with message ", getCurrentExceptionMsg()
proc readArcAscii(self: var Raster) =
try:
# read the file
var f = open(self.filename)
defer: close(f)
var xllcenter: float64 = NegInf
var yllcenter: float64 = NegInf
var xllcorner: float64 = NegInf
var yllcorner: float64 = NegInf
var index = 0
for line in f.lines:
var lineSplit = line.strip.split(" ")
if lineSplit.len() == 1:
lineSplit = line.strip.split("\t")
let lastIndex = len(lineSplit) - 1
if lineSplit[0].toLowerAscii().contains("nrows"):
self.rows = parseInt(lineSplit[lastIndex].strip)
elif lineSplit[0].toLowerAscii().contains("ncols"):
self.columns = parseInt(lineSplit[lastIndex].strip)
elif lineSplit[0].toLowerAscii().contains("xllcorner"):
xllcenter = parseFloat(lineSplit[lastIndex].strip)
elif lineSplit[0].toLowerAscii().contains("yllcorner"):
yllcenter = parseFloat(lineSplit[lastIndex].strip)
elif lineSplit[0].toLowerAscii().contains("xllcenter"):
xllcorner = parseFloat(lineSplit[lastIndex].strip)
elif lineSplit[0].toLowerAscii().contains("yllcenter"):
yllcorner = parseFloat(lineSplit[lastIndex].strip)
elif lineSplit[0].toLowerAscii().contains("cellsize"):
self.resolutionX = parseFloat(lineSplit[lastIndex].strip)
self.resolutionY = parseFloat(lineSplit[lastIndex].strip)
elif lineSplit[0].toLowerAscii().contains("nodata_value"):
if lineSplit[lastIndex].contains("."):
self.dataType = DataType.f32
else:
self.dataType = DataType.i32
self.nodata = parseFloat(lineSplit[lastIndex].strip)
else: # it's a data line
if self.values == @[]:
self.values = newSeq[float64](self.rows * self.columns)
for val in lineSplit:
if not val.isNilOrWhitespace():
self.values[index] = parseFloat(val.strip)
index += 1
# set the North, East, South, and West coodinates
if xllcorner.classify != fcNegInf:
self.east = xllcorner + float64(self.columns)*self.resolutionX
self.west = xllcorner
self.south = yllcorner
self.north = yllcorner + float64(self.rows)*self.resolutionY
else:
self.east = xllcenter - (0.5 * self.resolutionX) + float64(self.columns)*self.resolutionX
self.west = xllcenter - (0.5 * self.resolutionX)
self.south = yllcenter - (0.5 * self.resolutionY)
self.north = yllcenter - (0.5 * self.resolutionY) + float64(self.rows)*self.resolutionY
# initialize the other unused raster parameters
self.palette = "default"
self.paletteNonlinearity = 1'f64
self.projection = "not specified"
self.zUnits = "not specified"
self.xyUnits = "not specified"
self.byteOrder = Endianness.littleEndian
# the data will be unique to the new raster
self.minimum = Inf
self.maximum = NegInf
self.displayMinimum = Inf
self.displayMaximum = NegInf
except:
echo "Got exception ", repr(getCurrentException()), " with message ", getCurrentExceptionMsg()
proc writeArcAscii(self: var Raster) =
try:
# Save the file
let o = open(self.filename, fmWrite)
defer: o.close
o.writeLine("NCOLS $1".format(self.columns))
o.writeLine("NROWS $1".format(self.rows))
o.writeLine("XLLCORNER $1".format(self.west))
o.writeLine("YLLCORNER $1".format(self.south))
o.writeLine("CELLSIZE $1".format((self.resolutionX + self.resolutionY) / 2'f64))
o.writeLine("NODATA_VALUE $1".format(self.nodata))
# write the data
let numCells = self.rows * self.columns
var col = 0
var s = ""
for i in 0..numCells-1:
if col < self.columns - 1:
s.add("$1 ".format(self.values[i].formatFloat(ffDecimal, 2)))
else:
s.add("$1".format(self.values[i].formatFloat(ffDecimal, 2)))
col += 1
if col == self.columns:
o.writeLine(s)
s = ""
col = 0
except:
echo "Got exception ", repr(getCurrentException()), " with message ", getCurrentExceptionMsg()
proc read*(self: var Raster) =
if self.filename.toLowerAscii.endsWith(".dep") or self.filename.toLowerAscii.endsWith(".tas"):
self.readWhiteboxRaster()
elif self.filename.toLowerAscii.endsWith(".asc") or self.filename.toLowerAscii.endsWith(".txt"):
self.readArcAscii()
else:
raise newException(FileIOError, "Unrecognized raster file type")
proc write*(self: var Raster) =
if self.filename.toLowerAscii.endsWith(".dep") or self.filename.toLowerAscii.endsWith(".tas"):
self.writeWhiteboxRaster()
elif self.filename.toLowerAscii.endsWith(".asc") or self.filename.toLowerAscii.endsWith(".txt"):
self.writeArcAscii()
else:
raise newException(FileIOError, "Unrecognized raster file type")
proc newRasterFromFile*(filename: string): Raster =
# make sure it is a supported file format
if filename.toLowerAscii().endsWith(".dep") or filename.toLowerAscii().endsWith(".tas"):
result.filename = filename
elif filename.toLowerAscii().endsWith(".asc"):
result.filename = filename
else:
raise newException(FileIOError, "Unrecognized file extension")
result.read()
proc createRasterFromOther*(filename: string,
other: Raster,
dataType = DataType.none,
nodata=NegInf,
copyData=false): Raster =
# make sure it is a supported file format
if filename.toLowerAscii().endsWith(".dep") or filename.toLowerAscii().endsWith(".tas"):
result.filename = filename
elif fileName.toLowerAscii().endsWith(".asc"):
result.filename = filename
else:
raise newException(FileIOError, "Unrecognized file extension")
# copy parameters unrelated to the data and metadata
result.north = other.north
result.south = other.south
result.east = other.east
result.west = other.west
result.rows = other.rows
result.columns = other.columns
result.resolutionX = other.resolutionX
result.resolutionY = other.resolutionY
result.stacks = other.stacks
if nodata.classify == fcNegInf:
result.nodata = other.nodata
else:
result.nodata = nodata
result.dataScale = other.dataScale
if dataType == DataType.none:
result.dataType = other.dataType
else:
result.dataType = dataType
result.palette = other.palette
result.paletteNonlinearity = other.paletteNonlinearity
result.projection = other.projection
result.zUnits = other.zUnits
result.xyUnits = other.xyUnits
result.byteOrder = other.byteOrder
# the data will be unique to the new raster
result.minimum = Inf
result.maximum = NegInf
result.displayMinimum = Inf
result.displayMaximum = NegInf
if not copyData:
result.values = repeat(result.nodata, result.rows * result.columns)
else:
result.values = other.values
# the metadata will also be unique
result.metadata = newSeq[string]()