0

I'm trying to translate the following JavaScript into R but having problems:

/*
Author of the script: Carlos Bentes
*/

// Normalized Difference Vegetation Index
var ndvi = (B08-B04)/(B08+B04);

// Threshold for vegetation
var veg_th = 0.4;

// Simple RGB
var R = 2.5*B04;
var G = 2.5*B03;
var B = 2.5*B02;

// Transform to Black and White
var Y = 0.2*R + 0.7*G + 0.1*B;
var pixel = [Y, Y, Y];

// Change vegetation color
if(ndvi >= veg_th)
  pixel = [0.1*Y, 1.8*Y, 0.1*Y];

return pixel;

This is the Green City Index which is part of the repository of custom scripts that can be used with Sentinel-Hub services.

Specifically I'm having issues with:

var pixel = [Y, Y, Y];

And

if(ndvi >= veg_th)
  pixel = [0.1*Y, 1.8*Y, 0.1*Y];

return pixel;

I think var pixel = [Y, Y, Y]; is the same as pixel <- c(Y, Y, Y)? Which means in the if statement I'd have:

if(ndvi >= veg_th){ 
   pixel <- c(0.1*Y, 1.8*Y, 0.1*Y) 
   return(pixel)
}

But I'm getting an error:

Error in if (ndvi >= veg_th) { : argument is not interpretable as logical

Some notes about the parts I do understand for those not familiar with sentinel:

  • B04 and B08 are raster bands from Sentinel so
  • ndvi is a raster as well (normalized difference vegetation index)
  • veg_th is a value set as a threshold for interpreting results of ndvi
  • R, G, and B are making a color composite from the sentinel rasters
Nick is tired
  • 6,860
  • 20
  • 39
  • 51

0 Answers0