0

I need to plot the spectral signature of training data in each of the 8 classes of the supervised classification script I wrote in GEE. The variable 'training_points' is made of sample points I selected in GEE interface and I merged them using:

var Training_Points = Water.merge(Residential).merge(Agricultural).merge(Arbusti).merge(BoschiMisti).merge(Latifoglie).merge(Conifere).merge(BareSoil);

The variable 'input 'is an imagecollection as:

var input = ee.Image(mean_Summer_IC);

Then I used these two variables in the script below:

//Create training data
var train = input.sampleRegions({
  collection: Training_Points, 
  properties: ['land_class'], 
  scale: 10,
  tileScale: 2
});

var BANDS = input.bandNames()
var numBands = BANDS.length()
var bandsWithClass = BANDS.add('land_class')
var classIndex = bandsWithClass.indexOf('land_class')

// Use .combine() to get a reducer capable of 
// computing multiple stats on the input
var combinedReducer = ee.Reducer.mean().combine({
  reducer2: ee.Reducer.stdDev(),
  sharedInputs: true})

// Use .repeat() to get a reducer for each band
// We then use .group() to get stats by class
var repeatedReducer = combinedReducer.repeat(numBands).group(classIndex)

var Training_PointsStats = train.reduceColumns({
    selectors: BANDS.add('land_class'),
    reducer: repeatedReducer,
})

// Result is a dictionary, we do some post-processing to
// extract the results
var groups = ee.List(Training_PointsStats.get('groups'))

var classNames = ee.List(['Water','Residential', 'Agricultural', 'Arbusti', 'BoschiMisti', 'Latifoglie', 'Conifere', 'BareSoil'])

var fc = ee.FeatureCollection(groups.map(function(item) {
  // Extract the means
  var values = ee.Dictionary(item).get('mean')
  var groupNumber = ee.Dictionary(item).get('group')
  var properties = ee.Dictionary.fromLists(BANDS, values)
  var withClass = properties.set('class', classNames.get(groupNumber))
  return ee.Feature(null, withClass)
}))

// Chart spectral signatures of training data
var options = {
  title: 'Average Spectral Signatures',
  hAxis: {title: 'Bands'},
  vAxis: {title: 'Reflectance', 
    viewWindowMode:'explicit',
    viewWindow: {
        max:0.6,
        min:0
    }},
  lineWidth: 1,
  pointSize: 4,
  series: {
    0: {color: '105af0'},                                   
    1: {color: 'dc350a'}, 
    2: {color: 'caa712'}, 
    3: {color: 'b9ffa4'},
    4: {color: '369b47'},
    5: {color: '21ff2d'},
    6: {color: '275b25'},
    7: {color: 'f7e084'},                         
}};

// Default band names don't sort propertly
// Instead, we can give a dictionary with
// labels for each band in the X-Axis
var bandDescriptions = {
  'B2': 'B2/Blue',
  'B3': 'B3/Green',
  'B4': 'B4/Red',
  'B5': 'B5/Red Edge 1',
  'B6': 'B5/Red Edge 2  ',
  'B7': 'B7/Red Edge 3  ',
  'B8': 'B8/NIR',
  'B8A': 'B8A/Red Edge 4    ',
  'B11': 'B11/SWIR-1',
  'B12': 'B12/SWIR-2'
}

// Create the chart and set options.
var chart = ui.Chart.feature.byProperty({
  features: fc,
  xProperties: bandDescriptions,
  seriesProperty: 'class'
})
.setChartType('ScatterChart')
.setOptions(options);

print(chart)

var classChart = function(land_class, label, color) {
  var options = {
  title: 'Spectral Signatures for ' + label + ' Class',
  hAxis: {title: 'Bands'},
  vAxis: {title: 'Reflectance', 
    viewWindowMode:'explicit',
    viewWindow: {
        max:0.6,
        min:0
    }},
  lineWidth: 1,
  pointSize: 4,
  };

  var fc = train.filter(ee.Filter.eq('land_class', land_class))
  var chart = ui.Chart.feature.byProperty({
  features: fc,
  xProperties: bandDescriptions,
  })
.setChartType('ScatterChart')
.setOptions(options);

print(chart)
}
classChart(0, 'Water')
classChart(1, 'Residential')
classChart(2, 'Agricultural')
classChart(3, 'Arbusti')
classChart(4, 'BoschiMisti')
classChart(5, 'Latifoglie')
classChart(6, 'Conifere')
classChart(7, 'BareSoil')

and I keep receiving errors:

Cannot read properties of undefined (reading 'properties')
List.get:List index must be between 0 and 7, or -8 and -1. Found 8.

the series is indeed set from 0 to 7 so I cannot understand why I keep getting this error. The link to the whole script is here starting from Line 647: https://code.earthengine.google.com/d613222ee468a9afa8abf917b95844ac?accept_repo=users%2Fessepratico%2FPratico_et_al_RemoteSensing_2021

1 Answer 1

0

I solved the error by adding these lines to the script:

Training_Points = Training_Points.remap({
  lookupIn: [1, 2, 3, 4, 5, 6, 7, 8],
  lookupOut:[0, 1, 2, 3, 4, 5, 6, 7],
  columnName: 'land_class'
})

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.