cven
cven

Reputation: 11

Attempting to transform from JavaScript to RGEE

I am trying to rewrite this code from EarthEngine (JavaScript) to the RGEE API :

function collection_index(image) {
  
  function setNdviMinMax(img) {
    var minMax = img
    .select('NDVI')
    .reduceRegion({
      reducer: ee.Reducer.minMax(),
      scale: 20,
      maxPixels: 1e13
    });
  return img.set({
    'NDVI_min': minMax.get('NDVI_min'),
    'NDVI_max': minMax.get('NDVI_max'),
  });
  }  
  
  var ndvi_param = image.normalizedDifference(['B8', 'B4']);
  ndvi_param =  ndvi_param.rename('NDVI');
  var SWIR_param = image.select('B12').rename('SWIR').divide(10000);
  var STR_param = SWIR_param.expression(
  '((1-SWIR)**2)/(2*SWIR)',{
    'SWIR' : SWIR.select('SWIR'),
  }).rename('STR')
  
  ndvi_param = setNdviMinMax(ndvi_param)
  
  
  ndvi_param = ndvi_param.updateMask(agrico)
 
  return ndvi_param.addBands(STR_param) 
}

var coll = S2.map(collection_index)

print(coll)

and I proceeded in this way:

collection_index <- function(image){
  setNdviMinMax <- function(img){
    minMax <- img$select('NDVI')$
      reduceRegion(list(reducer= ee$Reducer$minMax(),
                        scale= 20,
                        maxPixels = 1e+09)
      )
    return(img$set(
      list(
        'NDVI_min'= minMax$get('NDVI_min'),
        'NDVI_max'= minMax$get('NDVI_max')
      )
    )
      
    )
  }
  ndvi_param <- image$normalizedDifference(list("B8", "B3"))
  ndvi_param <-  ndvi_param$rename('NDVI')
  SWIR_param <- image$select('B12')$rename('SWIR')$divide(10000)
  STR_param <- SWIR_param$expression('((1-SWIR)**2)/(2*SWIR)', list('SWIR' = SWIR$select('SWIR')))$rename('STR')
  ndvi_param <- setNdviMinMax(ndvi_param)
  ndvi_param <- ndvi_param$updateMask(agrico)
return(ndvi_param$addBands(STR_param) )
}

coll <- S2$map(collection_index)
ee_print(coll)

However, I have this error :

Error in py_call_impl(callable, dots$args, dots$keywords) : RuntimeError: Evaluation error: ee.ee_exception.EEException: Invalid argument for ee.Reducer(): ({'reducer': <ee.Reducer object at 0x7fd1e3548220>, 'scale': 20.0, 'maxPixels': 1000000000.0},). Must be a ComputedObject.

Could someone help me fix this error?
Thank you very much

The GEE source code is from João Otavio Firigato

Upvotes: 0

Views: 110

Answers (1)

Kevin Reid
Kevin Reid

Reputation: 43782

I don't know R so this is solely based on seeing others' examples, but I am confident the error is here:

      reduceRegion(list(reducer= ee$Reducer$minMax(),
                        scale= 20,
                        maxPixels = 1e+09)
      )

reduceRegion does not take a dictionary but named arguments. So this should be

      reduceRegion(
        reducer = ee$Reducer$minMax(),
        scale = 20,
        maxPixels = 1e+09
      )

Upvotes: 0

Related Questions