Error 400 on JavaScript, with script made on request builder

Hello everyone, I’m making a server in NestJS to get NDVI images of fields and the scripting of sentinel i used based on the request build, here is mine ndvi.service.ts:

import { SentinelAuthService } from 'src/sentinel-auth/sentinel-auth.service';
import { Injectable } from '@nestjs/common';
import {
  S2L2ALayer,
  setAuthToken,
  ApiType,
  MimeTypes,
  CRS_EPSG4326,
  BBox,
} from '@sentinel-hub/sentinelhub-js';

@Injectable()
export class NdviService {
  constructor(private readonly sentinelAuthService: SentinelAuthService) {}

  async getNdviData(
    fieldLngLat: any,
    fieldBbox: any,
    fieldUuid: any,
  ): Promise<any> {
    const authenticated = await this.sentinelAuthService.authenticate();

    if (authenticated) {
      const authToken = this.sentinelAuthService.getAuthToken();
      // console.log('authToken ' + authToken);
      setAuthToken(authToken);

      const evalscript = `//VERSION=3

function setup() {
  return {
    input: ["NIR_Narrow", "Red", "dataMask"],
    output: { bands: 4 }
  };
}

function evaluatePixel(sample) {
  var ndvi = (sample.NIR_Narrow - sample.Red) / (sample.NIR_Narrow + sample.Red)

  if (ndvi < -1.1) return [0, 0, 0, sample.dataMask];
  else if (ndvi < -0.2) return [0.75, 0.75, 0.75, sample.dataMask];
  else if (ndvi < -0.1) return [0.86, 0.86, 0.86, sample.dataMask];
  else if (ndvi < 0) return [1, 1, 0.88, sample.dataMask];
  else if (ndvi < 0.025) return [1, 0.98, 0.8, sample.dataMask];
  else if (ndvi < 0.05) return [0.93, 0.91, 0.71, sample.dataMask];
  else if (ndvi < 0.075) return [0.87, 0.85, 0.61, sample.dataMask];
  else if (ndvi < 0.1) return [0.8, 0.78, 0.51, sample.dataMask];
  else if (ndvi < 0.125) return [0.74, 0.72, 0.42, sample.dataMask];
  else if (ndvi < 0.15) return [0.69, 0.76, 0.38, sample.dataMask];
  else if (ndvi < 0.175) return [0.64, 0.8, 0.35, sample.dataMask];
  else if (ndvi < 0.2) return [0.57, 0.75, 0.32, sample.dataMask];
  else if (ndvi < 0.25) return [0.5, 0.7, 0.28, sample.dataMask];
  else if (ndvi < 0.3) return [0.44, 0.64, 0.25, sample.dataMask];
  else if (ndvi < 0.35) return [0.38, 0.59, 0.21, sample.dataMask];
  else if (ndvi < 0.4) return [0.31, 0.54, 0.18, sample.dataMask];
  else if (ndvi < 0.45) return [0.25, 0.49, 0.14, sample.dataMask];
  else if (ndvi < 0.5) return [0.19, 0.43, 0.11, sample.dataMask];
  else if (ndvi < 0.55) return [0.13, 0.38, 0.07, sample.dataMask];
  else if (ndvi < 0.6) return [0.06, 0.33, 0.04, sample.dataMask];
  else return [0, 0.27, 0, sample.dataMask];
}`;

      const layer = new S2L2ALayer({
        evalscript: evalscript,
        maxCloudCoverage: '70',
        harmonizeValues: 'true',
      });

      const getMapParams = {
        bbox: new BBox(
          CRS_EPSG4326,
          -47.794306627870014,
          -21.39866663394473,
          -47.79084940434106,
          -21.39531295351236,
        ),
        fromTime: new Date('2023-08-27T00:00:00Z'),
        toTime: new Date('2023-09-27T23:59:59Z'),
        width: 512,
        height: 533.426,
        format: MimeTypes.PNG,
        geometry: {
          coordinates: [
            [
              [-47.79250565561276, -21.39531295351236],
              [-47.79084940434106, -21.39657059268862],
              [-47.792441335175056, -21.39866663394473],
              [-47.794306627870014, -21.397154492914282],
              [-47.79250565561276, -21.39531295351236],
              [-47.79250565561276, -21.39531295351236],
            ],
          ],
          type: 'Polygon',
        },
      };

      const imageBlob = await layer.getMap(getMapParams, ApiType.PROCESSING);

      return imageBlob;
    } else {
      console.log('auth failed');
    }
  }
}

but it returns 3 erros for me:

1-) on the layer constant the “maxCloudCoverage” returns an error related to type this one I did manage to find a solution when taking a look into sentinel files

2-) on the layer.getMap(getMapParams, …), it returns an error related to the type of the “geometry” key, specifically like this:

The argument of type '{ bbox: BBox; fromTime: Date; toTime: Date; width: number; height: number; format: MimeType | "JPEG_OR_PNG"; geometry: { coordinates: number[][][]; type: string; }; }' is not assignable to the parameter of type 'GetMapParams'.
Property types 'geometry' are incompatible.
Type '{ coordinates: number[][][]; type: string; }' cannot be assigned to type 'Polygon | MultiPolygon'.
Type '{ coordinates: number[][][]; type: string; }' cannot be assigned to type 'MultiPolygon'.
Property types 'type' are incompatible.
Type 'string' cannot be assigned to type '"MultiPolygon"'.

3-) if i just take all the “geometry” key away an in the layer constant i just let the “evalscript” key, it still returns me an error 400 which I don’t undestand why since the script is basically the same as the request builder.

anyway if someone can help me with this I would be very grateful, and if you feel like i need to give some more information or if you have any tip on anything, please feel free to say it. Thanks!

Hi @fellipe ,

It seems there’s an formatting issue of your geometry. Could you please try to format it as below:

{
  "coordinates": [
    [
      [
        -47.79250565561276,
        -21.39531295351236
      ],
      [
        -47.79084940434106,
        -21.39657059268862
      ],
      [
        -47.792441335175056,
        -21.39866663394473
      ],
      [
        -47.794306627870014,
        -21.397154492914282
      ],
      [
        -47.79250565561276,
        -21.39531295351236
      ],
      [
        -47.79250565561276,
        -21.39531295351236
      ]
    ]
  ],
  "type": "Polygon"
}

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.