Skip to content

Instantly share code, notes, and snippets.

@lossyrob
Created June 25, 2014 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lossyrob/2a8e1ba20b7094cf37e2 to your computer and use it in GitHub Desktop.
Save lossyrob/2a8e1ba20b7094cf37e2 to your computer and use it in GitHub Desktop.
AverageLotsOfRasters for GeoTrellis 0.8
package geotrellis.raster.op
import geotrellis._
import geotrellis.feature.Extent
import geotrellis.process._
import geotrellis.raster.op._
import geotrellis.testkit._
import geotrellis.raster.op.local.Add
import org.scalatest._
class AverageLotsOfRastersTest extends FunSuite
with TestServer {
val e = Extent(0.0, 0.0, 10.0, 10.0)
val re = RasterExtent(e, 1.0, 1.0, 10, 10)
def r(n:Int) = Raster(Array.fill(100)(n), re)
def r(n:Double) = Raster(Array.fill(100)(n), re)
def addInts(ns:Int*) = Add(ns.map(n => Literal(r(n))))
def addDoubles(ns:Double*) = Add(ns.map(n => r(n)))
test("average rasters in sequential groups to limit memory usage") {
val limit = 30
val ops:Seq[Op[Raster]] = (1.0 until 200.0 by 1.0).map { i:Double => Literal(r(i)) }
val count = ops.length
val dividedOps:Seq[Op[Raster]] = ops.map { rOp => local.Divide(rOp, count) }
val firstRaster:Op[Raster] = dividedOps.head
val groups = dividedOps.tail.grouped(limit).map { _.toArray }.toList
val rOps:Op[Raster] = groups.foldLeft (firstRaster) ((oldResult:Op[Raster], newOps:Array[Op[Raster]]) => (logic.WithResult(oldResult)({ oldResult => local.Add( local.Add(newOps), oldResult) })) )
val output = get(rOps)
}
test("avg double rasters concurrently") {
val limit = 30
val ops:Seq[Op[Raster]] = (1.0 until 200.0 by 1.0).map { i:Double => Literal(r(i)) }
val count = ops.length
val dividedOps:Seq[Op[Raster]] = ops.map { rOp => local.Divide(rOp, count) }
val firstRaster:Op[Raster] = dividedOps.head
val groups = dividedOps.tail.grouped(limit).map(local.Add(_) )
val ops2 = groups.toSeq.flaMapOps(seq => Add(seq))
val output = get(ops2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment