1

In my code, I have displayed the statistics using hardcode i.e information is fed manually in a list and displayed. I would like to display all statistics correctly using for loop by iself automatically.


//Create a circle by drawing a 2000 m buffer around a point and saving it to variable roi.
var roi = ee.Geometry.Point([-76.147, 43.046]).buffer(20000);

//Create a variable that points to a particular Landsat image in the Earth Engine
var myimage = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_015030_20100531');

// Compute the mean .
var meanDict = myimage.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: polygon,
  scale: 90
});

// Compute the median.
var medianDict = myimage.reduceRegion({
  reducer: ee.Reducer.median(),
  geometry: polygon,
  scale: 90
});

// Compute the mode.
var modeDict = myimage.reduceRegion({
  reducer: ee.Reducer.mode(),
  geometry: polygon,
  scale: 90
});

// Reduce the image to get a one-band maximum value image.
var maxValue = myimage.reduce(ee.Reducer.max());

//min value
var minValue = myimage.reduce(ee.Reducer.min());

// Extract the bands from collection and make a list
var bands= ee.List([
    [ 'B1','0.45','0.092377477','0.086616925','0.086616925'],
    ['B2','0.51','0.085137095','0.076246974','0.076246974'],
    ['B3','0.59','0.062736553','0.053236987','0.044920224'],
    ['B4','0.67','0.324248892','0.334150672','0.357299032'],
    ['B5','0.88','0.161820343','0.157588621','0.153408903'],
    ['B6','1.65','0.3812067','0.295290527','293.7689209'],
    ['B7','2.29','0.076728936','0.064302692','0.052958585'],
    ['B8','0.5','672.0124447','671','672']
]);

var featList=bands.map(function(list){
  return ee.Feature(null).set({bands:ee.List(list).get(0),wavelength:ee.List(list).get(1),
      mean:ee.List(list).get(2),median:ee.List(list).get(3),mode:ee.List(list).get(4)})
})

var features = ee.FeatureCollection(featList)

//Export table to Drive;
Export.table.toDrive({
  collection: features, //Select FeatureCollection
  folder: 'GEE', //Set folder name
  fileFormat: 'CSV' //Set file format
});
6
  • 1
    Hi poorva, Welcome on SIG. If you wxant to get an answer, I suggest you refactor your question : - reduce the amont of code your sending to the minimum (I've already cleaned up some stuff) - better describe your problem in the title and in the question Mar 31, 2021 at 10:38
  • okay Pierrick ...thankyou! Mar 31, 2021 at 10:53
  • I basically want to know how to display the wavelength,mean,median,mode of all bands of a single image.The code which i have written doesnt use loops.I want to do it with the loops.Here is the link to my code [link] code.earthengine.google.com/6585a64332f06649d7c32c43c7e1b4d2 @PierrickRambaud PLEASE HELP!! Mar 31, 2021 at 10:57
  • Don't repeat yourself in the comments it's reducing your impact. + you don't need to "ask" for help. If you there you need a hand we know that. and finally don't use capital letters, it's aggressive. Mar 31, 2021 at 12:00
  • Okay, I got it. Mar 31, 2021 at 15:23

1 Answer 1

0

I modified your code adding some lines for retrieving directly bands names, wavelength, mean, median and mode of all bands of a single image (and commenting your older bands variable). Unique exception was the wavelength because this information is not included in 'Metadata properties'. So, I created this list manually. Complete code looks as follows:

//center the map and display the image
Map.setCenter(26.7606, 83.3732,10);
Map.addLayer(myimage);

//Display the image using a specific band combination and call image ColorIR composite.
Map.addLayer(myimage, {bands:['B4' , 'B3' , 'B2']},'colorIR composite',false);

//Define visualisation parameters to display the image in the map window.
var vizParams={
    bands:['B4' , 'B3' , 'B2'],
    min: 0,
    max: 0.5,
    gamma: [0.95,1.1,1]
};

Map.addLayer(myimage, vizParams, 'Color IR composite' ,false);

//Create a circle by drawing a 2000 m buffer around a point and saving it to variable roi.
var roi = ee.Geometry.Point([-76.147, 43.046]).buffer(20000);
//Display the buffer
Map.addLayer(roi);

//Create a variable that points to a particular Landsat image in the Earth Engine
var myimage = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_015030_20100531');

print("my image", myimage);

var bandNames = myimage.bandNames();
print('Band names:', bandNames);  // ee.List of band names

var properties = myimage.propertyNames();
print('Metadata properties:', properties);  // ee.List of metadata properties

//there is not information about wavelengt from bands so, it must be added manually

var waveLengthBands = ee.List(['0.45-0.52', '0.52-0.60 ', '0.63-0.69', '0.76-0.90', 
  '1.55-1.75', '10.40-12.50', '2.08-2.35', 'none']);
print('WaveLength Bands', waveLengthBands);

var myimage = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_015030_20100531');
// Compute the mean .
var meanDict = myimage.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: polygon,
  scale: 90
});

