/
ArrayTile.scala
628 lines (573 loc) · 19.6 KB
/
ArrayTile.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
/*
* 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 spire.syntax.cfor._
/**
* [[ArrayTile]] provides access and update to the grid data of a
* tile. Designed to be a near drop-in replacement for Array in many
* cases.
*/
abstract class ArrayTile extends Tile with Serializable {
/**
* cols and rows are explicitly defined to help with the Grid[N].{cols | rows} specialized functions dispatch.
* See https://github.com/locationtech/geotrellis/issues/3427
*/
def cols: Int
def rows: Int
/**
* Return the [[ArrayTile]] equivalent of this ArrayTile.
*
* @return The object on which the method was invoked
*/
def toArrayTile() = this
/**
* Returns a [[Tile]] equivalent to this [[ArrayTile]], except with
* cells of the given type.
*
* @param targetCellType The type of cells that the result should have
* @return The new Tile
*/
def convert(targetCellType: CellType): ArrayTile = {
val tile = ArrayTile.alloc(targetCellType, cols, rows)
if(!cellType.isFloatingPoint) {
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
tile.set(col, row, get(col, row))
}
}
} else {
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
tile.setDouble(col, row, getDouble(col, row))
}
}
}
tile
}
def withNoData(noDataValue: Option[Double]): Tile
def interpretAs(newCellType: CellType): Tile
/**
* Execute a function on each cell of the [[ArrayTile]].
*
* @param f A function from Int to Unit. Presumably, the function is executed for side-effects.
*/
def foreach(f: Int => Unit): Unit = {
val len = size
var i = 0
while (i < len) {
f(apply(i))
i += 1
}
}
/**
* Execute a function on each cell of the [[ArrayTile]].
*
* @param f A function from Double to Unit. Presumably, the function is executed for side-effects.
*/
def foreachDouble(f: Double => Unit): Unit = {
val len = size
var i = 0
while (i < len) {
f(applyDouble(i))
i += 1
}
}
/**
* Execute an [[IntTileVisitor]] at each cell of the [[ArrayTile]].
*
* @param visitor An IntTileVisitor
*/
def foreachIntVisitor(visitor: IntTileVisitor): Unit = {
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
visitor(col, row, get(col, row))
}
}
}
/**
* Execute an [[DoubleTileVisitor]] at each cell of the [[ArrayTile]].
*
* @param visitor A DoubleTileVisitor
*/
def foreachDoubleVisitor(visitor: DoubleTileVisitor): Unit = {
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
visitor(col, row, getDouble(col, row))
}
}
}
/**
* 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 = {
val output = ArrayTile.alloc(cellType, cols, rows)
var i = 0
val len = size
while (i < len) {
output(i) = f(apply(i))
i += 1
}
output
}
/**
* 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 = {
val len = size
val tile = ArrayTile.alloc(cellType, cols, rows)
var i = 0
while (i < len) {
tile.updateDouble(i, f(applyDouble(i)))
i += 1
}
tile
}
/**
* 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
}
/**
* Combine two [[ArrayTile]]s' cells into new cells using the given
* integer function. For every (x, y) cell coordinate, get each of
* the ArrayTiles' integer values, map them to a new value, and
* assign it to the output's (x, y) cell.
*
* @param other The other ArrayTile
* @param f A function from (Int, Int) to Int
* @return The result, an ArrayTile
*/
def combine(other: ArrayTile)(f: (Int, Int) => Int): ArrayTile = {
(this, other).assertEqualDimensions()
val output = ArrayTile.alloc(cellType.union(other.cellType), cols, rows)
var i = 0
val len = size
while (i < len) {
output(i) = f(apply(i), other(i))
i += 1
}
output
}
/**
* Combine the cells of an [[ArrayTile]] and a [[Tile]] into a new
* Tile using the given function. For every (x, y) cell coordinate,
* get each of the Tiles' integer value, 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 match {
case ar: ArrayTile =>
combine(ar)(f)
case ct: ConstantTile =>
ct.combine(this)((z1, z2) => f(z2, z1))
case ct: CompositeTile =>
ct.combine(this)((z1, z2) => f(z2, z1))
case ct: CroppedTile =>
ct.combine(this)((z1, z2) => f(z2, z1))
case t =>
this.map((col, row, z) => f(z, t.get(col, row)))
}
/**
* Combine two [[ArrayTile]]s' cells into new cells using the given
* double function. For every (x, y) cell coordinate, get each of
* the ArrayTiles' double values, map them to a new value, and
* assign it to the output's (x, y) cell.
*
* @param other The other ArrayTile
* @param f A function from (Double, Double) to Double
* @return The result, an ArrayTile
*/
def combineDouble(other: ArrayTile)(f: (Double, Double) => Double): ArrayTile = {
(this, other).assertEqualDimensions()
val output = ArrayTile.alloc(cellType.union(other.cellType), cols, rows)
var i = 0
val len = size
while (i < len) {
output.updateDouble(i, f(applyDouble(i), other.applyDouble(i)))
i += 1
}
output
}
/**
* Combine the cells of an [[ArrayTile]] and a [[Tile]] into a new
* Tile using the given function. For every (x, y) cell coordinate,
* get 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 (Double, Double) to Double
* @return The result, an Tile
*/
def combineDouble(other: Tile)(f: (Double, Double) => Double): Tile = {
other match {
case ar: ArrayTile =>
combineDouble(ar)(f)
case ct: ConstantTile =>
ct.combineDouble(this)((z1, z2) => f(z2, z1))
case ct: CompositeTile =>
ct.combineDouble(this)((z1, z2) => f(z2, z1))
case t =>
this.mapDouble((col, row, z) => f(z, t.getDouble(col, row)))
}
}
/**
* Check for equality between the present [[ArrayTile]] and any
* other object.
*
* @param other The other object
* @return A boolean
*/
override def equals(other: Any): Boolean = other match {
case tile: ArrayTile => {
if (tile == null) return false
if (tile.cols != cols || tile.rows != rows) return false
if (tile.cellType != cellType) return false
var i = 0
if (cellType.isFloatingPoint)
while (i < size) {
val value = applyDouble(i)
val otherValue = tile.applyDouble(i)
// if both values are not NaNs and are not equal
if (!java.lang.Double.isNaN(value) && !java.lang.Double.isNaN(otherValue) && (value != otherValue)) return false
// if one of the values is a NaN
if((!java.lang.Double.isNaN(value) && java.lang.Double.isNaN(otherValue)) || java.lang.Double.isNaN(value) && !java.lang.Double.isNaN(otherValue)) return false
i += 1
}
else
while (i < size) {
if (apply(i) != tile(i)) return false
i += 1
}
true
}
case _ => false
}
/**
* Fetch the datum at the given index in the array.
*
* @param i The index
* @return The Int datum found at the index
*/
def apply(i: Int): Int
/**
* Fetch the datum at the given index in the array.
*
* @param i The index
* @return The Double datum found at the index
*/
def applyDouble(i: Int): Double
/**
* Fetch the datum at the given column and row of the
* [[ArrayTile]].
*
* @param col The column
* @param row The row
* @return The Int datum found at the given location
*/
def get(col: Int, row: Int) = apply(row * cols + col)
/**
* Fetch the datum at the given column and row of the
* [[ArrayTile]].
*
* @param col The column
* @param row The row
* @return The Double datum found at the given location
*/
def getDouble(col: Int, row: Int) = applyDouble(row * cols + col)
/**
* Return a copy of the present [[ArrayTile]].
*
* @return The copy
*/
def copy: ArrayTile
/**
* Return the underlying array of this [[ArrayTile]] as a list.
*
* @return The list
*/
def toList = toArray().toList
/**
* Return the under-laying array of this [[ArrayTile]] as a list.
*
* @return The list
*/
def toListDouble = toArrayDouble().toList
/**
* Return a copy of the underlying array of the present
* [[ArrayTile]].
*
* @return The copy as an Array[Int]
*/
def toArray(): Array[Int] = {
val len = size
val arr = Array.ofDim[Int](len)
var i = 0
while (i < len) {
arr(i) = apply(i)
i += 1
}
arr
}
/**
* Return a copy of the underlying array of the present
* [[ArrayTile]].
*
* @return The copy as an Array[Double]
*/
def toArrayDouble(): Array[Double] = {
val len = size
val arr = Array.ofDim[Double](len)
var i = 0
while (i < len) {
arr(i) = applyDouble(i)
i += 1
}
arr
}
override def toString: String = s"ArrayTile($cols,$rows,$cellType)"
}
/**
* An object housing apply methods which produce [[ArrayTile]]s.
*/
object ArrayTile {
/**
* Allocate a new [[MutableArrayTile]].
*
* @param t The [[CellType]] of the new [[MutableArrayTile]]
* @param cols The number of columns that the new [[MutableArrayTile]] should have
* @param rows The number of rows that the new [[MutableArrayTile]] should have
* @return The new [[MutableArrayTile]]
*/
def alloc(t: CellType, cols: Int, rows: Int): MutableArrayTile =
t match {
case _: BitCells => BitArrayTile.ofDim(cols, rows)
case ct: ByteCells => ByteArrayTile.ofDim(cols, rows, ct)
case ct: UByteCells => UByteArrayTile.ofDim(cols, rows, ct)
case ct: ShortCells => ShortArrayTile.ofDim(cols, rows, ct)
case ct: UShortCells => UShortArrayTile.ofDim(cols, rows, ct)
case ct: IntCells => IntArrayTile.ofDim(cols, rows, ct)
case ct: FloatCells => FloatArrayTile.ofDim(cols, rows, ct)
case ct: DoubleCells => DoubleArrayTile.ofDim(cols, rows, ct)
}
/**
* Create a new, empty [[MutableArrayTile]].
*
* @param t The [[CellType]] of the new [[MutableArrayTile]]
* @param cols The number of columns that the new [[MutableArrayTile]] should have
* @param rows The number of rows that the new [[MutableArrayTile]] should have
* @return The new [[MutableArrayTile]]
*/
def empty(t: CellType, cols: Int, rows: Int): MutableArrayTile =
t match {
case _: BitCells => BitArrayTile.empty(cols, rows)
case ct: ByteCells => ByteArrayTile.empty(cols, rows, ct)
case ct: UByteCells => UByteArrayTile.empty(cols, rows, ct)
case ct: ShortCells => ShortArrayTile.empty(cols, rows, ct)
case ct: UShortCells => UShortArrayTile.empty(cols, rows, ct)
case ct: IntCells => IntArrayTile.empty(cols, rows, ct)
case ct: FloatCells => FloatArrayTile.empty(cols, rows, ct)
case ct: DoubleCells => DoubleArrayTile.empty(cols, rows, ct)
}
/**
* Create a [[MutableArrayTile]] from a byte array.
*
* @param bytes The array of bytes
* @param t The [[CellType]] of the new [[MutableArrayTile]]
* @param cols The number of columns that the new [[MutableArrayTile]] should have
* @param rows The number of rows that the new [[MutableArrayTile]] should have
* @return The new [[MutableArrayTile]]
*/
def fromBytes(bytes: Array[Byte], t: CellType, cols: Int, rows: Int): MutableArrayTile =
t match {
case _: BitCells => BitArrayTile.fromBytes(bytes, cols, rows)
case ct: ByteCells => ByteArrayTile.fromBytes(bytes, cols, rows, ct)
case ct: UByteCells => UByteArrayTile.fromBytes(bytes, cols, rows, ct)
case ct: ShortCells => ShortArrayTile.fromBytes(bytes, cols, rows, ct)
case ct: UShortCells => UShortArrayTile.fromBytes(bytes, cols, rows, ct)
case ct: IntCells => IntArrayTile.fromBytes(bytes, cols, rows, ct)
case ct: FloatCells => FloatArrayTile.fromBytes(bytes, cols, rows, ct)
case ct: DoubleCells => DoubleArrayTile.fromBytes(bytes, cols, rows, ct)
}
/**
* Create a new [[ByteConstantNoDataArrayTile]] from an array of
* Bytes.
*
* @param arr The array of Bytes
* @param cols The number of columns in the new tile
* @param rows The number of rows in the new tile
* @return The newly-created tile
*/
def apply(arr: Array[Byte], cols: Int, rows: Int): ByteConstantNoDataArrayTile = new ByteConstantNoDataArrayTile(arr, cols, rows)
/**
* Create a new [[ShortConstantNoDataArrayTile]] from an array of
* Shorts.
*
* @param arr The array of Shorts
* @param cols The number of columns in the new tile
* @param rows The number of rows in the new tile
* @return The newly-created tile
*/
def apply(arr: Array[Short], cols: Int, rows: Int): ShortConstantNoDataArrayTile = new ShortConstantNoDataArrayTile(arr, cols, rows)
/**
* Create a new [[IntConstantNoDataArrayTile]] from an array of
* integers.
*
* @param arr The array of integers
* @param cols The number of columns in the new tile
* @param rows The number of rows in the new tile
* @return The newly-created tile
*/
def apply(arr: Array[Int], cols: Int, rows: Int): IntConstantNoDataArrayTile = new IntConstantNoDataArrayTile(arr, cols, rows)
/**
* Create a new [[FloatConstantNoDataArrayTile]] from an array of
* Floats.
*
* @param arr The array of Floats
* @param cols The number of columns in the new tile
* @param rows The number of rows in the new tile
* @return The newly-created tile
*/
def apply(arr: Array[Float], cols: Int, rows: Int): FloatConstantNoDataArrayTile = new FloatConstantNoDataArrayTile(arr, cols, rows)
/**
* Create a new [[DoubleConstantNoDataArrayTile]] from an array of
* Doubles.
*
* @param arr The array of Doubles
* @param cols The number of columns in the new tile
* @param rows The number of rows in the new tile
* @return The newly-created tile
*/
def apply(arr: Array[Double], cols: Int, rows: Int): DoubleConstantNoDataArrayTile = new DoubleConstantNoDataArrayTile(arr, cols, rows)
/** Pixel-wise combine two of tiles given a function over Ints.
*
* @param left input tile
* @param right input tile
* @param out mutable result tile
* @param f combine function
*/
def combine(left: Tile, right: Tile, out: MutableArrayTile, f: (Int, Int) => Int): Unit = {
cfor(0)(_ < left.rows, _ + 1) { row =>
cfor(0)(_ < left.cols, _ + 1) { col =>
val a = left.get(col, row)
val b = right.get(col, row)
out.set(col, row, f(a, b))
}
}
}
/** Pixel-wise combine two of tiles given a function over Doubles.
*
* @param left input tile
* @param right input tile
* @param out mutable result tile
* @param f combine function
*/
def combineDouble(left: Tile, right: Tile, out: MutableArrayTile, f: (Double, Double) => Double): Unit = {
cfor(0)(_ < left.rows, _ + 1) { row =>
cfor(0)(_ < left.cols, _ + 1) { col =>
val a = left.getDouble(col, row)
val b = right.getDouble(col, row)
out.setDouble(col, row, f(a, b))
}
}
}
}
/**
* An object housing apply methods which produce [[RawArrayTile]]s.
*/
object RawArrayTile {
/**
* Create a new [[ByteRawArrayTile]] from an array of Bytes.
*
* @param arr The array of Bytes
* @param cols The number of columns in the new tile
* @param rows The number of rows in the new tile
* @return The newly-created tile
*/
def apply(arr: Array[Byte], cols: Int, rows: Int): ByteRawArrayTile = new ByteRawArrayTile(arr, cols, rows)
/**
* Create a new [[ShortRawArrayTile]] from an array of Shorts.
*
* @param arr The array of Shorts
* @param cols The number of columns in the new tile
* @param rows The number of rows in the new tile
* @return The newly-created tile
*/
def apply(arr: Array[Short], cols: Int, rows: Int): ShortRawArrayTile = new ShortRawArrayTile(arr, cols, rows)
/**
* Create a new [[IntRawArrayTile]] from an array of integers.
*
* @param arr The array of integers
* @param cols The number of columns in the new tile
* @param rows The number of rows in the new tile
* @return The newly-created tile
*/
def apply(arr: Array[Int], cols: Int, rows: Int): IntRawArrayTile = new IntRawArrayTile(arr, cols, rows)
/**
* Create a new [[FloatRawArrayTile]] from an array of Floats.
*
* @param arr The array of Floats
* @param cols The number of columns in the new tile
* @param rows The number of rows in the new tile
* @return The newly-created tile
*/
def apply(arr: Array[Float], cols: Int, rows: Int): FloatRawArrayTile = new FloatRawArrayTile(arr, cols, rows)
/**
* Create a new [[DoubleRawArrayTile]] from an array of Doubles.
*
* @param arr The array of Doubles
* @param cols The number of columns in the new tile
* @param rows The number of rows in the new tile
* @return The newly-created tile
*/
def apply(arr: Array[Double], cols: Int, rows: Int): DoubleRawArrayTile = new DoubleRawArrayTile(arr, cols, rows)
}