2

I am trying to obtain (for further export), a dataset grouping my NDVI values by fields, for which I have polygons.

To do so, I use the Google earth engine function sampleRegions: this takes an image, a Feature Collection, and allows to merge the two, giving a feature collection where each features contains the values of image's pixels contained in each polygon, associated with the values of the corresponding polygons.

The problem is that by returning a feature collection, it loses track of the geometry/location information of all pixels. That is, I can see the values of all pixels in a given field, but do no know to which location within the field these pixels belong.

Any idea on how I could retrieve these?

Or should I adopt another procedure?

// Delineate fields
var Field1 = ee.Geometry.Polygon([
  [[-122.37158060073853,38.458125582533334],
    [-122.3727822303772, 38.45752068253876],
  [-122.37192392349243,38.45646209534339],
  [-122.37063646316528,38.457033398298925],
  [-122.37158060073853,38.458125582533334]]
  ]);
var Field1_F  = ee.Feature(Field1,{id: 1});

var Field2= ee.Geometry.Polygon([
    [[-122.37115144729614,38.45536988592448],
    [-122.369863986969, 38.4560084103617],
    [-122.3706579208374,38.456966186421305],
    [-122.37196683883667,38.45636127670514],
    [-122.37115144729614,38.45536988592448]]])

var Field2_F  = ee.Feature(Field2,{id: 2});
var Fields_FC = ee.FeatureCollection([Field1_F,Field2_F]);

// get NDVI
var NDVI = ee.ImageCollection('LANDSAT/LE7_L1T_32DAY_NDVI').
mean(); 

// Merge NDVI and field
var pixelsID = NDVI.sampleRegions(Fields_FC, ['id'], 30);

Map.setCenter(-122.3713231086731,38.456663732197235, 17)
Map.addLayer(Fields_FC, {}, 'feature');
Map.addLayer(NDVI, 
    {min:-1, max:1, opacity: 0.9, palette: ['blue', 'green', 'red']},         "NDVI")


// this is almost the perfect output, yet does not contain pixel location
print(pixelsID, "pixelsID")
  • What is your desired output? You could easily mask and export each field individually and therefore keep the value and geolocation of each pixel. What do you plan to do with the data? – Kersten Feb 24 '17 at 12:07
  • Hi @Kersten My ideal data is the raster, with additional band indicating in which field/polygon each pixel falls. Or alternatively, a dataset with one row for each pixel, pixel centroid and NDVI+ field. This will be used later on for more sophisticated statistical analysis in R not available in EE. Thanks! – Matifou Feb 25 '17 at 18:01
  • So a rasterized version of your field polygons as additional layer would be enough? So that for each pixel you have the NDVI values and as second band the field ID? – Kersten Feb 25 '17 at 20:08
  • Exactly! Any idea on how I could do that? – Matifou Feb 25 '17 at 20:43
2

What you want to do is convert your FeatureCollection of polygons to a raster with .reduceToImage(). Once that is done you can add it as a new band to your existing NDVI mean using .addBands(). The multiband image can then be exported for further analysis.

Here's the example: https://code.earthengine.google.com/2e2dad3f39d32ee75f840239620e67d0

and the code:

// Delineate fields
var Field1 = ee.Geometry.Polygon([
  [[-122.37158060073853,38.458125582533334],
    [-122.3727822303772, 38.45752068253876],
  [-122.37192392349243,38.45646209534339],
  [-122.37063646316528,38.457033398298925],
  [-122.37158060073853,38.458125582533334]]
  ]);
var Field1_F  = ee.Feature(Field1,{id: 1});

var Field2= ee.Geometry.Polygon([
    [[-122.37115144729614,38.45536988592448],
    [-122.369863986969, 38.4560084103617],
    [-122.3706579208374,38.456966186421305],
    [-122.37196683883667,38.45636127670514],
    [-122.37115144729614,38.45536988592448]]])

var Field2_F  = ee.Feature(Field2,{id: 2});
var Fields_FC = ee.FeatureCollection([Field1_F,Field2_F]);

//convert field vector to raster
var fieldsRaster = Fields_FC.reduceToImage(['id'], ee.Reducer.firstNonNull()).select(['first'], ['field_id'])

// get NDVI mean over all Landsat 7 32day composites
var NDVI = ee.ImageCollection('LANDSAT/LE7_L1T_32DAY_NDVI').mean(); 

// add field ID as second band to mean NDVI image
var NDVIFields = NDVI.addBands(fieldsRaster)


Map.setCenter(-122.3713231086731,38.456663732197235, 17)
// Map.addLayer(Fields_FC, {}, 'feature');
// Map.addLayer(NDVI, 
    // {min:-1, max:1, opacity: 0.9, palette: ['blue', 'green', 'red']}, "NDVI")

Map.addLayer(fieldsRaster, {}, 'rasterized field features')
Map.addLayer(NDVIFields, {}, 'multiband of mean NDVI and field IDs')
  • Awesome, exactly what I needed. Thanks so much for following up closely on the question and giving such clear answer, much appreciated! – Matifou Feb 27 '17 at 0:12

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.