2

I would like to generate a histogram of the ratio between pixels within a polygon called "MODISofstation". Thereby all the pixels within my polygon should be compared to the pixel where my Point "pt" is located in and I can see the variability within the polygon. I am only looking at B8 for this example. The first part of my code works fine and I receive all the values from each month but from the part "//RATIO" on, I fail to make it work to calculate the monthly mean, the ratio and bring it into a histogram.

Below you can find the code snippet


var start = '2020-06-01';
var finish = '2020-06-30';
var pt = ee.Geometry.Point([-49.31582, 69.56833]);
// Map the function over one year of Landsat 8 TOA data and take the median.
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate (start,finish)
.filterBounds(pt)
.filter("WRS_ROW <= 122")
.map(maskL8);


//Map. addLayer(B8)
//Map. addLayer(pt)
Map.centerObject(pt, 16);

print(l8);



// B8 VALUE OF PT 

var myB8 = l8.select("B8")
print("myB8",myB8); 

var getB8 = function(image) {

  // Reducing region and getting value
  var value_B8 = ee.Image(image)
    .reduceRegion(ee.Reducer.first(), pt)
    .get('B8');

  return value_B8;
};

var count = myB8.size();

var B8_list = l8.toList(count).map(getB8);


print("B8 list", B8_list);


var img = l8.first();

Map.addLayer(img);
Map.addLayer(pt);

var allDates = l8.aggregate_array('system:time_start');

var allDatesSimple = allDates.map(function procDates (ele) {
  
  return ee.Date(ele).format().slice(0,10);
  
});

var paired = allDatesSimple.zip(B8_list);

print (paired);



// RATIO

//Monthly mean of pt pixel
var MonthlyMeanPt = myB8.mean;
print (MonthlyMean)

var ratio= ee.Image.divide(Monthlymean)
Map.addLayer (ratio);

//Histogram within my polygon MODISofstation


var histogram = ui.Chart.image.histogram({
  image: ratio,
  region: MODISofstation,
  scale: 15,
});
histogram.setOptions({
  title: 'Histogram of Ratio'
});

print(histogram);

1 Answer 1

1

You have a misunderstanding about your expected result, several mistakes in your code and some not defined variables. If you want to generate a histogram of the ratio between pixels within a polygon called "MODISofstation", it is very important to have it. So, I assumed in my code an arbitrary polygon geometry named MODISofstation for this goal. This geometry is necessary for clipping MonthlyMeanPt image (you have a mistake in 'mean' function in this case; it is mean()). This image contains average values for each pixel coincident with your Image Collection.

On the other hand, if you want to compare all pixels within your polygon related to the pixel where your Point "pt" is located, you need the mean of B8_list (a number). Following code line:

var ratio= ee.Image.divide(Monthlymean)

doesn't have sense because there is not any image for dividing. You need something as:

var ratio = MonthlyMeanPt.clip(MODISofstation)
  .divide(ee.Number(mean_B8_list));

After these considerations, rest of your code works as expected.

Complete code looks as follows:

var MODISofstation = ee.Geometry.Polygon(
        [[[-46.938207078168, 69.71939231975276],
          [-46.960179734418, 69.65360126960447],
          [-46.7789053203555, 69.67364624034165],
          [-46.74869291801175, 69.76598971182032],
          [-47.02335112113675, 69.81058877699088],
          [-47.17715971488675, 69.79921074457349],
          [-47.17715971488675, 69.74413025357914]]]);

var start = '2020-06-01';
var finish = '2020-06-30';
var pt = ee.Geometry.Point([-49.31582, 69.56833]);
// Map the function over one year of Landsat 8 TOA data and take the median.
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate (start,finish)
.filterBounds(pt)
.filter("WRS_ROW <= 122");
//.map(maskL8);


//Map. addLayer(B8)
//Map. addLayer(pt)
Map.centerObject(pt, 16);

print(l8);

// B8 VALUE OF PT 

var myB8 = l8.select("B8");
print("myB8",myB8); 

var getB8 = function(image) {

  // Reducing region and getting value
  var value_B8 = ee.Image(image)
    .reduceRegion(ee.Reducer.first(), pt)
    .get('B8');

  return value_B8;
};

var count = myB8.size();

var B8_list = l8.toList(count).map(getB8);

print("B8 list", B8_list);

var mean_B8_list = B8_list.reduce(ee.Reducer.mean());

print("mean_B8_list", mean_B8_list);

var img = l8.first();

var imageVisParam1 = {"opacity":1,
                     "bands":["B1","B2","B3"],
                     "min":0.44147244095802307,
                     "max":1.1667611598968506,"gamma":1};

Map.addLayer(img, imageVisParam1, 'image');
Map.addLayer(pt, {'color':'red'}, 'point');

var allDates = l8.aggregate_array('system:time_start');

var allDatesSimple = allDates.map(function procDates (ele) {
  
  return ee.Date(ele).format().slice(0,10);
  
});

var paired = allDatesSimple.zip(B8_list);

//print (paired);

// RATIO

//Monthly mean of pt pixel
var MonthlyMeanPt = myB8.mean();

var ratio = MonthlyMeanPt.clip(MODISofstation)
  .divide(ee.Number(mean_B8_list));

//print (MonthlyMeanPt);

var imageVisParam2 = {"opacity":1,
                      "bands":["B8"],
                      "min":1.0024652300739718,
                      "max":1.1152103662434536,
                      "gamma":1};

Map.addLayer (ratio, imageVisParam2, 'ratio');

//Map.addLayer(MODISofstation, {'color':'blue'}, 'MODISofstation');
Map.centerObject(ratio, 8);

//Histogram within my polygon MODISofstation

var histogram = ui.Chart.image.histogram({
  image: ratio,
  region: MODISofstation,
  scale: 15,
});

histogram.setOptions({
  title: 'Histogram of Ratio'
});

print(histogram);

After running it in Console of GEE, it produces expected histogram; as it can be observed in following image:

enter image description here

2
  • That helps a lot, thank you! What I was wondering, how are the mind and max of imageVisParam1 and imageVisParam2 defined? Since some ratio values are also below 1
    – xdsccc
    Commented Mar 16, 2021 at 11:52
  • You can select them by using "image visualization parameters" selector for each image. Select a custom 100 % option (for getting the best range) and, afterward, import it (blue button) to GEE code editor for using in your code. It is very easy.
    – xunilk
    Commented Mar 16, 2021 at 13:01

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.