NDWI Normalized Difference Water Index, PlanetScope

//VERSION=3
//NDWI

function setup() {
  return {
    input: [{ bands: ["green", "nir", "dataMask"] }],
    output: [
      { id: "default", bands: 4 },
    ]
  }
}

function evaluatePixel(sample) {
  let ndwi = index(green, nir);

  let id_default = colorBlend(ndwi,
    [-1, -0.5, -0.2, 0, 0.2, 0.5, 1.0],
    [
      [1, 0, 1],
      [1, 0.5, 0],
      [1, 1, 0],
      [0.2, 1, 0.5],
      [0, 0, 1],
      [0, 0, 0.3],
      [0, 0, 0],
    ]
  );

  return { default: [...id_default, sample.dataMask] };
}
//VERSION=3
//PlanetScope NDWI EO Browser

function setup() {
    return {
        input: [{
            bands: [
                "green",
                "nir",
                "dataMask",
                "clear"]
        }],
        output: [
            { id: "default", bands: 4 },
            { id: "index", bands: 1, sampleType: "FLOAT32" },
            { id: "eobrowserStats", bands: 2, sampleType: "FLOAT32" },
            { id: "dataMask", bands: 1 },
        ]
    }
}

function evaluatePixel(sample) {

    let ndwi = index(sample.green, sample.nir)

    const clear = sample.dataMask && sample.clear;

    let ndwi_colored = colorBlend(ndwi,
        [-1, -0.5, -0.2, 0, 0.2, 0.5, 1.0],
        [
            [1, 0, 1],
            [1, 0.5, 0],
            [1, 1, 0],
            [0.2, 1, 0.5],
            [0, 0, 1],
            [0, 0, 0.3],
            [0, 0, 0],
        ]
    );

    return {
        default: [...ndwi_colored, clear],
        index: [ndwi],
        eobrowserStats: [ndwi, !clear],
        dataMask: [sample.dataMask],
    };
}
//VERSION=3
//NDWI

function setup() {
  return {
    input: [{ bands: ["green", "nir"] }],
    output: [
      { id: "default", bands: 1, sampleType: "FLOAT32" },
    ]
  }
}

function evaluatePixel(sample) {
  let ndwi = index(sample.green, sample.nir);
  return { default: [isFinite(ndwi) ? ndwi : NaN] };
}

Evaluate and Visualize

The example data is using Planet Sandox data. This data is restricted to Sentinel Hub users with active paid plans. If you are already a Planet Customer, see here on how to get access.

General description of the script

The NDWI is useful for water body mapping, as water bodies strongly absorb light in visible to infrared electromagnetic spectrum. NDWI uses green and near infrared bands to highlight water bodies. It is sensitive to built-up land and can result in over-estimation of water bodies.

\[NDWI = \frac{green-nir}{green+nir}.\]

Values description: Index values greater than 0.5 usually correspond to water bodies. Vegetation usually corresponds to much smaller values and built-up areas to values between zero and 0.2.

Description of representative images

NDWI (for water content in leaves) of Rome.

NDWI of Rome

References

Source: https://en.wikipedia.org/wiki/Normalized_difference_water_index