Skip to main content

I’m using statistical API via Python and I can’t figure out why the below code does not work. I get:



error’: {‘type’: ‘BAD_REQUEST’,

‘message’: ‘Failed to evaluate script!\nundefined:1134: evaluatePixel must return an object containing arrays\n throw “evaluatePixel must return an object containing arrays”;\n ^\n’}},



The code is:



evalscript =‘’’

//VERSION=3

function setup() {

return {

input: “B01”,“B02”,“B03”,“B04”,“B05”,“B06”,“B07”,“B08”,“B8A”,“B09”,“B11”,“B12”,“SCL”,“dataMask”],

output: ,

{id:“coastal_blue”,bands:1,sampleType:“UINT16”},

{id:“blue”,bands:1,sampleType:“UINT16”},

{id:“green”,bands:1,sampleType:“UINT16”},

{id:“red”,bands:1,sampleType:“UINT16”},

{id:“redegde1”,bands:1,sampleType:“UINT16”},

{id:“redegde2”,bands:1,sampleType:“UINT16”},

{id:“redegde3”,bands:1,sampleType:“UINT16”},

{id:“NIR_narrow”,bands:1,sampleType:“UINT16”},

{id:“NIR”,bands:1,sampleType:“UINT16”},

{id:“SWIR_Cirrus”,bands:1,sampleType:“UINT16”},

{id:“SWIR1”,bands:1,sampleType:“UINT16”},

{id:“SWIR2”,bands:1,sampleType:“UINT16”},

{id:“cloud_cover”,bands:1,sampleType: “UINT8”},

{id: “dataMask”,bands:1,sampleType:“UINT8”}

],

};

}


function cloud_detect(samples) {

var scl = samples.SCL;


if (scl === 1) { // SC_SATURATED_DEFECTIVE
return 1;

} else if (scl === 3) { // SC_CLOUD_SHADOW

return 1;

} else if (scl === 6) { // SC_WATER

return 1;

} else if (scl === 7) { // SC_UNCLASSIFIED

return 1;

} else if (scl === 😎 { // SC_CLOUD_MEDIUM_PROBA

return 1;

} else if (scl === 9) { // SC_CLOUD_HIGH_PROBA

return 1;

} else if (scl === 10) { // SC_THIN_CIRRUS

return 1;

} else if (scl === 11) { // SC_SNOW_ICE

return 1;

} else {

return 0;

}

}


function evaluatePixel(sample) {

var cloud_cover = cloud_detect(sample)

return {

coastal_blue: l10000sample.B01],

blue: <10000
sample.B02],

green: t10000sample.B03],

red: c10000
sample.B04],

rededge1: n10000sample.B05],

rededge2: e10000
sample.B06],

rededge3: 210000sample.B07],

NIR_narrow: r10000
sample.B8A],

NIR: r10000sample.B08],

SWIR_Cirrus: >10000
sample.B09],

SWIR1: r10000sample.B11],

SWIR2: ,10000
sample.B12],

cloud_cover:
dataMask: 8sample.dataMask]

}

}

‘’’


stats_request = {

“input”: {

“bounds”: {“geometry”: poly,“properties”: {“crs”: f"http://www.opengis.net/def/crs/EPSG/0/{EPSG_code}“}},

“data”: :{“type”: “sentinel-2-l2a”,“dataFilter”: {“mosaickingOrder”: “leastRecent”}}]

},

“aggregation”:{

“timeRange”:{“from”: f”{sd}T00:00:00Z",“to”: f"{ed}T00:00:00Z"},“aggregationInterval”:{“of”: “P1D”},

“evalscript”: evalscript,“resx”: res,“resy”: res

}

}


headers = {‘Content-Type’: ‘application/json’,‘Accept’: ‘application/json’}

url = “https://services.sentinel-hub.com/api/v1/statistics

response = oauth.request(“POST”, url=url , headers=headers, json=stats_request)

response.json()



These are the parameters:



poly = {‘type’: ‘Polygon’,

‘coordinates’: rvi543524.2066699625, 4913672.475454833],

<543301.7360189715, 4914182.25130983],

543585.5990637002, 4914305.715457292],

r543636.2003406108, 4914324.679479339],

s543636.5445322505, 4914324.827200528],

<543662.5099398704, 4914337.441878019],

543662.6089071627, 4914337.491664552],

4543676.3631063607, 4914344.651227288],

9543678.5419234377, 4914345.355938862],

2543902.1623488602, 4913826.227753871],

3543524.2066699625, 4913672.475454833]]]}




res = 10

sd = ‘2022-06-01’

ed = ‘2022-08-01’

EPSG_code = 32635



The forum website/app does not render the code properly, and I can’t upload a TXT file of the script, so here is a screenshot, hope it is clearer

Hi, I had a look into the errors for you. You are right, it didn’t render in correctly so thank you for attaching the screenshot.


Your error is actually a Typo in the inputs in the setup function. Whilst your outputs are “rededge1”, “rededge2” & “rededge3”, you defined them as “redegde1”, “redegde2” & “redegde3” in your inputs which is why the script doesn’t run.


Once you fix that, it should run smoothly 🙂


Hi, thank you, that was the problem… I guess I looked at this script so long that my eyes got used to this typo.


I hope it will be possible in the future to attach txt files or jupyter notebooks to questions here, it will be very helpful.


Reply