// Get the mean from the dictionary and print it.
print('Mean', meanDict);

// Compute the median.
var medianDict = myimage.reduceRegion({
  reducer: ee.Reducer.median(),
  geometry: polygon,
  scale: 90
});

// Get the mode from the dictionary and print it.
print('Median',medianDict);

// Compute the mode.
var modeDict = myimage.reduceRegion({
  reducer: ee.Reducer.mode(),
  geometry: polygon,
  scale: 90
});

// Get the median from the dictionary and print it.
print('Mode',modeDict);

var bands =  bandNames.map(function (ele) {
  
  var idx = bandNames.indexOf(ele);
  
  return [ele, waveLengthBands.get(idx), meanDict.get(ele), medianDict.get(ele), modeDict.get(ele)];
  
});

print("list of stats", bands);

// Reduce the image to get a one-band maximum value image.
var maxValue = myimage.reduce(ee.Reducer.max());

// Display the result.
Map.centerObject(myimage, 10);
Map.addLayer(maxValue, {max: 13000}, 'Maximum value image');

//min value
var minValue = myimage.reduce(ee.Reducer.min());

// Display the result.
Map.centerObject(myimage, 10);
Map.addLayer(minValue, {min: 0}, 'Minimum value image');

// Extract the bands from collection and make a list
/*
var bands= ee.List([
                    [ 'B1','0.45','0.092377477','0.086616925','0.086616925'],
                     ['B2','0.51','0.085137095','0.076246974','0.076246974'],
                     ['B3','0.59','0.062736553','0.053236987','0.044920224'],
                     ['B4','0.67','0.324248892','0.334150672','0.357299032'],
                     ['B5','0.88','0.161820343','0.157588621','0.153408903'],
                     ['B6','1.65','0.3812067','0.295290527','293.7689209'],
                     ['B7','2.29','0.076728936','0.064302692','0.052958585'],
                     ['B8','0.5','672.0124447','671','672']]);
*/

var featList = bands.map(function(list){
  return ee.Feature(null).set({bands:ee.List(list).get(0),wavelength:ee.List(list).get(1),
      mean:ee.List(list).get(2),median:ee.List(list).get(3),mode:ee.List(list).get(4)});
});

var features = ee.FeatureCollection(featList);
print(features);

//Export table to Drive;
Export.table.toDrive({
  collection: features, //Select FeatureCollection
  folder: 'GEE_folder', //Set folder name
  fileFormat: 'CSV' //Set file format
});

After running above code in GEE code editor and its associated task, I got cvs file as expected. It looks as follows after it being opened with a plain text editor.

system:index,bands,mean,median,mode,wavelength,.geo
0,B1,0.09237747700969104,0.08661692518953008,0.08661692518953008,0.45-0.52,
1,B2,0.08513709531653076,0.07624697435362061,0.07624697435362061,0.52-0.60 ,
2,B3,0.06273655323585847,0.05323698744177813,0.04492022364556938,0.63-0.69,
3,B4,0.3242488916113202,0.33415067195892323,0.35729903211388575,0.76-0.90,
4,B5,0.1618203434605938,0.1575886209460983,0.15340890275378363,1.55-1.75,
5,B6,296.3088653679029,295.5290527343749,293.76892089843756,10.40-12.50,
6,B7,0.07672893620940532,0.06430269171473459,0.05295858501710743,2.08-2.35,
7,BQA,672.0124446914976,672.0000000000078,672.0000000000078,none,
0

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.