/
ConstantTile.scala
776 lines (682 loc) · 23.8 KB
/
ConstantTile.scala
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
/*
* Copyright 2016 Azavea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package geotrellis.raster
import geotrellis.raster.resample._
import geotrellis.vector.Extent
import java.nio.ByteBuffer
import spire.syntax.cfor._
/**
* The trait underlying constant tile types.
*/
trait ConstantTile extends Tile {
/** Precomputed view of tile cells as seen by [[get]] method */
protected val iVal: Int
/** Precomputed view of tile cells as seen by [[getDouble]] method */
protected val dVal: Double
/**
* Fetch the datum at the given column and row of the tile.
*
* @param col The column
* @param row The row
* @return The Int datum found at the given location
*/
def get(col: Int, row: Int): Int = iVal
/**
* Fetch the datum at the given column and row of the tile.
*
* @param col The column
* @param row The row
* @return The Double datum found at the given location
*/
def getDouble(col: Int, row: Int): Double = dVal
/**
* Return the data behind this tile as an array of integers.
*
* @return The copy as an Array[Int]
*/
def toArray(): Array[Int] = Array.ofDim[Int](cols * rows).fill(iVal)
/**
* Return the data behind this tile as an array of doubles.
*
* @return The copy as an Array[Int]
*/
def toArrayDouble(): Array[Double] = Array.ofDim[Double](cols * rows).fill(dVal)
/**
* Returns a [[Tile]] equivalent to this tile, except with cells of
* the given type.
*
* @param newType The type of cells that the result should have
* @return The new Tile
*/
def convert(newType: CellType): Tile = {
if(newType.isFloatingPoint != cellType.isFloatingPoint)
logger.warn(s"Conversion from $cellType to $newType may lead to data loss.")
newType match {
case BitCellType => new BitConstantTile(if (iVal == 0) false else true, cols, rows)
case ct: ByteCells => ByteConstantTile(iVal.toByte, cols, rows, ct)
case ct: UByteCells => UByteConstantTile(iVal.toByte, cols, rows, ct)
case ct: ShortCells => ShortConstantTile(iVal.toShort , cols, rows, ct)
case ct: UShortCells => UShortConstantTile(iVal.toShort , cols, rows, ct)
case ct: IntCells => IntConstantTile(iVal , cols, rows, ct)
case ct: FloatCells => FloatConstantTile(dVal.toFloat , cols, rows, ct)
case ct: DoubleCells => DoubleConstantTile(dVal, cols, rows, ct)
}
}
def interpretAs(newCellType: CellType): Tile =
withNoData(None).convert(newCellType)
/**
* Execute a function on each cell of the tile. The function
* returns Unit, so it presumably produces side-effects.
*
* @param f A function from Int to Unit
*/
def foreach(f: Int => Unit) {
var i = 0
val len = size
while (i < len) { f(iVal); i += 1 }
}
/**
* Execute a function on each cell of the tile. The function
* returns Unit, so it presumably produces side-effects.
*
* @param f A function from Double to Unit
*/
def foreachDouble(f: Double => Unit) = {
var i = 0
val len = size
while (i < len) { f(dVal); i += 1 }
}
/**
* Execute an [[IntTileVisitor]] at each cell of the present tile.
*
* @param visitor An IntTileVisitor
*/
def foreachIntVisitor(visitor: IntTileVisitor): Unit = {
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
visitor(col, row, iVal)
}
}
}
/**
* Execute an [[DoubleTileVisitor]] at each cell of the present tile.
*
* @param visitor An DoubleTileVisitor
*/
def foreachDoubleVisitor(visitor: DoubleTileVisitor): Unit = {
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
visitor(col, row, dVal)
}
}
}
/**
* Map each cell in the given tile to a new one, using the given
* function.
*
* @param f A function from Int to Int, executed at each point of the tile
* @return The result, a [[Tile]]
*/
def map(f: Int => Int): Tile
/**
* Combine two tiles' cells into new cells using the given integer
* function. For every (x, y) cell coordinate, get each of the
* tiles' integer values, map them to a new value, and assign it to
* the output's (x, y) cell.
*
* @param other The other Tile
* @param f A function from (Int, Int) to Int
* @return The result, an Tile
*/
def combine(other: Tile)(f: (Int, Int) => Int): Tile = other.map(z => f(iVal, z))
/**
* Map each cell in the given tile to a new one, using the given
* function.
*
* @param f A function from Double to Double, executed at each point of the tile
* @return The result, a [[Tile]]
*/
def mapDouble(f: Double => Double): Tile = DoubleConstantTile(f(dVal), cols, rows)
/**
* Combine two tiles' cells into new cells using the given double
* function. For every (x, y) cell coordinate, get each of the
* tiles' double values, map them to a new value, and assign it to
* the output's (x, y) cell.
*
* @param other The other Tile
* @param f A function from (Int, Int) to Int
* @return The result, an Tile
*/
def combineDouble(other: Tile)(f: (Double, Double) => Double): Tile = other.mapDouble(z => f(dVal, z))
/**
* Map an [[IntTileMapper]] over the present tile.
*
* @param mapper The mapper
* @return The result, a [[Tile]]
*/
def mapIntMapper(mapper: IntTileMapper): Tile = {
val tile = ArrayTile.alloc(cellType, cols, rows)
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
tile.set(col, row, mapper(col, row, get(col, row)))
}
}
tile
}
/**
* Map an [[DoubleTileMapper]] over the present tile.
*
* @param mapper The mapper
* @return The result, a [[Tile]]
*/
def mapDoubleMapper(mapper: DoubleTileMapper): Tile = {
val tile = ArrayTile.alloc(cellType, cols, rows)
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
tile.setDouble(col, row, mapper(col, row, getDouble(col, row)))
}
}
tile
}
}
object ConstantTile {
/**
* Create a [[ConstantTile]] from a byte array.
*
* @param bytes The array of bytes, as provided by [[ConstantTile.toBytes()]]
* @param t The [[CellType]] of the new [[ConstantTile]]
* @param cols The number of columns that the new [[ConstantTile]] should have
* @param rows The number of rows that the new [[ConstantTile]] should have
* @return The new [[ConstantTile]]
*/
def fromBytes(bytes: Array[Byte], t: CellType, cols: Int, rows: Int): ConstantTile =
t match {
case _: BitCells => BitConstantTile.fromBytes(bytes, cols, rows)
case ct: ByteCells => ByteConstantTile.fromBytes(bytes, cols, rows, ct)
case ct: UByteCells => UByteConstantTile.fromBytes(bytes, cols, rows, ct)
case ct: ShortCells => ShortConstantTile.fromBytes(bytes, cols, rows, ct)
case ct: UShortCells => UShortConstantTile.fromBytes(bytes, cols, rows, ct)
case ct: IntCells => IntConstantTile.fromBytes(bytes, cols, rows, ct)
case ct: FloatCells => FloatConstantTile.fromBytes(bytes, cols, rows, ct)
case ct: DoubleCells => DoubleConstantTile.fromBytes(bytes, cols, rows, ct)
}
}
/**
* The companion object for the [[BitConstantTile]] type.
*/
object BitConstantTile {
/**
* A function which takes a number of columns and rows, and produces
* a new BitConstantTile.
*
* @param cols The number of columns
* @param rows The number of rows
* @return The BitConstantTile
*/
def apply(i: Int, cols: Int, rows: Int): BitConstantTile =
BitConstantTile(if(i == 0) false else true, cols, rows)
/**
* Produce a new [[BitConstantTile]] from an array of bytes.
*
* @param bytes The value to fill into the new tile
* @param cols The number of columns
* @param rows The number of rows
* @return The new BitArrayTile
*/
def fromBytes(bytes: Array[Byte], cols: Int, rows: Int): BitConstantTile = {
val interpreted = BitArrayTile.fromBytes(bytes, 1, 1)
BitConstantTile(interpreted.array(0), cols, rows)
}
}
/**
* The [[BitConstantTile]] type.
*/
case class BitConstantTile(v: Boolean, cols: Int, rows: Int) extends ConstantTile {
protected val iVal = if(v) 1 else 0
protected val dVal = if(v) 1.0 else 0.0
val cellType = BitCellType
/**
* Another name for the 'mutable' method on this class.
*
* @return An [[ArrayTile]]
*/
def toArrayTile(): ArrayTile = mutable()
/**
* Return the [[MutableArrayTile]] equivalent of this tile.
*
* @return The MutableArrayTile
*/
def mutable(): MutableArrayTile = BitArrayTile.fill(v, cols, rows)
/**
* Return the underlying data behind this tile as an array.
*
* @return An array of bytes
*/
def toBytes(): Array[Byte] = Array(iVal.toByte)
def withNoData(noDataValue: Option[Double]): ConstantTile = this
def map(f: Int => Int): Tile = BitConstantTile(f(iVal), cols, rows)
}
/**
* The [[ByteConstantTile]] type.
*/
case class ByteConstantTile(v: Byte, cols: Int, rows: Int,
cellType: ByteCells with NoDataHandling = ByteConstantNoDataCellType
) extends ConstantTile {
protected val (iVal: Int, dVal: Double) =
cellType match {
case _: ConstantNoData =>
(b2i(v), b2d(v))
case _: NoNoData =>
(v.toInt, v.toDouble)
case ct: ByteUserDefinedNoDataCellType =>
if (ct.noDataValue == v) (Int.MinValue, Double.NaN)
else (v.toInt, v.toDouble)
}
/**
* Another name for the 'mutable' method on this class.
*/
def toArrayTile(): ArrayTile = mutable
/**
* Return the [[MutableArrayTile]] equivalent of this tile.
*
* @return The MutableArrayTile
*/
def mutable(): MutableArrayTile = ByteArrayTile.fill(v, cols, rows, cellType)
/**
* Return the underlying data behind this tile as an array.
*
* @return An array of bytes
*/
def toBytes(): Array[Byte] = Array(v)
def withNoData(noDataValue: Option[Double]) =
ByteConstantTile(v, cols, rows, cellType.withNoData(noDataValue))
def map(f: Int => Int): Tile = ByteConstantTile(i2b(f(iVal)), cols, rows, cellType)
}
object ByteConstantTile {
/**
* Create a new [[ByteConstantTile]] from the array of bytes
* produced by [[ByteConstantTile.toBytes()]].
*
* @param bytes The value to fill into the new tile
* @param cols The number of columns
* @param rows The number of rows
* @param cellType The cell type from which to derive the NODATA
* @return The new [[ByteConstantTile]]
*/
def fromBytes(bytes: Array[Byte], cols: Int, rows: Int,
cellType: ByteCells with NoDataHandling): ByteConstantTile = {
val interpreted = ByteArrayTile.fromBytes(bytes, 1, 1, cellType)
ByteConstantTile(interpreted.array(0), cols, rows, cellType)
}
}
/**
* The [[UByteConstantTile]] type.
*/
case class UByteConstantTile(v: Byte, cols: Int, rows: Int,
cellType: UByteCells with NoDataHandling = UByteConstantNoDataCellType
) extends ConstantTile {
protected val (iVal: Int, dVal: Double) =
cellType match {
case _: ConstantNoData =>
(ub2i(v), ub2d(v))
case _: NoNoData =>
(v.toInt, v.toDouble)
case ct: UByteUserDefinedNoDataCellType =>
if (ct.noDataValue == v) (Int.MinValue, Double.NaN)
else (v.toInt, v.toDouble)
}
/**
* Another name for the 'mutable' method on this class.
*/
def toArrayTile(): ArrayTile = mutable()
/**
* Return the [[MutableArrayTile]] equivalent of this tile.
*
* @return The MutableArrayTile
*/
def mutable(): MutableArrayTile = UByteArrayTile.fill(v, cols, rows, cellType)
/**
* Return the underlying data behind this tile as an array.
*
* @return An array of bytes
*/
def toBytes(): Array[Byte] = Array(v)
/**
* Resample this [[ByteConstantTile]] to a target extent.
*
* @param current An extent (ignored)
* @param target The target extent
* @param method The resampling method (ignored)
* @return The resampled [[Tile]]
*/
def resample(current: Extent, target: RasterExtent, method: ResampleMethod): Tile =
ByteConstantTile(v, target.cols, target.rows)
def withNoData(noDataValue: Option[Double]) =
UByteConstantTile(v, cols, rows, cellType.withNoData(noDataValue))
def map(f: Int => Int): Tile = UByteConstantTile(i2ub(f(iVal)), cols, rows, cellType)
}
object UByteConstantTile {
/**
* Create a new [[UByteConstantTile]] from the array of bytes
* produced by [[UByteConstantTile.toBytes()]].
*
* @param bytes The value to fill into the new tile
* @param cols The number of columns
* @param rows The number of rows
* @param cellType The cell type from which to derive the NODATA
* @return The new [[UByteConstantTile]]
*/
def fromBytes(bytes: Array[Byte], cols: Int, rows: Int,
cellType: UByteCells with NoDataHandling): UByteConstantTile = {
val interpreted = UByteArrayTile.fromBytes(bytes, 1, 1, cellType)
UByteConstantTile(interpreted.array(0), cols, rows, cellType)
}
}
/**
* The [[ShortConstantTile]] type.
*/
case class ShortConstantTile(v: Short, cols: Int, rows: Int,
cellType: ShortCells with NoDataHandling = ShortConstantNoDataCellType
) extends ConstantTile {
protected val (iVal: Int, dVal: Double) =
cellType match {
case _: ConstantNoData =>
(s2i(v), s2d(v))
case _: NoNoData =>
(v.toInt, v.toDouble)
case ct: ShortUserDefinedNoDataCellType =>
if (ct.noDataValue == v) (Int.MinValue, Double.NaN)
else (v.toInt, v.toDouble)
}
/**
* Another name for the 'mutable' method on this class.
*/
def toArrayTile(): ArrayTile = mutable()
/**
* Return the [[MutableArrayTile]] equivalent of this tile.
*
* @return The MutableArrayTile
*/
def mutable(): MutableArrayTile = ShortArrayTile.fill(v, cols, rows, cellType)
/**
* Return the underlying data behind this tile as an array.
*
* @return An array of bytes
*/
def toBytes(): Array[Byte] = {
val arr = Array.ofDim[Byte](cellType.bytes)
ByteBuffer.wrap(arr).asShortBuffer.put(v)
arr
}
def withNoData(noDataValue: Option[Double]) =
ShortConstantTile(v, cols, rows, cellType.withNoData(noDataValue))
def map(f: Int => Int): Tile = ShortConstantTile(i2s(f(iVal)), cols, rows, cellType)
}
object ShortConstantTile {
/**
* Create a new [[ShortConstantTile]] from the array of bytes
* produced by [[ShortConstantTile.toBytes()]].
*
* @param bytes The value to fill into the new tile
* @param cols The number of columns
* @param rows The number of rows
* @param cellType The cell type from which to derive the NODATA
* @return The new [[ShortConstantTile]]
*/
def fromBytes(bytes: Array[Byte], cols: Int, rows: Int,
cellType: ShortCells with NoDataHandling): ShortConstantTile = {
val interpreted = ShortArrayTile.fromBytes(bytes, 1, 1, cellType)
ShortConstantTile(interpreted.array(0), cols, rows, cellType)
}
}
/**
* The [[UShortConstantTile]] type.
*/
case class UShortConstantTile(v: Short, cols: Int, rows: Int,
cellType: UShortCells with NoDataHandling = UShortConstantNoDataCellType
) extends ConstantTile {
protected val (iVal: Int, dVal: Double) =
cellType match {
case _: ConstantNoData =>
(us2i(v), us2d(v))
case _: NoNoData =>
(v.toInt, v.toDouble)
case ct: UShortUserDefinedNoDataCellType =>
if (ct.noDataValue == v) (Int.MinValue, Double.NaN)
else (v.toInt, v.toDouble)
}
/**
* Another name for the 'mutable' method on this class.
*/
def toArrayTile(): ArrayTile = mutable()
/**
* Return the [[MutableArrayTile]] equivalent of this tile.
*
* @return The MutableArrayTile
*/
def mutable(): MutableArrayTile = UShortArrayTile.fill(v, cols, rows, cellType)
/**
* Return the underlying data behind this tile as an array.
*
* @return An array of bytes
*/
def toBytes(): Array[Byte] = {
val arr = Array.ofDim[Byte](cellType.bytes)
ByteBuffer.wrap(arr).asShortBuffer.put(v)
arr
}
/**
* Resample this [[ShortConstantTile]] to a target extent.
*
* @param current An extent (ignored)
* @param target The target extent
* @param method The resampling method (ignored)
* @return The resampled [[Tile]]
*/
def resample(current: Extent, target: RasterExtent, method: ResampleMethod): Tile =
ShortConstantTile(v, target.cols, target.rows)
def withNoData(noDataValue: Option[Double]) =
UShortConstantTile(v, cols, rows, cellType.withNoData(noDataValue))
def map(f: Int => Int): Tile = UShortConstantTile(i2us(f(iVal)), cols, rows, cellType)
}
object UShortConstantTile {
/**
* Create a new [[UShortConstantTile]] from the array of bytes
* produced by [[UShortConstantTile.toBytes()]].
*
* @param bytes The value to fill into the new tile
* @param cols The number of columns
* @param rows The number of rows
* @param cellType The cell type from which to derive the NODATA
* @return The new [[UShortConstantTile]]
*/
def fromBytes(bytes: Array[Byte], cols: Int, rows: Int,
cellType: UShortCells with NoDataHandling): UShortConstantTile = {
val interpreted = UShortArrayTile.fromBytes(bytes, 1, 1, cellType)
UShortConstantTile(interpreted.array(0), cols, rows, cellType)
}
}
/**
* The [[IntConstantTile]] type.
*/
case class IntConstantTile(v: Int, cols: Int, rows: Int,
cellType: IntCells with NoDataHandling = IntConstantNoDataCellType
) extends ConstantTile {
protected val (iVal: Int, dVal: Double) =
cellType match {
case _: ConstantNoData =>
(v, i2d(v))
case _: NoNoData =>
(v, v.toDouble)
case ct: IntUserDefinedNoDataCellType =>
if (ct.noDataValue == v) (Int.MinValue, Double.NaN)
else (v.toInt, v.toDouble)
}
/**
* Another name for the 'mutable' method on this class.
*/
def toArrayTile(): ArrayTile = mutable()
/**
* Return the [[MutableArrayTile]] equivalent of this tile.
*
* @return The MutableArrayTile
*/
def mutable(): MutableArrayTile = IntArrayTile.fill(v, cols, rows, cellType)
/**
* Return the underlying data behind this tile as an array.
*
* @return An array of bytes
*/
def toBytes(): Array[Byte] = {
val arr = Array.ofDim[Byte](cellType.bytes)
ByteBuffer.wrap(arr).asIntBuffer.put(v)
arr
}
def withNoData(noDataValue: Option[Double]) =
IntConstantTile(v, cols, rows, cellType.withNoData(noDataValue))
def map(f: Int => Int): Tile = IntConstantTile(f(iVal), cols, rows, cellType)
}
object IntConstantTile {
/**
* Create a new [[IntConstantTile]] from the array of bytes
* produced by [[IntConstantTile.toBytes()]].
*
* @param bytes The value to fill into the new tile
* @param cols The number of columns
* @param rows The number of rows
* @param cellType The cell type from which to derive the NODATA
* @return The new [[IntConstantTile]]
*/
def fromBytes(bytes: Array[Byte], cols: Int, rows: Int,
cellType: IntCells with NoDataHandling): IntConstantTile = {
val interpreted = IntArrayTile.fromBytes(bytes, 1, 1, cellType)
IntConstantTile(interpreted.array(0), cols, rows, cellType)
}
}
/**
* The [[FloatConstantTile]] type.
*/
case class FloatConstantTile(v: Float, cols: Int, rows: Int,
cellType: FloatCells with NoDataHandling = FloatConstantNoDataCellType
) extends ConstantTile {
protected val (iVal: Int, dVal: Double) =
cellType match {
case _: ConstantNoData =>
(f2i(v), f2d(v))
case _: NoNoData =>
(v.toInt, v.toDouble)
case ct: FloatUserDefinedNoDataCellType =>
if (ct.noDataValue == v) (Int.MinValue, Double.NaN)
else (v.toInt, v.toDouble)
}
/**
* Another name for the 'mutable' method on this class.
*/
def toArrayTile(): ArrayTile = mutable()
/**
* Return the [[MutableArrayTile]] equivalent of this tile.
*
* @return The MutableArrayTile
*/
def mutable(): MutableArrayTile = FloatArrayTile.fill(v, cols, rows, cellType)
/**
* Return the underlying data behind this tile as an array.
*
* @return An array of bytes
*/
def toBytes(): Array[Byte] = {
val arr = Array.ofDim[Byte](cellType.bytes)
ByteBuffer.wrap(arr).asFloatBuffer.put(v)
arr
}
def withNoData(noDataValue: Option[Double]) =
FloatConstantTile(v, cols, rows, cellType.withNoData(noDataValue))
def map(f: Int => Int): Tile = FloatConstantTile(i2f(f(iVal)), cols, rows, cellType)
}
object FloatConstantTile {
/**
* Create a new [[FloatConstantTile]] from the array of bytes
* produced by [[FloatConstantTile.toBytes()]].
*
* @param bytes The value to fill into the new tile
* @param cols The number of columns
* @param rows The number of rows
* @param cellType The cell type from which to derive the NODATA
* @return The new [[FloatConstantTile]]
*/
def fromBytes(bytes: Array[Byte], cols: Int, rows: Int,
cellType: FloatCells with NoDataHandling): FloatConstantTile = {
val interpreted = FloatArrayTile.fromBytes(bytes, 1, 1, cellType)
FloatConstantTile(interpreted.array(0), cols, rows, cellType)
}
}
/**
* The [[DoubleConstantTile]] type.
*/
case class DoubleConstantTile(v: Double, cols: Int, rows: Int,
cellType: DoubleCells with NoDataHandling = DoubleConstantNoDataCellType
) extends ConstantTile {
protected val (iVal: Int, dVal: Double) =
cellType match {
case _: ConstantNoData =>
(d2i(v), v)
case _: NoNoData =>
(v.toInt, v)
case ct: DoubleUserDefinedNoDataCellType =>
if (ct.noDataValue == v) (Int.MinValue, Double.NaN)
else (v.toInt, v)
}
/**
* Another name for the 'mutable' method on this class.
*/
def toArrayTile(): ArrayTile = mutable()
/**
* Return the [[MutableArrayTile]] equivalent of this tile.
*
* @return The MutableArrayTile
*/
def mutable(): MutableArrayTile = DoubleArrayTile.fill(dVal, cols, rows, cellType)
/**
* Return the underlying data behind this tile as an array.
*
* @return An array of bytes
*/
def toBytes(): Array[Byte] = {
val arr = Array.ofDim[Byte](cellType.bytes)
ByteBuffer.wrap(arr).asDoubleBuffer.put(v)
arr
}
def withNoData(noDataValue: Option[Double]): ConstantTile =
DoubleConstantTile(v, cols, rows, cellType.withNoData(noDataValue))
def map(f: Int => Int): Tile = DoubleConstantTile(i2d(f(iVal)), cols, rows, cellType)
}
object DoubleConstantTile {
/**
* Create a new [[DoubleConstantTile]] from the array of bytes
* produced by [[DoubleConstantTile.toBytes()]].
*
* @param bytes The value to fill into the new tile
* @param cols The number of columns
* @param rows The number of rows
* @param cellType The cell type from which to derive the NODATA
* @return The new [[DoubleConstantTile]]
*/
def fromBytes(bytes: Array[Byte], cols: Int, rows: Int,
cellType: DoubleCells with NoDataHandling): DoubleConstantTile = {
val interpreted = DoubleArrayTile.fromBytes(bytes, 1, 1, cellType)
DoubleConstantTile(interpreted.array(0), cols, rows, cellType)
}
}