Wednesday, 1 May 2019

Google Earth Engine: eq filter not taking values in list


I want to filter a raster in GEE with multiple values. I input a list of values and map over the function to apply the filter. Here is the GEE code to loop over a list of numbers and filter the raster (DEM) for values equal to the numbers in the list.


var aec = function(n){
var ii = ee.Image(n) // casting the number to image before filtering but didn't work

var DEM141 = DEM.eq(ii)

var DEM141Count = DEM141.reduceRegion({
geometry: ROI,
scale: 30,
reducer: ee.Reducer.sum()
})
return DEM141Count
}


var elevs = ee.List.sequence(100,150,1);
var areas = elevs.map(aec)
print(areas)

I keep getting this error: Image.lte, argument 'image2': Invalid type. Expected: Image. Actual: Float.


The GEE code link is: https://code.earthengine.google.com/6e55360bc82695f930d64ee70a415dc0



Answer



The trick is to define n as a imageConstant with a projection:


Map.addLayer(DEM, {min: 100, max: 300}, 'DEM')


var ElevHist = ui.Chart.image.histogram({
region: ROI,
image: DEM,
scale: 30,

})

print(ElevHist)

print(DEM.projection())


// var lte = ee.Image(DEM).lte(ii)

var aec = function(n){
var ii = ee.Image.constant(n)//.reproject(DEM.projection())
var DEM141 = ee.Image(DEM).lte(ii)

var DEM141Count = ee.Number(DEM141.reduceRegion({
geometry: ROI,
scale: 30,

reducer: ee.Reducer.sum()
}).values().get(0))
return DEM141Count
}

// print(DEM141Count.get('elevation'))

var elevs = ee.List.sequence(140,150,1);
var areas = elevs.map(aec)
print(areas)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...