2

I am trying to create a function in Google Earth Engine which would iterate through an ImageCollection consisting of DEMs and return a message saying when a big elevation difference occurs. I believe I am not far from the answer, but I can't manage to make it work.

To what I understand, when you subtract one image from another it does so on pixel-by-pixel basis and returns an image. If you want elevation changes at a specific point, a geometic point can be defined. But what if you want the elevation difference to be calculated on the whole DEM and if at any location a big elevation change occurs, the function detects it? Do you need to explicitly define the region of interests, even though the DEMs themselves act as roi?

Script:

function anomaly(image) {
  // Converting the ImageCollection ''newdems'' to list
  var Imagelist = newdems.toList(newdems.size());
  // Defining the previous image
  var previous = ee.Image(Imagelist.get(-1));
  // Equation to calculate the elevation difference 
  var difference = image.subtract(previous);
  // Conditional statement to return the name of the image after which the big elevation change occurs
  return ee.Algorithms.If(difference.gt(150),print('Anomaly occurs after: ', previous),ee.Image(0));
};
2
  • Welcome to stackexhange. Please share a code snippet of the complete working example to help us help you. It would be much easier to suggest changes to code if you shared newdems image collection and the region of interest geometry rather than just the function.
    – kkrao
    Jun 20, 2020 at 18:40
  • 1
    Of course, here is the link to the script: code.earthengine.google.com/31637958f6b33143a6880cae0efe2697 As you can see in the script, I had to remove the useless dems manually, i tried to use threshold values to filter out them automatically, but the original dems in the imagecollection already masked out false values, that is why I struggle to automate this part of the script. But for this stage of my work, manually removing them will do.
    – AdoMath
    Jun 20, 2020 at 19:21

1 Answer 1

1

Answering your specific questions: Do you need to explicitly define the region of interests, even though the DEMs themselves act as roi? Yes. I think you should reduce the difference image with its boundingbox, searching for a maximum value of change. for example:

var difference = image.subtract(previous);
var roi = difference.geometry();
var numDiff =  difference.reduceRegion({reducer: ee.Reducer.max(),
                                       geometry: roi,
                                      scale: 250,
                                      bestEffort: true});

But i think there are other problems in your function. For example your previous image will always be the same... If your idea was to substract image 2 with image 1, image 3 with image 2 etc, the code below may work. Its an example with ndvi scenes. (i wish i had a dem time series!)

var col = ee.ImageCollection("MODIS/MOD13A1").filter(ee.Filter.date('2014-04-01', '2014-08-01'))
                                             .select('NDVI')
                                             .map(function(img){
                                               return img.clip(geometry)})
var list = col.toList(col.size());


var indices = ee.List.sequence(0,col.size().subtract(2), 1)

var colDifference = indices.map(function(num){
  num = ee.Number(num)
  var current = ee.Image(list.get(num));
  var add = num.add(ee.Number(1));
  var post = ee.Image(list.get(add))
  var imgDiff = post.subtract(current);
  var numDiff =  imgDiff.reduceRegion({reducer: ee.Reducer.max(),
                                       geometry: geometry,
                                      scale: 250,
                                      bestEffort: true});
  
        return ee.Feature(null, {'diffMax': numDiff})
        
}).map(function(feat){
  return ee.Dictionary(ee.Feature(feat).get('diffMax')).get('NDVI')
});


var clientsideList = colDifference.getInfo()


///////////////////CLIENT SIDE
for(var i = 0; i < 8; i++){
  if (clientsideList[i] > 1100){
          print('anomaly')}
        else {
      print('tutto a posto')
}}

https://code.earthengine.google.com/?scriptPath=users%2Fjjfoguet%2Fdefault%3Asubtracting_consecutive

1
  • 1
    Great answering! Thank you for your insight, many things were clarified and after adapting your script to mine, it worked! If you do need a dem time series script in your repertoire, I can share mine :)
    – AdoMath
    Jun 21, 2020 at 11:41

Your Answer

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

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