Statistical API Histogram Error on NDVI data

I am using the Statistical API and runs fine, until I try to generate a histogram on NDVI data.
I get the error: “type”: “EXECUTION_ERROR”,
“message”: “class java.lang.Double cannot be cast to class java.lang.Integer (java.lang.Double and java.lang.Integer are in module java.base of loader ‘bootstrap’)”
Any idea how to resolve this?

Hi John,

Thanks for the query, can you provide a code snippet of your Statistical API request please. Then I can help you debug where we can fix your request :slight_smile:

“input”: {
“bounds”: {
“bbox”: [
100.700701,
14.759279,
100.705206,
14.762641
]
},
“data”: [
{
“dataFilter”: {},
“type”: “sentinel-2-l2a”
}
]
},
“aggregation”: {
“timeRange”: {
“from”: “2022-04-04T00:00:00Z”,
“to”: “2022-04-07T23:59:59Z”
},
“aggregationInterval”: {
“of”: “P1D”
},
“width”: 512,
“height”: 584.288,
“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: “Stats”,\n bands: [“NDVI”]\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 NDVI = 100*(samples.B08 - samples.B04) / (samples.B08+samples.B04);\n let y = 0.106;\n return {\n Stats: [NDVI],\n dataMask: [samples.dataMask],\n scl: [samples.SCL]\n };\n}\n”
},
“calculations”: {
“default”: {
“histograms”: {
“default”: {
“nBins”: 10
}
}
}
}

Hi John,

I have found a solution for you, there are two cells below that you can execute in a Jupyter Notebook. The first one imports the correct libraries and creates an Oauth2session for you. The second defines the evalscript and the statistical API request.

from sentinelhub import SHConfig, SentinelHubStatistical, BBox, Geometry, DataCollection, CRS

from creds import *

from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session

# Create a session
client = BackendApplicationClient(client_id=<your client id>)
oauth = OAuth2Session(client=client)


# Get token for the session
token = oauth.fetch_token(token_url='https://services.sentinel-hub.com/oauth/token',
                          client_secret=<your client secret>)

# All requests using this session will have an access token automatically added
resp = oauth.get("https://services.sentinel-hub.com/oauth/tokeninfo")
print(resp.content)

You can then run the below cell that defines your evalscript and Statistical API request. The json return can then be used to generate histogram plots.

evalscript = """
//VERSION=3
  function setup() {
    return {
      input: [{
        bands: [
          "B04",
          "B08",
          "CLM",
          "dataMask"
        ]
      }],
      output: [
        {
          id: "NDVI",
          bands: 1
        },
        {
          id: "dataMask",
          bands: 1
        }
      ],
      mosaicking: "SIMPLE"
    };
  }

  function evaluatePixel(samples) {
      let ndvi = (samples.B08 - samples.B04) / (samples.B08 + samples.B04);

      let ndviMask = 1;
      let noCloudMask = 1;
      if (!isFinite(ndvi)) {
        ndviMask = 0;
      }
      if (samples.CLM === 1) {
        noCloudMask = 0;
      }

      return {
          NDVI: [ndvi],
          dataMask: [
            samples.dataMask * ndviMask * noCloudMask
          ]
      };
  }
"""

stats_request = {
  "input": {
    "bounds": {
      "bbox": [414315, 4958219, 414859, 4958819],
    "properties": {
        "crs": "http://www.opengis.net/def/crs/EPSG/0/32633"
        }
    },
    "data": [
      {
        "type": "sentinel-2-l2a",
        "dataFilter": {
            "mosaickingOrder": "leastRecent"
        },
      }
    ]
  },
  "aggregation": {
    "timeRange": {
            "from": "2021-07-01T00:00:00Z",
            "to": "2021-08-01T00:00:00Z"
      },
    "aggregationInterval": {
        "of": "P1D"
    },
    "evalscript": evalscript,
    "resx": 10,
    "resy": 10
  },
  "calculations": {
    "NDVI": {
      "histograms": {
        "default": {
          "nBins": 5,
          "lowEdge": 0.0,
          "highEdge": 0.3
        }
      },
      "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

If you have any more questions, please don’t hesitate to ask, we will be happy to help! :slight_smile: