1

My goal is do a land cover classification using Landsat 8 imagery and my shapefile which when imported to GEE, looks like this,

Feature  HID    ClassName   SuperclassID
Index  
  0      911      Veg1           900    
  1      911      Veg1           900
  2      912      Veg2           900
......      

So far using the below script I've been able to load the imagery and the vector file

var L8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_RT_TOA");
var table = ee.FeatureCollection("path/to/shapefile")

function preprocess(table, class_id){
  /**
   Preprocess and return training data points

   * @param  {ee.FeatureCollection} table - Shapefile  
   * @param  {number} class_id -  Id of superclass to be classified
   * @return {[type]}     [description]
   */
   
   var vfilter = ee.Filter.inList('SuperClassID', [class_id]);
   var filtfeats = table.filter(vfilter);
   
   
   var ids = [911, 912, 913];
   var labels = [0, 1, 2]

   // Remap ids 
   filtfeats = filtfeats.remap(ids, labels, 'HID')

   // Generate training points 
   ......
   return points


points = preprocess(table, 900)

How can I generate training data points from filtfeats or more generally a FeatureCollection?

1 Answer 1

0

I was able to solve this via the following code snippet

var L8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_RT_TOA");
var table = ee.FeatureCollection("path/to/shapefile")

function preprocess(table, class_id){
  /**
   Preprocess and return training data points

   * @param  {ee.FeatureCollection} table - Shapefile  
   * @param  {number} class_id -  Id of superclass to be classified
   * @return {[type]}     [description]
   */
   
   var vfilter = ee.Filter.inList('SuperClassID', [class_id]);
   var filtfeats = table.filter(vfilter);
   
   
   var ids = [911, 912, 913];
   var labels = [0, 1, 2]

   // Remap ids 
   filtfeats = filtfeats.remap(ids, labels, 'HID')

   // Generate training points 
   // 1. Make an image out of the HID attribute.
   var img = filtfeats
      .filter(ee.Filter.notNull(['HID']))
      .reduceToImage({
        properties: ['HID'],
        reducer: ee.Reducer.first()})
      .rename('class')
      .byte()
      .addBands(ee.Image.pixelLonLat());
      
  // 2. Sample points
  var points = img.sample({
    'region': roi,
    'numPixels': 5000,
    'scale': 30,
    'seed':0,
    'geometries': true
  });
   
  return points


points = preprocess(table, 900)

Being new to Earth Engine, I'm not sure if this is the best way to this. So if anyone has any better ideas, please let me know.

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.