1

I have created an array with the results of a Pearson correlation between image bands. I want to convert that array into a properly structured Table. Currently, I am able to export it, but the output table looks like this: output

All the lists in the array appear in the same cell. Instead, I would like to have each list in a row, and each element in the list in a column. Even better if I could add the name of the columns (band1, band2, R, p-value)

My code: https://code.earthengine.google.com/?scriptPath=users%2Fericacinerea%2Fgee_training%3Aband_correlation

and one extract of it:

//check the correlation between bands in a stack

//var list_of_results = ee.List([]);
var myArray = []; // recommended way

//Get the names of the input bands
var bands = ["b1","b2","b3","b4"];

for (var i=0; i<bands.length;i++) {
  for (var j=i+1; j<bands.length;j++) {
    var sample = image.sample({'region':aoi, 
                            'scale': 1000, 
                            'projection': 'EPSG:4326',
                            'numPixels':100});
    var correl = ee.Reducer.pearsonsCorrelation();
    var reduced = sample.reduceColumns(correl, [bands[i], bands[j]]);
    var results = [bands[i],bands[j], reduced.get('correlation'), reduced.get('p-value')];
    //print(results);
    myArray.push(results);
    //var list_of_results = list_of_results.add(results);
  }
}

//print outputs in lines
var csv = myArray.map(function(row){ 
  //var list = [row[0], row[1], row[2], row[3]];
  print(ee.String(row[0]).cat(',').cat(row[1]).cat(',').cat(row[2]).cat(',').cat(row[3]));
});

Export.table.toDrive(
    ee.FeatureCollection([
        ee.Feature(null, {myArray: myArray})
    ]));
1
  • 1
    Welcome to GIS SE. As a new user, please take the Tour. Coding Questions are generally req3 to have code (not just a link). Please Edit the Question.
    – Vince
    Commented Jun 3, 2022 at 11:17

2 Answers 2

0

Based in printed myArray variable in GEE console, you have a list of 6 elements with 4 elements in each one (total 24 possibilities). For reducing to only 12 possibilities (including band1, band2, R, p-value as column name), my suggestion is to modify your script as follows:

print(myArray);

//print outputs in lines
var csv = myArray.map(function(row){ 
  //var list = [row[0], row[1], row[2], row[3]];
  //print(ee.String(row[0]).cat(',').cat(row[1]).cat(',').cat(row[2]).cat(',').cat(row[3]));
});

Export.table.toDrive(
    ee.FeatureCollection([
        ee.Feature(null, {
          'b1,2_R': ee.List(ee.List(myArray).get(0)).get(2),
          'b1,2_p_value': ee.List(ee.List(myArray).get(0)).get(3),
          'b1,3_R': ee.List(ee.List(myArray).get(1)).get(2),
          'b1,3_p_value': ee.List(ee.List(myArray).get(1)).get(3),
          'b1,4_R': ee.List(ee.List(myArray).get(2)).get(2),
          'b1,4_p_value': ee.List(ee.List(myArray).get(2)).get(3),
          'b2,3_R': ee.List(ee.List(myArray).get(3)).get(2),
          'b2,3_p_value': ee.List(ee.List(myArray).get(3)).get(3),
          'b2,4_R': ee.List(ee.List(myArray).get(4)).get(2),
          'b2,4_p_value': ee.List(ee.List(myArray).get(4)).get(3),
          'b3,4_R': ee.List(ee.List(myArray).get(5)).get(2),
          'b3,4_p_value': ee.List(ee.List(myArray).get(5)).get(3)

        })
    ]));

Above modifications it will produce each list in a row and each element in the list in a column as you desire. It can be observed in following image for new exported CSV file. Complete code here.

enter image description here

0

I have finally solved the problem, see the new code at: https://code.earthengine.google.com/?scriptPath=users%2Fericacinerea%2Fgee_training%3Aband_correlation_v2

//check the correlation between bands in a stack

var myArray = []; // recommended way

//Get the names of the input bands
var bands = ["b1","b2","b3","b4"];

for (var i=0; i<bands.length;i++) {
  for (var j=i+1; j<bands.length;j++) {
    var sample = image.sample({'region':aoi, 
                            'scale': 1000, 
                            'projection': 'EPSG:4326',
                            'numPixels':100});
    var correl = ee.Reducer.pearsonsCorrelation();
    var reduced = sample.reduceColumns(correl, [bands[i], bands[j]]);
    var results = {'band1':bands[i],'band2': bands[j], 'R': reduced.get('correlation'), 'p-value': reduced.get('p-value')};
    //print(results);
    myArray.push(results);
    //var list_of_results = list_of_results.add(results);
  }
}

var band_correlation_results = ee.FeatureCollection(myArray
  .map(function (dict, i) {
    return ee.Feature(null, dict);
  })
);

print(band_correlation_results, 'band_correlation_results');


Export.table.toDrive(band_correlation_results);

And the output looks like this: enter image description here

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.