4

I'm trying to write an earth engine script which extracts the pixel band values inside a polygon. (geometry) Only for pixels completely within the polygon. The polygons will be quite small. (field size)

I have looked through the docs, and searched for this, but am unable to find a solution. Have only found statistical reductions.

// get the dates
var startDate = ee.Date.fromYMD(2018,1,1);
var endDate = ee.Date.fromYMD(2018,2,1);

// Create a geometry representing an export region.
var myGeometry = ee.Geometry.Polygon(
[[[-61.15985870361328, -32.94904673848672],
      [-61.152305603027344, -32.944797148619905],
      [-61.15659713745117, -32.9395388987784],
      [-61.16432189941406, -32.94378874137744]]]);



var landsatCollection = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR");

// Filter image collection to dateRange of interest
var landsatDateCollection = ee.ImageCollection(landsatCollection
.filterDate(startDate,endDate));

//Filter to AOI.
var landsatAOI = ee.ImageCollection(landsatDateCollection
.filterBounds(myGeometry));

// Filter to image with least clouds
var landsatBestImage = ee.Image(landsatAOI
.sort('CLOUD_COVER')
.first());

// Load a landsat image and select three bands.
var landsatDownload = ee.Image(landsatBestImage)
.select(['B4', 'B5']);

/////////
//Added code that was improved after Jon's comments
/////////


// generate a new image containing lat/lon of the pixel and reproject it to Landsat projection
var coordsImage = ee.Image.pixelLonLat().reproject(landsatDownload.projection());

var joinedImage = coordsImage.addBands(landsatDownload);

var valuesList = joinedImage.reduceRegion({
  reducer: ee.Reducer.toList(4),
  geometry: myGeometry
}).values().get(0);

print(valuesList);

Jon's inspiration on the MODIS script allowed the modification above. The valuesList now contains all the pixel information.

To see how to Export the data in valuesList to a CSV see

Earth Engine convert list with coordinates and values into a feature collection for export

8
  • What is your expected output? You could do this pretty easily if you just wanted to export an image; just mask the image with a mask created by your polygons, then export at native resolution.
    – Jon
    Commented Apr 6, 2018 at 17:44
  • The idea is to extract the pixel centroid coordinates, and the value of B4, and B5. Later I will add the date image etc. Commented Apr 6, 2018 at 19:24
  • 1
    Here's an example for how to do it with a Modis image: code.earthengine.google.com/2acea4c7e2f39c372d95a1cd06547577
    – Jon
    Commented Apr 6, 2018 at 21:06
  • Also: groups.google.com/forum/#!searchin/…
    – Jon
    Commented Apr 6, 2018 at 21:08
  • Thanks Jon. I'm exploring the code for modis to see if I can adapt it to landsat. As is, it has an error which I can't find the reason to. If I find what it is, I'll post back. The google group seems to be blocked due to spam, or malicious content. Commented Apr 7, 2018 at 11:07

0