Skip to content

Commit

Permalink
add multiband foreach to MultibandTile
Browse files Browse the repository at this point in the history
  • Loading branch information
hjaekel authored and lossyrob committed Sep 26, 2016
1 parent c9f2658 commit 7b11f94
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,144 @@ class GeoTiffMultibandTileSpec extends FunSpec
}
}

describe("GeoTiffMultibandTile multiband foreach") {
it("should multiband foreach all values, striped, pixel interleave") {

val tile =
MultibandGeoTiff(geoTiffPath("3bands/int32/3bands-striped-pixel.tif")).tile

val bandCount = tile.bandCount
val cellCount = tile.rows * tile.cols

var counts = 0
tile.foreach { value =>
value.length should be(bandCount)
counts += 1
}

counts should be(cellCount)
}

it("should multiband foreach all values, tiled, pixel interleave") {

val tile =
MultibandGeoTiff(geoTiffPath("3bands/int32/3bands-tiled-pixel.tif")).tile

val bandCount = tile.bandCount
val cellCount = tile.rows * tile.cols

var counts = 0
tile.foreach { value =>
value.length should be(bandCount)
counts += 1
}

counts should be(cellCount)
}

it("should multiband foreach all values, striped, band interleave") {

val tile =
MultibandGeoTiff(geoTiffPath("3bands/int32/3bands-striped-band.tif")).tile

val bandCount = tile.bandCount
val cellCount = tile.rows * tile.cols

var counts = 0
tile.foreach { value =>
value.length should be(bandCount)
counts += 1
}

counts should be(cellCount)
}

it("should multiband foreach all values, tiled, band interleave") {

val tile =
MultibandGeoTiff(geoTiffPath("3bands/int32/3bands-tiled-band.tif")).tile

val bandCount = tile.bandCount
val cellCount = tile.rows * tile.cols

var counts = 0
tile.foreach { value =>
value.length should be(bandCount)
counts += 1
}

counts should be(cellCount)
}

it("should multiband foreachDouble all values, striped, pixel interleave") {

val tile =
MultibandGeoTiff(geoTiffPath("3bands/int32/3bands-striped-pixel.tif")).tile

val bandCount = tile.bandCount
val cellCount = tile.rows * tile.cols

var counts = 0
tile.foreachDouble { value =>
value.length should be(bandCount)
counts += 1
}

counts should be(cellCount)
}

it("should multiband foreachDouble all values, tiled, pixel interleave") {

val tile =
MultibandGeoTiff(geoTiffPath("3bands/int32/3bands-tiled-pixel.tif")).tile

val bandCount = tile.bandCount
val cellCount = tile.rows * tile.cols

var counts = 0
tile.foreachDouble { value =>
value.length should be(bandCount)
counts += 1
}

counts should be(cellCount)
}

it("should multiband foreachDouble all values, striped, band interleave") {

val tile =
MultibandGeoTiff(geoTiffPath("3bands/int32/3bands-striped-band.tif")).tile

val bandCount = tile.bandCount
val cellCount = tile.rows * tile.cols

var counts = 0
tile.foreachDouble { value =>
value.length should be(bandCount)
counts += 1
}

counts should be(cellCount)
}

it("should multiband foreachDouble all values, tiled, band interleave") {

val tile =
MultibandGeoTiff(geoTiffPath("3bands/int32/3bands-tiled-band.tif")).tile

val bandCount = tile.bandCount
val cellCount = tile.rows * tile.cols

var counts = 0
tile.foreachDouble { value =>
value.length should be(bandCount)
counts += 1
}

counts should be(cellCount)
}
}

describe("GeoTiffMultibandTile combine") {
val original = IntConstantTile(199, 4, 4)

Expand Down
28 changes: 28 additions & 0 deletions raster/src/main/scala/geotrellis/raster/ArrayMultibandTile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,34 @@ class ArrayMultibandTile(_bands: Array[Tile]) extends MultibandTile with MacroMu
band(b0) foreachDouble f
}

def foreach(f: Array[Int] => Unit): Unit = {
var i = 0
cfor(0)(_ < cols, _ + 1) { col =>
cfor(0)(_ < rows, _ + 1) { row =>
val bandValues = Array.ofDim[Int](bandCount)
cfor(0)(_ < bandCount, _ + 1) { band =>
bandValues(band) = bands(band).get(col, row)
}
f(bandValues)
i += 1
}
}
}

def foreachDouble(f: Array[Double] => Unit): Unit = {
var i = 0
cfor(0)(_ < cols, _ + 1) { col =>
cfor(0)(_ < rows, _ + 1) { row =>
val bandValues = Array.ofDim[Double](bandCount)
cfor(0)(_ < bandCount, _ + 1) { band =>
bandValues(band) = bands(band).getDouble(col, row)
}
f(bandValues)
i += 1
}
}
}

/**
* Combine a subset of the bands of a [[ArrayMultibandTile]] into a
* new integer-valued [[MultibandTile]] using the function f.
Expand Down
18 changes: 18 additions & 0 deletions raster/src/main/scala/geotrellis/raster/MultibandTile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ trait MultibandTile extends CellGrid with MacroCombinableMultibandTile[Tile] wit
*/
def foreachDouble(b0: Int)(f: Double => Unit): Unit

/**
* Multiband iterate over tile's int value using a function that
* takes in an array of values, and returns the foreached
* value for that cell value.
*
* @param f The function
*/
def foreach(f: Array[Int] => Unit): Unit

/**
* Multiband iterate over tile's double value using a function that
* takes in an array of values, and returns the foreached
* value for that cell value.
*
* @param f The function
*/
def foreachDouble(f: Array[Double] => Unit): Unit

/**
* Combine a subset of the bands of a tile into a new
* integer-valued multiband tile using the function f.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,34 @@ abstract class GeoTiffMultibandTile(
}
}

def foreach(f: Array[Int] => Unit): Unit = {
var i = 0
cfor(0)(_ < cols, _ + 1) { col =>
cfor(0)(_ < rows, _ + 1) { row =>
val bandValues = Array.ofDim[Int](bandCount)
cfor(0)(_ < bandCount, _ + 1) { band =>
bandValues(band) = bands(band).get(col, row)
}
f(bandValues)
i += 1
}
}
}

def foreachDouble(f: Array[Double] => Unit): Unit = {
var i = 0
cfor(0)(_ < cols, _ + 1) { col =>
cfor(0)(_ < rows, _ + 1) { row =>
val bandValues = Array.ofDim[Double](bandCount)
cfor(0)(_ < bandCount, _ + 1) { band =>
bandValues(band) = bands(band).getDouble(col, row)
}
f(bandValues)
i += 1
}
}
}

/**
* Piggy-back on the other combine method to support combing a
* subset of the bands.
Expand Down

0 comments on commit 7b11f94

Please sign in to comment.