2

I'd like to sample points from temporal Sentinel-2 images covering a state, about 13 month's mosaic image were stacked into a new image. When I sampled points on the image, I got nothing. I think maybe some null or masked points led this case. I wonder is there a good solution?

//Load state borders from assets

var studyArea = ee.Feature(ee.FeatureCollection('ft:17aT9Ud-YnGiXdXEJUyycH2ocUqreOeKGbzCkUw')
    .filter(ee.Filter.eq('id', 'MO'))
    .first()).getInfo().geometry;
var start = ee.Date('2017-01-01');
var finish = ee.Date('2017-12-31');

// Map the function over one year of data and take the median. // Load Sentinel-2 TOA reflectance data.

var s2col = ee.ImageCollection('COPERNICUS/S2')
                  .filterDate(start, finish)
                  .filterBounds(studyArea)
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30));

// Difference in days between start and finish

var diff = finish.difference(start, 'day')

print(diff)

var temporalResolution = 30 // days

// Make a list of all dates

var range = ee.List.sequence(0, diff.subtract(1), temporalResolution).map(function(day){return start.advance(day,'day')})

print('range',range)

// Function for iteraton over the range of dates

var day_mosaics_s2 = function(date, newlist) 
{
   date = ee.Date(date)
  newlist = ee.List(newlist)
  var filtered = s2col.filterDate(date, date.advance(temporalResolution, 'day'))
  var image = ee.Image(filtered.mosaic().clip(studyArea)
 .select('B2','B3','B4','B5','B6','B7','B8','B8A','B11','B12'));
  return ee.List(ee.Algorithms.If(filtered.size(), newlist.add(image), newlist))
}


var news2col = ee.ImageCollection(ee.List(range.iterate(day_mosaics_s2, ee.List([]))));

print('mosaicImageCollection_s2',news2col);


var stackCollection = function(collection) 
{
  var first = ee.Image(collection.first()).select([]);
  var appendBands = function(image, previous) 
 {
    return ee.Image(previous).addBands(image);
  };
  return ee.Image(collection.iterate(appendBands, first));
};

var stacked_s2 = stackCollection(news2col);

print('stacked_composite bands', stacked_s2.bandNames());


var training = stacked_s2.sample(
  {
  numPixels: 500,
  scale:10,
  seed: 0
  }
  );

  print('samples',training)
  • As a new user, please take the Tour, which explains how GIS SE operates. All coding questions should include the code in the question body (links to third party sites can fail over time, harming question quality). – Vince Jan 10 '19 at 0:24
2

Probably that is because when you use sample, it will only return a feature when in that image all the bands have a value. Some of your bands in the stacked_s2 have masked values, simply because your temporal range is too small to fill all the pixels with a value. That is way sample won't return features when you use all the bands. If you will try sample on stacked_s2 when selecting a couple of bands, you will see that it will return feature samples.

I got two solutions for you to overcome this:

You can keep using sample, but you will have to unmask all masked pixels in your image stacked_s2. Then you can later filter out feature samples containing these numbers:

var stackCollection = function(collection) {
  var first = ee.Image(collection.first()).select([]);
  var appendBands = function(image, previous) {
    return ee.Image(previous).addBands(image);
  };
  return ee.Image(collection.iterate(appendBands, first)).unmask(-99);  // unmask the masked pixels with a value
};

var stacked_s2 = stackCollection(news2col);

var training = stacked_s2.sample({
  region: studyArea,
  numPixels: 500,
  scale: 10,
  seed: 0
});

print('samples works when masked pixels are unmasked', training)

Or you could use randomPoints to generate random samples in your area of interest. Then use reduceRegions and 500 sample points will be returned with 'null' values where the image band was masked.

var points = ee.FeatureCollection.randomPoints(studyArea, 500, 0, 1);
var samples = stacked_s2.reduceRegions(points, ee.Reducer.first(), 10);
print('samples from randompoints: works without unmasking', samples)

Link to script

| improve this answer | |
  • It worked! A small change makes a big difference, thanks. – Jsun Jan 10 '19 at 15:02
  • @ Kuik One more question, how can i delete null or masked points in FeatureCollection sampled from the stacked temporal images in GEE quickly? – Jsun Jan 15 '19 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.