2

I am new researcher using GEE and want to use a shapefile (e.g.,Provincial boundaries) to achieve NDVI mean of each province using GEE, how should we do it?

code:

// Create a mosaic of Landsat 5 images from June through September, 2007.
var allBandMosaic = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
  .filterDate('2016-03-01', '2016-11-30')
  .select('B[0-7]')
  .median();

// Var geometry = ee.Geometry.Rectangle([77, 17, 135, 56]);

// Use the normalizedDifference(A, B) to compute (A - B) / (A + B)
var ndvi = allBandMosaic.normalizedDifference(['B5', 'B4']);

var palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
               '74A901', '66A000', '529400', '3E8601', '207401', '056201',
               '004C00', '023B01', '012E01', '011D01', '011301'];


// Center the map
Map.setCenter(103.16, 34.82, 8);

// Display the input image and the NDVI derived from it.
Map.addLayer(ndvi, {min: 0, max: 1, palette: palette}, 'NDVI');

// Export the image, specifying scale and region.
//Export.image.toDrive({
//  image: ndvi,
//  description: 'ndviToDriveExample',
//  scale: 30,
//  region: geometry
//});
var fc = ee.FeatureCollection('users/qiangsun/shengjie')
  .filter(ee.Filter.stringContains('ENAME', 'Gansu'));
Map.addLayer(fc,{color:'FF0000'},'shenjie');

var means = ndvi.reduceRegions({
  collection: fc,
  reducer: ee.Reducer.mean(),
  scale: 30,
});
means = means.get('ndvi')
print('gansu',means);

However, I want to batch process for all provincial mean NDVI in China, and why the result for Gansu is null?.

closed as unclear what you're asking by Kersten, LaughU, whyzar, KHibma, csk Nov 14 '18 at 19:35

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

I have no access to users/qiangsun/shengjie to reproduce the exact error, but supposing that gansu is just one city, you have to use reduceRegion instead of reduceRegions. And you also have to notice that Image.normalizedDifference returns a band called nd. You could rename it if you really need it to be named ndvi

var means = ndvi.reduceRegion({
  geometry: fc.first().geometry(),
  reducer: ee.Reducer.mean(),
  scale: 30,
});
means = means.get('nd')
print('gansu',means);

To compute the mean for all cities use reduceRegions

// Remove the filter for Gansu
var fc = ee.FeatureCollection('users/qiangsun/shengjie')
//           .filter(ee.Filter.stringContains('ENAME', 'Gansu'));
Map.addLayer(fc,{color:'FF0000'},'shenjie');

var means = ndvi.reduceRegions({
  collection: fc,
  reducer: ee.Reducer.mean(),
  scale: 30,
});

// the resulting mean is a FeatureCollection
// so you can export it as a table
Export.table.toDrive({
    collection: means, 
    description: 'Some Description', 
    folder: 'Some_folder', 
    fileNamePrefix: 'name_of_the_file', 
    fileFormat: 'CSV'
})
  • i am deeply grateful for your help, actually, the users/qiangsun/shengjie contains all provinces of China. Gansu is the one of provinces including in the shapefile. and i would like know how to get NDVI mean for each province in China quickly. thank you~~~ – qiangqiang sun Oct 29 '18 at 2:18
  • So, did my answer helped you? Is it what you needed? – Rodrigo E. Principe Oct 29 '18 at 3:04
  • I'm afraid not. if i have a shapefile with thousands of polygons, i want to know how to get NDVI mean for each polygon. and export it. – qiangqiang sun Oct 29 '18 at 8:18
  • What did the reduceRegions function do? It should do what you are looking for. – Rodrigo E. Principe Oct 29 '18 at 10:18
  • as you said, i do not have any experiences for the reduceRegions function? i learn from others with simple usage. sorry – qiangqiang sun Oct 29 '18 at 11:11

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