TypeError: Cannot read property 'B08' of undefined

Hi,

I have a script which suppose to return the difference between two NDVI images from two last dates.
I wrote my evalscript to take the B04 and B08, take the most recent images,calc NDVI and then return one band image with the normalized difference.
However,
I get the error:

“status”:400,“reason”:“Bad Request”,“message”:“Failed to evaluate script!\nevalscript.js:18: TypeError: Cannot read property ‘B08’ of undefined\n let date1=index(sample[0].B08, sample[0].B04);\n ^\nTypeError: Cannot read property ‘B08’ of undefined\n at evaluatePixel (evalscript.js:18:30)\n at executeForSingleScene (:1119:17)\n”,“code”:“RENDERER_EXCEPTION”}

My evalscript:

eval_dates = '''
VERSION=3
function setup() {
  return {
    input: ["B04", "B08", "CLM"],
    output: {
      bands: 1, 
      mosaicking: "mostRecent",
      sampleType: "FLOAT32"
    }
  };
}

function evaluatePixel(sample) {
    if (sample.CLM == 1) {
        return [null]
    }
    let date1=index(sample[0].B08, sample[0].B04);
    let date2=index(sample[1].B08, sample[1].B04);
    let difference=index(date1,date2);
    return [difference];
}
'''

#Run the script

box_size,bbox,bbox_coords_wgs84=get_bbox_from_shape(gdf,10)
time_range=('2022-05-31','2022-07-07')

img=get_image(eval_dates ,DataCollection.SENTINEL2_L2A,time_range,bbox,bbox_size,folder)

I followed this tutorial to write the evalscript,please help me find the error :slight_smile:

Thanks

Hi Ruth!

In your Evalscript you are trying to access positions in an array of samples by doing sample[0] or sample[1], meaning that you are assuming that your sample contains multiple dates. However, you have not specified the mosaicking parameter, which defaults to SIMPLE.

Looking at the documentation in the mosaicking section we can read that:

SIMPLE (default) - the simplest method, it flattens the mosaicked image so only a single sample is passed to evaluation.

and in the samples section of the documentation, we can see that when mosaicking is SIMPLE, then sample is an object containing the band values of the single mosaicked sample.

This means that you cannot access positions in your sample variable.

For your specific purpose, I would recommend using the ORBIT mosaicking parameter in your Evalscript:

ORBIT - the mosaicked image is flattened for each orbit so that there is only one sample per pixel per orbit. Multiple samples can therefore be present if there is more than one orbit for the selected time range at the pixel location.

which in turn will influence your sample:

When mosaicking is TILE or ORBIT:

samples - an array of samples as defined in the SIMPLE case. None, one or multiple samples can therefore be present depending on how many orbits/tiles there are for the selected time range and area of interest. Pixel values of a band can be accessed for each sample as an item of the array, e.g. samples[0].B02.

1 Like

Thanks for the quick answer!

I am a bit confused. I think I mismatched two parameters, the Mosaic and mosaicikingOrder.
I asked here in the forum few weeks ago about the order I get the images,and then Grega told me that in order to get the most recent image as 0 I need to use mosaicking “mostRecent” , which I also used here:

function setup() {
  return {
    input: ["B04", "B08", "CLM"],
    output: {
      bands: 1, 
      mosaicking: "mostRecent",
      sampleType: "FLOAT32"
    }

however, I have tried to change only the mosaiciking from “mostRecent” to ORBIT,

eval_dates = '''
VERSION=3
function setup() {
  return {
    input: ["B04", "B08", "CLM"],
    output: {
      bands: 1, 
      mosaicking: "ORBIT",
      sampleType: "FLOAT32"
    }
  };
}

but still getting the same error :frowning:

I believe you are confusing two different parameters here, hopefully the explanations below will make it clear:

  • mosaicking is a parameter that is set in the Evalscript. As stated in the documentation, the parameter defines how the source data is mosaicked. The only three values the parameter can take are: SIMPLE, ORBIT or TILE.

  • mosaickingOrder is a parameter that is set in the request (just like the bounding box or dates). When there are multiple acquisitions, the parameter defines the order in which the acquisitions are returned. There are also three values this parameter can take: mostRecent, leastRecent and leastCC.

Let’s look at an example to clear things up:

Say you pick an area and a date range where there are two Sentinel-2 acquisitions:

Rome town center [2022-06-23 to 2022-06-30]: the two available dates are 2022-06-24 and 2022-06-29.

  • With mosaicking: "ORBIT" and mosaickingOrder: "mostRecent" (in your payload):
    sample[0] will correspond to the image on 2022-06-29 and sample[1] to the image on 2022-06-24.

  • With mosaicking: "ORBIT" and mosaickingOrder: "leastRecent" (in your payload):
    sample[0]will correspond to the image on 2022-06-24 and sample[1] to the image on 2022-06-29.

  • With mosaicking: "SIMPLE" and mosaickingOrder: "mostRecent" (in your payload):
    → there is no more sample[0] and sample[1], just sample that corresponds to the image on 2022-06-29

  • With mosaicking: "SIMPLE" and mosaickingOrder: "leastRecent" (in your payload):
    → there is no more sample[0] and sample[1], just sample that corresponds to the image on 2022-06-24

1 Like