This topic describes how to use custom user-defined functions (UDFs) on DLA Ganos. In addition to built-in UDFs, DLA Ganos allows you to extend functions by using custom UDFs. This provides an easy way for interconnection between DLA Ganos and your business system.

Procedure

The following procedure describes how to calculate the normalized difference vegetation index (NDVI) by using a custom UDF on DLA Ganos.

  1. Initialize SparkSession and DLA Ganos.
      implicit val spark = SparkSession
        .builder()
        .master("local[*]")
        .appName(getClass.getName)
        .withKryoSerialization
        .getOrCreate()
        .withGanosRaster
    
      import spark.implicits. _
  2. Load the raster data layer.
    def readTiff(name: String) =
        SinglebandGeoTiff(IOUtils.toByteArray(getClass.getResourceAsStream(s"/$name")))
    def redBand = readTiff("L8-B4-Elkton-VA.tiff").projectedRaster.toLayer("red_band")
    def nirBand = readTiff("L8-B5-Elkton-VA.tiff").projectedRaster.toLayer("nir_band")
  3. Configure a custom UDF.
     val ndvi = udf((red: Tile, nir: Tile) => {
        val redd = red.convert(DoubleConstantNoDataCellType)
        val nird = nir.convert(DoubleConstantNoDataCellType)
        (nird - redd) / (nird + redd)
      })
  4. Calculate the NDVI and provide the output result.
    val df = redBand.spatialJoin(nirBand).withColumn("ndvi", ndvi($"red_band", $"nir_band")).asLayer
    val pr = df.toRaster($"ndvi", 233, 214)
    val brownToGreen = ColorRamp(
        RGB(166, 97, 26),
        RGB(223, 194, 125),
        RGB(245, 245, 245),
        RGB(128, 205, 193),
        RGB(1, 133, 113)
      ).stops(128)
    val colors = ColorMap.fromQuantileBreaks(pr.tile.histogramDouble(), brownToGreen)
    pr.tile.color(colors).renderPng().write("ndvi.png")
    The following figure shows the output results.1221