Skip to main content

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

Hi, in case you didn’t know you can build your request using Request Builder. By default, this will be a CURL request, so you should be able to get what you want from here 🙂


Hi,
Thanks for your response. I have tried Request Bulder, but I got some problems.
In my experiments, curl returned all but not histogram data, as I said earlier the Python code worked like a charm.

 


Hi, can you provide the curl request that is not working correctly, and I can help look into it.


Here you are. This is different one as Python example (sentinelhub example for sentinel-2-l2a.


curl -X POST https://services.sentinel-hub.com/api/v1/statistics \
-H 'Authorization: Bearer xxxxx' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"input": {
"bounds": {
"bbox": [
12.44693,
41.870072,
12.541001,
41.917096
]
},
"data": [
{
"dataFilter": {"maxCloudCoverage": "100"},
"type": "sentinel-2-l2a"
}
]
},
"aggregation": {
"timeRange": {
"from": "2020-07-04T00:00:00Z",
"to": "2020-07-05T00:00:00Z" },
"aggregationInterval": {
"of": "P1D"
},
"width": 512,
"height": 343.697,
"evalscript": "//VERSION=3\nfunction setup() {\n return {\n input: [{\n bands: [\n \"B04\",\n \"B08\",\n \"SCL\",\n \"dataMask\"\n ]\n }],\n output: [\n {\n id: \"data\",\n bands: 3\n },\n {\n id: \"scl\",\n sampleType: \"INT8\",\n bands: 1\n },\n {\n id: \"dataMask\",\n bands: 1\n }]\n };\n}\n\nfunction evaluatePixel(samples) {\n let index = (samples.B08 - samples.B04) / (samples.B08+samples.B04);\n return {\n data: [index, samples.B08, samples.B04],\n dataMask: [samples.dataMask],\n scl: [samples.SCL]\n };\n}\n"
},
"calculations": {
"B0": {
"histograms": {
"default": {
"nBins": 5,
"lowEdge": "0.2",
"highEdge": "0.8"
}
}
}
}
}'

Thanks for sharing, I tested the request and the it seems to work correctly for me. What is the error you receive back?


Hi,

I just can’t see any histogram which defined in calculations section, do you?


Python code returns:

histogram’: {‘bins’: i{‘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}}}}}}]


Hi, the Statistical API will only return a JSON in the response. However, the information that is need to generate a histogram is contained within the JSON response. You can then use any common data science libraries (for example; pandas, matplotlib) to handle and then visualise the information contained in the JSON. There are some good examples here for example.


Hi again,

Thank you for your response.

The Python code returns the necessary data, so no extra libraries are needed.

The problem is that the use of Javascript (or curl) for the histogram is missing.

One solution is to use Python as a subprocess inside node/javascript.


I’m not an expert with Javascript but a quick Google came up with this: Histograms in JavaScript


Thaks, but it’s not about showing the data, it’s about getting it.


{‘lowEdge’: 0.16, ‘highEdge’: 0.32, ‘count’: 887}


The return you shared has all the information you need to build your histogram, the lowEdge and highEdge referring to the slice of data values and the count of pixels that fall into this slice.


What else are you missing?


Reply