Skip to content

Instantly share code, notes, and snippets.

@lossyrob
Created December 15, 2013 03:39
Show Gist options
  • Save lossyrob/7968574 to your computer and use it in GitHub Desktop.
Save lossyrob/7968574 to your computer and use it in GitHub Desktop.
GeoTrellis 0.9 code snippet for merging rasters.
import geotrellis._
import geotrellis.source._
import geotrellis.raster._
import geotrellis.data.arg._
object Main {
def main(args:Array[String]):Unit = {
println("Merging...")
GeoTrellis.init(GeoTrellisConfig("/home/rob/data/sf/catalog.json"))
try {
val outpath = "/home/rob/data/sf/dem/sf-dem.arg"
val outname = "sf-dem"
val names = (1 to 4).map(i => s"sf-dem$i")
val extent = Extent(-1.3642671917813445 * math.pow(10,7),
4537301.94769072,
-1.3618479632115778 * math.pow(10,7),
4556831.701332695)
val info = RasterSource(names.head).info.get
val rasterType = info.rasterType
val fullExtent = names.map(RasterSource(_).info.get.rasterExtent.extent).reduce(_.combine(_))
val fullRe = info.rasterExtent.createAligned(fullExtent)
val re = info.rasterExtent.createAligned(extent)
val data = RasterData.emptyByType(rasterType,fullRe.cols,fullRe.rows)
for(name <- names) {
println(s"Processing $name")
val rs = RasterSource(name)
val r = rs.get
for(col <- 0 until fullRe.cols;
row <- 0 until fullRe.rows) {
val (x,y) = fullRe.gridToMap(col,row)
val (dcol,drow) = r.rasterExtent.mapToGrid(x,y)
if(dcol >= 0 && dcol < r.rasterExtent.cols &&
drow >= 0 && drow < r.rasterExtent.rows) {
data.setDouble(col,row, r.getDouble(dcol,drow))
}
}
}
val raster = Raster(data.warp(fullRe,re),re)
ArgWriter(rasterType).write(outpath,raster,outname)
} finally {
GeoTrellis.shutdown
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment