0

I am trying to extract pixel values by points and convert to a table in Google Earth Engine. I'd like to extract pixel values by lat,lon points I upload from a csv for each S2 image with a matching date. Before using the entire csv (3000+ points), I created two random points and am following the code in this post to extract points. However, the table it output looks like this. I'm not sure why only MSK_CLDPRB, MSK_SNWPRB, QA10, QA20, and QA60 are showing. I'd like all bands to be present. Can anyone steer me in the right direction?

// random points to create a FeatureCollection
var p1 = ee.Geometry.Point([-76.353, 39.094])
var p2 = ee.Geometry.Point([-76.133, 38.03])
var pts = ee.FeatureCollection(ee.List([ee.Feature(p1),ee.Feature(p2)]))
print(pts)

// Cloud Mask function
function maskS2clouds(image) {
  var qa = image.select('QA60');

  // Bits 10 and 11 are clouds and cirrus, respectively.
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;

  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));

  //apply scaling factor and add prevent loss of timestamp
  return image.updateMask(mask).divide(10000)
    .copyProperties(image, image.propertyNames());
}

// Load Sentinel-2 TOA reflectance data.  
var S2 = ee.ImageCollection('COPERNICUS/S2_SR')
           //.filterDate('2017-03-28', '2020-05-20')
           .filterDate('2020-05-01', '2020-05-20')
           .filterBounds(ee.Geometry.Point(-77, 39))
           .map(maskS2clouds);

print(S2);

// Empty Collection to fill
var ft = ee.FeatureCollection(ee.List([]))

var fill = function(img, ini) {
  // type cast
  var inift = ee.FeatureCollection(ini)

  // gets the values for the points in the current img
  var ft2 = img.reduceRegions(pts, ee.Reducer.first(),30)

  // gets the date of the img
  var date = img.date().format()

  // writes the date in each feature
  var ft3 = ft2.map(function(f){return f.set("date", date)})

  // merges the FeatureCollections
  return inift.merge(ft3)
}

// Iterates over the ImageCollection
var newft = ee.FeatureCollection(S2.iterate(fill, ft))
print(newft)

// Export
Export.table.toDrive(newft,"extract_test");

Link to code

2
  • For sampling raster, GEE has sampleRegions method to extract pixel values for each band of an image. This will sure extract all bands.
    – Kevin
    May 29, 2020 at 9:48
  • @Kevin Thank you, that worked for one image but now I am wondering how to use that to extract band values in images from coordinates with matching dates.
    – aewindle
    May 29, 2020 at 18:41

1 Answer 1

0

You can use map method over your S2 image collection to extract pixel values. Example code is below:

var newft = ee.FeatureCollection(S2.map(function (img) {
  return img.sampleRegions({collection: pts, scale: 30, geometries: true})
})).flatten()

Since each sampleRegions returns a feature collection, the outcome of the map function would be a collection of feature collection. Therefore, flatten is used to convert it to a 1-D feature collection.

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.