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.rf.TileUDT@1c078424,Some(List(org.apache.spark.sql.rf.TileUDT@1c078424)))

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

scala> equalized.select(tile_mean($"equalized") as "equalizedMean").show(5, false)
+------------------+
|equalizedMean     |
+------------------+
|32634.257767197705|
|32777.45174876396 |
|32776.88976377953 |
|32775.86650796558 |
|32779.3839956052  |
+------------------+
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.rf.TileUDT@1c078424,Some(List(org.apache.spark.sql.rf.TileUDT@1c078424)))

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

scala> downsampled.select(tile_to_array_double($"minime") as "cell_values").limit(2).show(false)
+-----------------------------------------------------------------------------------------------------------------------------------------+
|cell_values                                                                                                                              |
+-----------------------------------------------------------------------------------------------------------------------------------------+
|[8136.0, 11087.0, 10196.0, 10105.0, 9959.0, 11311.0, 9853.0, 12560.0, 10539.0, 10032.0, 8157.0, 11072.0, 8208.0, 8686.0, 7857.0, 7889.0] |
|[8837.0, 8316.0, 10855.0, 10780.0, 8125.0, 10082.0, 9930.0, 8290.0, 7624.0, 10739.0, 12019.0, 11287.0, 11738.0, 8125.0, 11830.0, 11260.0]|
+-----------------------------------------------------------------------------------------------------------------------------------------+