0

Here is my server side code:

app.post("/process-coordinates", (req, res) => {
  //const coordinates = req.body.coordinates;
  // Here you can perform any Earth Engine operation using the received coordinates
  // Example: Export the region to Earth Engine
  var dataset = ee.ImageCollection("CSP/HM/GlobalHumanModification").first();

  // Convert the floating-point band to integer values.
  var convertedDataset = dataset.select("gHM").multiply(100).toInt();

  // Define the visualization parameters.
  var visualization = {
    bands: ["gHM"],
    min: 0,
    max: 100,
    palette: ["0c0c0c", "071aff", "ff0000", "ffbd03", "fbff05", "fffdfd"],
  };

  // Visualize the dataset.
  var visualizedImage = convertedDataset.visualize(visualization);

  // Define an export region covering Metro Manila.
  var exportRegion = ee.Geometry.Polygon([
    [
      [120.87, 14.39],
      [120.85, 14.3],
      [121.05, 14.3],
      [121.03, 14.434],
    ],
  ]);

  // Compute the mean pixel value within the Metro Manila region.
// Compute the mean pixel value within the Metro Manila region.
var stats = convertedDataset.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: exportRegion,
  scale: 1000, // Adjust the scale according to your requirements
});

// Check if the key 'gHM' exists in the dictionary
if (stats.contains('gHM')) {
  // Get the mean GHM value from the dictionary.
  var meanGHMValue = stats.get('gHM');
  // Print the computed mean value.
  console.log("Mean GHM value within Metro Manila:", meanGHMValue);
} else {
  console.log("Mean GHM value within Metro Manila not found");
}

  
  // Send a response
  res.status(200).send("Coordinates received successfully");
});

I'm having trouble with generating the mean value from the Google Earth Engine JavaScript library when I'm calling the API from my server. Instead of the mean value, I am getting:

Mean GHM value within Metro Manila: {
  func: {
    signature_: {
      args: [Array],
      description: 'Extracts a named value from a dictionary.  If the dictionary does not contain the given key, then defaultValue is returned, unless it is null.',
      returns: 'Object',
      name: 'Dictionary.get'
    }
  },
  args: {
    dictionary: {
      func: [Object],
      args: [Object],
      varName: null,
      sourceFrame: null,
      dict_: null
    },
    key: {
      func: null,
      args: null,
      varName: null,
      sourceFrame: null,
      string_: 'gHM'
    }
  },
  varName: null,
  sourceFrame: null
}

in the earth engine code editor I am getting the mean numerical value properly but in my API calls in my server.js its resulting to that long line.

1 Answer 1

0

Turns out I need to turn it to a client side data first by using ee.number here is the snippet:

if (stats.contains('gHM')) {
  // Get the mean GHM value from the dictionary.
  var meanGHMValue = stats.get('gHM');
  var meanValue = ee.Number(meanGHMValue).getInfo();

it resulted to a number after this. I got confused because I did not have to do this in the ee code editor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.