Extract pixel values by points and convert to a table in Google Earth Engine

可紊 提交于 2019-12-03 22:07:05

I can't get access to your fusion table, so I made up some random points for the example. I'm pretty sure there is other ways to do it. GEE has many functions and some times they are a bit tricky to use. This would be my way:

// As I can't access your FusionTable,
// I make random points and create a FeatureCollection
var p1 = ee.Geometry.Point([142.36083984375, -37.466138602344046])
var p2 = ee.Geometry.Point([143.23974609375, -37.04640889969956])
var pts = ee.FeatureCollection(ee.List([ee.Feature(p1),ee.Feature(p2)]))

//IMPORT LANDSAT IMAGE
var L82014pre = ee.ImageCollection('LANDSAT/LC8_SR') //Landsat 8 Surface   reflectance
.filter(ee.Filter.eq('wrs_path', 94))
.filter(ee.Filter.eq('wrs_row', 86)) 
.filterDate(ee.Date.fromYMD(2013,12,13), ee.Date.fromYMD(2014,1,15))

// 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(L82014pre.iterate(fill, ft))

// Export
Export.table.toDrive(newft,
"anyDescription",
"anyFolder",
"anyNameYouWant")

@Rodrigo, thank you for your prompt reply. The point extraction code worked perfectly for that example. I tried the point extraction for another set of images for which a cloud mask had been applied, and discovered that if the first point of the first image in the collection fell outside the mask (i.e. on a pixel assigned a 'null' value), the exported table would not contain band data for any of the point. To get around this I added a filter to remove points containing 'null' values before they are merged with the feature collection.

Below is the code containing an example with both the failed extraction that does not remove 'null' values and the successful extraction removing 'null' values.

//cloud mask ------------------------------------------------------  
var maskCloudShadow = function(image){
var cfmask = image.select('cfmask');
return image.updateMask(cfmask.lt(1));   // keep clear (0) pixels
};

//select images from image collection -----------------------------
//extract filtered collection of Landsat 5 Surface Reflection
var L5fs1998post = ee.ImageCollection('LANDSAT/LT5_SR') //Landsat 5 Surface reflectance
.filterDate('1998-1-9', '1998-2-27') //filter to date bounds 
.filter(ee.Filter.eq('wrs_path', 91))//filter to path and row
.filter(ee.Filter.eq('wrs_row', 86))
.map(maskCloudShadow); //apply cloud and cloud shadow mask function
print(L5fs1998post);

//Create sample points --------------------------------------------
//var pts = 
ee.FeatureCollection('ft:1lfLgiQQSIIOpgjuZV5MZJno9_kLyQC49w6u3Hf9W');
var p1 = ee.Geometry.Point([146.84341192245483, -37.47371711676642]);
var p2 = ee.Geometry.Point([146.84167385101318, -37.4]);
var pts = ee.FeatureCollection(ee.List([ee.Feature(p1),ee.Feature(p2)]));

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

//Without removal of null values ----------------------------------
//Function to extract values from image collection based on point file and export as a table 
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(L5fs1998post.iterate(fill, ft));
print(newft);

// Export
Export.table.toDrive(newft,
"anyDescription",
"EarthEngine",
"sample_include_null");

//With removal of null values ------------------------------------------
//Function to extract values from image collection based on point file and export as a table 
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

var ft3a = ft3.filter(ee.Filter.neq('B1', null));//filter first to remove null values
return inift.merge(ft3a);
};

// Iterates over the ImageCollection
var newft_remove_null = ee.FeatureCollection(L5fs1998post.iterate(fill, ft));
print(newft_remove_null);

// Export
Export.table.toDrive(newft_remove_null,
"anyDescription",
"EarthEngine",
"sample_remove_null");

//plot cloudy scene and sample points ------------------------------------
var scene = ee.Image('LANDSAT/LT5_SR/LT50910861998042');
Map.setCenter(147, -37.5, 9);
Map.addLayer(scene, {bands: ['B3', 'B2', 'B1'], min: 0, max: 2000}, 'false-color composite');
Map.addLayer(pts);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!