Hi,
This Python code works fine, but how to get it to work with Javascript or curl.
I tried but didn’t succeed, getting data but not histogram at all.
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
# Set credentials
client_id = 'xxxxx
client_secret = 'xxxxxx'
# Set up credentials for use with batch
client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
# Fetch a token
token = oauth.fetch_token(token_url='https://services.sentinel-hub.com/oauth/token',
client_id=client_id, client_secret=client_secret)
evalscript = """
//VERSION=3
function setup() {
return {
input: [{
bands: [
"B04",
"B08",
"dataMask"
]
}],
output: [
{
id: "data",
bands: 1
},
{
id: "dataMask",
bands: 1
}]
};
}
function evaluatePixel(samples) {
let index = (samples.B08 - samples.B04) / (samples.B08+samples.B04);
return {
data: [index, samples.B08, samples.B04],
dataMask: [samples.dataMask]
};
}
"""
stats_request = {
"input": {
"bounds": {
"bbox": [414315, 4958219, 414859, 4958819],
},
"data": [
{
"type": "sentinel-2-l1c",
"dataFilter": {
"maxCloudCoverage": "40"
},
}
]
},
"aggregation": {
"timeRange": {
"from": "2020-07-04T00:00:00Z",
"to": "2020-07-05T00:00:00Z"
},
"aggregationInterval": {
"of": "P1D"
},
"evalscript": evalscript,
"resx": 10,
"resy": 10
},
"calculations": {
"default": {
"histograms": {
"default": {
"nBins": 5,
"lowEdge": 0.0,
"highEdge": 0.8
}
},
"statistics": {
"default": {
"percentiles": {
"k": [ 33, 50, 75, 90 ]
}
}
}
}
}
}
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)
sh_statistics = response.json()
sh_statistics
Response:
{'data': [{'interval': {'from': '2020-07-04T00:00:00Z',
'to': '2020-07-05T00:00:00Z'},
'outputs': {'data': {'bands': {'B0': {'stats': {'min': -0.056964658200740814,
'max': 0.44752582907676697,
'mean': 0.09596906432624684,
'stDev': 0.1365882610351553,
'sampleCount': 3240,
'noDataCount': 0,
'percentiles': {'33.0': -0.01080550067126751,
'75.0': 0.24427953362464905,
'90.0': 0.30311986804008484,
'50.0': 0.007215007208287716}},
'histogram': {'bins': [{'lowEdge': 0.0, 'highEdge': 0.16, 'count': 708},
{'lowEdge': 0.16, 'highEdge': 0.32, 'count': 887},
{'lowEdge': 0.32, 'highEdge': 0.48, 'count': 200},
{'lowEdge': 0.48, 'highEdge': 0.64, 'count': 0},
{'lowEdge': 0.64, 'highEdge': 0.8, 'count': 0}],
'overflowCount': 0,
'underflowCount': 1445}}}}}}],
'status': 'OK'}
Br. Jacke