Which functions can be used in Custom Script?

Home > Faq > Which functions can be used in Custom Script?

Description of classes and useful methods

By employing the dynamically interpreted JavaScript language, and providing some specialized functions one can combine the bands of multispectral satellite data in unprecedented ways.

Scripts consist basically of two parts, setup and evaluation.

In setup part the input and output components are defined. For input components the used bands are specified. For output components the resulting array size is specified.

In evaluation part the values of each pixel are used to calculate the resulting value. Usually the calculated value is processed with visualizer before being returned. How visualizers process values is described in the next section.
Code example:

let viz = new HighlightCompressVisualizerSingle();

function evaluatePixel(samples) {
    let val = 1.5 * (samples[0].B08 - samples[0].B04) / (samples[0].B08 + samples[0].B04 + 0.5);
    return viz.process(val);
}

function setup(ds) {
    setInputComponents([ds.B04, ds.B08]);
    setOutputComponentCount(1);
}

Visualizer classes

Visualizers are basically classes with a method process which evaluates the representation value for a pixel from pixel’s band values.
In custom scripts some predefined visualization classes are already available. They are listed and described here.

class DefaultVisualizer

Visualizer with included gamma correction, gain and offset are also configurable. This class is useful for RGB images.

constructor(minValue=0.0, maxValue=1.0, gain=1.0, offset=0.0, gamma=1.0)

methods:

clamp(num, min, max)

Clamps number num between min and max

process(val)

Returns processed value between 0.0 and 1.0

processList(components)

Returns array of processed values

class: ColorGradientVisualizer

Visualizer interpolates colors in defined intervals(valColPairs). It maps color values to interval between min and max.

constructor(valColPairs, minVal=0.0, maxVal=1.0)

methods:

getColor(val)

Return interpolated color for value using array of boundaries

process(val)

Returns processed value in rgb (0 - 255)

processList(components)

Returns array of processed values(array of RGB arrays)

createRedTemperature(minVal, maxVal)

Returns RedTemperature visualizer

createWhiteGreen(minVal, maxVal)

Returns WhiteGreen visualizer

createBlueRed(minVal, maxVal)

Returns BlueRed visualizer
For predefined value color pairs see constants section below.

class HighlightCompressVisualizer

Automatic highlight adjustment with optional gamma correction. Gain and offset are configurable. This class is useful for RGB images.

constructor(minValue=0.0, maxValue=1.0, gain=1.0, offset=0.0, gamma=1.0)

methods:

clamp(num, min, max)

Clamps num between min and max

process(val)

Returns processed value between 0.0 and 1.0

processList(components)

Returns array of processed values

class: ColorMapVisualizer

This visualizer assigns a color to the input value according to color map. It takes an array of (value, color) pairs which determine the color map. The function process returns the color to which input color corresponds.

constructor(valColPairs)

methods:

getColorFromValue(val)

Returns corresponding RGB color from color map for value val.

process(val)

Returns processed value as RGB color array with values between 0.0 and 1.0

createDefaultColorMap()

Returns new ColorMapVisualizer([
[-1.0, 0x000000],
[-0.2, 0xFF0000],
[-0.1, 0x9A0000],
[0.0, 0x660000],
[0.1, 0xFFFF33],
[0.2, 0xCCCC33],
[0.3, 0x666600],
[0.4, 0x33FFFF],
[0.5, 0x33CCCC],
[0.6, 0x006666],
[0.7, 0x33FF33],
[0.8, 0x33CC33],
[0.9, 0x006600]
]);

class Identity

Visualizer that passes trough the value.
methods:

process(val)

Returns value as single element array

Helper functions

In the list are already implemented helper functions that can be used in custom scripts.

hex2rgb(hex)

Return 8bit RGB array of hex value.

rgb2hex(rgb)

Return hex representation of 8bit RGB vector.

index(x, y)

Return remote sensing index.
For example NDVI = index(B8A, B04)

inverse(x)

Returns 1 / x.
If x is zero the JAVA_DOUBLE_MAX_VAL constant is returned.

combine(col1, col2, alpha)

Return hex color of convex combination of hex colors.

returned_color = alpha * col1 + (1 – alpha) * col2

Constants

Predefined constants in the list below are already used in visualizers and are also available externally in custom scripts.

const JAVA_DOUBLE_MAX_VAL = 1.7976931348623157E308;
const blueRed = [
    [1.000, 0x000080],
    [0.875, 0x0000FF],
    [0.625, 0x00FFFF],
    [0.375, 0xFFFF00],
    [0.125, 0xFF0000],
    [0.000, 0x800000]
];

const redTemperature = [
    [1.000, 0x000000],
    [0.525, 0xAE0000],
    [0.300, 0xFF6E00],
    [0.250, 0xFF8600],
    [0.000, 0xFFFFFF]
];

const greenWhite = [
    [1.000, 0x000000],
    [0.600, 0x006600],
    [0.300, 0x80B300],
    [0.000, 0xFFFFFF]
];