GeoTrellis Operations

GeoTrellis provides a rich set of Map Algebra operations and other tile processing features that can be used with RasterFrames via Spark’s UDF support.

Here’s an example creating a UDFs to invoke the equalize transformation on each tile in a RasterFrame, and then compute the resulting per-tile mean of it.

scala> import geotrellis.raster.equalization._
import geotrellis.raster.equalization._

scala> val equalizer = udf((t: Tile) => t.equalize())
equalizer: org.apache.spark.sql.expressions.UserDefinedFunction = UserDefinedFunction(<function1>,org.apache.spark.sql.gt.types.TileUDT@5f9e4832,Some(List(org.apache.spark.sql.gt.types.TileUDT@5f9e4832)))

scala> val equalized = rf.select(equalizer($"tile") as "equalized")
equalized: org.apache.spark.sql.DataFrame = [equalized: rf_tile]

scala> equalized.select(tileMean($"equalized") as "equalizedMean").show(5, false)
+------------------+
|equalizedMean     |
+------------------+
|32776.88976377953 |
|32775.508514924004|
|30774.931392296894|
|32777.8624183605  |
|32775.86650796558 |
+------------------+
only showing top 5 rows

Here’s an example downsampling a tile and rendering each tile as a matrix of numerical values.

scala> val downsample = udf((t: Tile) => t.resample(4, 4))
downsample: org.apache.spark.sql.expressions.UserDefinedFunction = UserDefinedFunction(<function1>,org.apache.spark.sql.gt.types.TileUDT@5f9e4832,Some(List(org.apache.spark.sql.gt.types.TileUDT@5f9e4832)))

scala> val render = udf((t: Tile) => "\n" + t.asciiDraw() + "\n")
render: org.apache.spark.sql.expressions.UserDefinedFunction = UserDefinedFunction(<function1>,StringType,Some(List(org.apache.spark.sql.gt.types.TileUDT@5f9e4832)))

scala> val downsampled = rf.select(render(downsample($"tile")) as "minime")
downsampled: org.apache.spark.sql.DataFrame = [minime: string]

scala> downsampled.show(5, false)
+-------------------------------------------------------------------------------------------------------+
|minime                                                                                                 |
+-------------------------------------------------------------------------------------------------------+
|
  8837  8316 10855 10780
  8125 10082  9930  8290
  7624 10739 12019 11287
 11738  8125 11830 11260


|
|
 11278  8090  9976 10251
  9152  9952  8598  7867
 11155 10279  9599  7892
 16544 12812  9280 12004


|
|
    ND    ND    ND    ND
    ND    ND    ND    ND
    ND    ND    ND    ND
    ND    ND    ND    ND


|
|
  8116  7766 10988 11587
  8844  8386  8538  7900
  8219  8007  8739  8615
 10438 13469  8790 13562


|
|
  8149  7967 16175 11629
  8453  8974 10055 10609
 12485 12847 11454  9899
  7913 10969 11147  9728


|
+-------------------------------------------------------------------------------------------------------+
only showing top 5 rows