Evalscript fails to produce ndvi values

for the following request:

-d '{
  "input": {
	"bounds": {
	  "bbox": [
		12.44693,
		41.870072,
		12.541001,
		41.917096
	  ]
	},
	"data": [
	  {
		"dataFilter": {
		  "timeRange": {
			"from": "2024-03-29T00:00:00Z",
			"to": "2024-04-29T23:59:59Z"
		  }
		},
		"type": "sentinel-2-l2a"
	  }
	]
  },
  "output": {
	"width": 512,
	"height": 343.697,
	"responses": [
	  {
		"identifier": "default",
		"format": {
		  "type": "image/tiff"
		}
	  }
	]
  },
  "evalscript": "//VERSION=3\nfunction setup() {\n      return {\n        input: [{\n          bands: [\n            \"B04\", \"B08\", \"dataMask\"\n          ]\n        }],\n        output: [\n          {\n            id: \"ndvi\", bands: 1\n          },\n          {\n            id: \"dataMask\", bands: 1\n          }\n        ]\n      };\n    }\n   \n    function evaluatePixel(samples) {\n        let index = (samples.B08 - samples.B04) / (samples.B08+samples.B04);\n        return {\n            ndvi: [index],\n            dataMask: [samples.dataMask]\n        };\n    }"
}'

i want to get the ndvi values of the designated geometry, and for the ‘nodata’ pixel assign ‘undefined’ to it.
what is worng with the evalscript i am using please

Try this:

function setup() {
   return {
       input: ["B04", "B08", "dataMask"],
       output: {
          bands: 1,
          sampleType: "FLOAT32",
          nodataValue: NaN
       }
   };
}

function evaluatePixel(samples) {
   if(!samples.dataMask){
      return [NaN]
   }
   const ndvi = index(samples.B08, samples.B04);
   return [ndvi]
}
1 Like