1

I am trying hard to get principal component analysis results as 'chart/csv.' format by applying ui.chart code but it shows the error "Error generating chart: No features contain non-null values of "system:time_start". I used Landsat 8 image (LANDSAT/LC08/C02/T1_TOA), and my geometry: [-114.07383847413794,51.042771673104525].

I Followed GEE resources: https://developers.google.com/earth-engine/tutorials/edu

    var image = ee.Image(landsat8
    .filterBounds(point)
    .filterDate('2015-06-01', '2015-09-01')
    .sort('CLOUD_COVER')
    .first());

    // PCA
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11'];

var arrayImage = image.select(bands).toArray();
var covar = arrayImage.reduceRegion({
  reducer: ee.Reducer.covariance(),
  maxPixels: 1e9
});

var covarArray = ee.Array(covar.get('array'));
var eigens = covarArray.eigen();

var eigenVectors = eigens.slice(1, 1);

// Perform the matrix multiplication, as with the TC components:

var principalComponents = ee.Image(eigenVectors)
.matrixMultiply(arrayImage.toArray(1));
var pcImage = principalComponents
  // Throw out an an unneeded dimension, [[]] -> [].
  .arrayProject([0])
  // Make the one band array image a multi-band image, [] -> image.
  .arrayFlatten([['pc1', 'pc2', 'pc3', 'pc4', 'pc5', 'pc6', 'pc7', 'pc8']]);

Map.addLayer(pcImage.select('pc1'), {}, 'PC');
Map.centerObject(pcImage)
print(pcImage)


var chart = ui.Chart.image.series({
  imageCollection: pcImage,
  region: point,
  reducer: ee.Reducer.mean(),
  scale: 30 // Adjust the scale as needed
})
.setChartType('LineChart')
.setOptions({
  title: 'PCA Time Series',
  hAxis: { title: 'Date' },
  vAxis: { title: 'PCA Value' },
  lineWidth: 1,
  pointSize: 3
});

print(chart);
3
  • The ui.Chart.image.series() is for a collection of images, while the current code with pcImage has only one image. In the first image variable, image with the lowest cloud cover is filtered and stored. Do you want to show a time series of PCA or for only one image?
    – Padmanabha
    Sep 3, 2023 at 19:33
  • Yes I do, would you please help me what should I do for getting time series of PCA @Padmanabha Sep 5, 2023 at 0:56
  • I have posted an answer. @HanifBhuian
    – Padmanabha
    Oct 11, 2023 at 3:25

1 Answer 1

0

To create a time series of Principal component values, several images from different periods are required. In the following example, I have given an example that uses the least cloud cover images from May to September for a series of years.

    // Change according to requirements
    var start_year=2015
    var end_year=2022
    var years=ee.List.sequence(start_year,end_year,1).map(function(y){return ee.Number(y).int()})
    
    var landsat_images=ee.ImageCollection(years.map(function(y){
      var img= landsat8
        .filterBounds(point)
        .filterDate(ee.String(y).cat('-06-01'), ee.String(y).cat('-09-01')) // change dates and months as needed
        .sort('CLOUD_COVER')
        .first()
      return img.set({'Year':(ee.Date(img.get('system:time_start'))).format('YYYY')})
    }))
    
    
    //     // PCA
    var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11'];
    
    var pcImages=landsat_images.map(function(image)
    {
    var arrayImage = image.select(bands).toArray();
    var covar = arrayImage.reduceRegion({
      reducer: ee.Reducer.covariance(),
      maxPixels: 1e9
    });
    
    var covarArray = ee.Array(covar.get('array'));
    var eigens = covarArray.eigen();
    
    var eigenVectors = eigens.slice(1, 1);
    
    // Perform the matrix multiplication, as with the TC components:
    
    var principalComponents = ee.Image(eigenVectors)
    .matrixMultiply(arrayImage.toArray(1));
    return principalComponents
      // Throw out an an unneeded dimension, [[]] -> [].
      .arrayProject([0])
      // Make the one band array image a multi-band image, [] -> image.
      .arrayFlatten([['pc1', 'pc2', 'pc3', 'pc4', 'pc5', 'pc6', 'pc7', 'pc8']])
      .copyProperties(image,['Year']);
    })
    
    // Map.addLayer(pcImage.select('pc1'), {}, 'PC');
    // Map.centerObject(pcImage)
    print(landsat_images)
    
    
    var chart = ui.Chart.image.series({
      imageCollection: pcImages,
      region: point,
      reducer: ee.Reducer.mean(),
      xProperty:"Year",
      scale: 30})
    .setChartType('LineChart')
    .setOptions({
      title: 'PCA Time Series',
      hAxis: { title: 'Year' },
      vAxis: { title: 'PCA Value' },
      lineWidth: 1,
      pointSize: 3
    });
    
    print(chart);

The final chart will show a time series of each bands for the year range mentioned.

enter image description here

2
  • Thank you very much for your kind response! Oct 16, 2023 at 16:37
  • Kindly accept the answer if your problem is solved.
    – Padmanabha
    Oct 16, 2023 at 18:07

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.