3

When I try to generate chart by region, I get following error:

Error generating chart: String: Unable to convert object to string.

How can I revise my code to remove this error?

var image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_026047_20140216')
    .select(['B[1-7]']);
Map.addLayer(image, {bands: ['B5', 'B4', 'B3'], min: 0, max: 0.5});

// Define and display a FeatureCollection of 7 known locations.
var points = ee.FeatureCollection([
  ee.Feature(ee.Geometry.Point(83.978710, 283202722)),
  ee.Feature(ee.Geometry.Point(84.008199, 28.218142)),
  ee.Feature(ee.Geometry.Point(84.2250, 28.371391)),
  ee.Feature(ee.Geometry.Point(83.947601, 28.215001)),
  ee.Feature(ee.Geometry.Point(83.895411, 28.588669)),
  ee.Feature(ee.Geometry.Point(84.042354, 28.523533)),
  ee.Feature(ee.Geometry.Point(84.085761,28.170022))
]);
Map.addLayer(points);


// Define a list of Landsat 8 wavelengths for X-axis labels.
var wavelengths = [0.44, 0.48, 0.56, 0.65, 0.86, 1.61, 2.2];

// Create the chart and set options.
var spectraChart = ui.Chart.image.regions(
    image, points, ee.Reducer.mean(), 30, 'label', wavelengths)
        .setChartType('ScatterChart')
        .setOptions(options);
var classNames = ee.List([ 'built_up', 'grassland', 'forest', 'water','snow','rock/barren','cultivation']);

// Define customization options.
var options = {
  title: 'Landsat 8 TOA spectra at three points near  City',
  hAxis: {title: 'Wavelength (micrometers)'},
  vAxis: {title: 'Reflectance'},
  lineWidth: 1,
  pointSize: 4,
  series: {
    0: {color: '00FF00'},// built_up
    1: {color: '00FF00'}, // grassland
    2: {color: '228B22'},//forest
    3: {color: '0000FF'}, // water
    4: {color: 'ADD8E6'}, // rock/barren
    5: {color: '808080'},//snow
    6: {color: 'FFFF00'}// cultivation
}};

// Define a list of Landsat 8 wavelengths for X-axis labels.
var wavelengths = [0.44, 0.48, 0.56, 0.65, 0.86, 1.61, 2.2];

// Create the chart and set options.
var spectraChart = ui.Chart.image.regions(
    image, points, ee.Reducer.mean(), 30, classNames, wavelengths)
        .setChartType('ScatterChart')
        .setOptions(options);

// Display the chart.
print(spectraChart);

code link: https://code.earthengine.google.com/0227789fa9d4b62979aa111d2d1e99ab

1 Answer 1

3

You first spectra chart fails because the points are not within the image bounds, so the chart actually has no values. Make sure these point fall within the image pixels. For simplicity, I used:

var points = ee.FeatureCollection.randomPoints(image.geometry(), 7);

The second chart fails because you incorrectly define the argument seriesProperty as an list. It should be a string which is a propertyName of the features of the regions argument. For example, set a property class with corresponding classNames to each feature. Then call class as seriesProperty:

// make class names and set each className to a feature
var classNames = ee.List([ 'built_up', 'grassland', 'forest', 
                           'water','snow','rock/barren','cultivation']);
var pointsWithClass = points.map(function(feat){
  return feat.set('class', classNames.getString(
                       ee.Number.parse(feat.getString('system:index'))))
})

// Create the chart and set options.
var spectraChart = ui.Chart.image.regions(
    image, pointsWithClass, ee.Reducer.mean(), 30, 'class', wavelengths)
        .setChartType('ScatterChart')
        .setOptions(options);

Make sure variable options is defined before you define the spectraChart. Link code

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.