1

I have one shapefile of agriculture plots that looks like this-

enter image description here

I want to run analysis in google earth engine but for each plot.

Is there any way to run this analysis without saving each plot and upload it as table?

Can google earth engine run my analysis for each plot separately while they are all in one shapefile?

this is my code-


var geometry=table;//this is the featurecollection

/**
 * Function to mask clouds using the Sentinel-2 QA band
 * @param {ee.Image} image Sentinel-2 image
 * @return {ee.Image} cloud masked Sentinel-2 image
 */
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));

  return image.updateMask(mask).divide(10000)
  .copyProperties(image, ['system:time_start']);
}

// Map the function over one year of data and take the median.
// Load Sentinel-2 TOA reflectance data.
var dataset = ee.ImageCollection('COPERNICUS/S2')
                  .filterDate('2019-06-01', '2019-06-30')
                  // Pre-filter to get less cloudy granules.
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                  .select('B1','B2','B3','B4','B8','QA60')
                  .filterBounds(geometry)
                  .map(maskS2clouds);

var rgbVis = {
  min: 0.0,
  max: 0.3,
  bands: ['B4', 'B3', 'B2'],
};

var clippedCol=dataset.map(function(im){ 
   return im.clip(geometry);
});


//test if clipping the image collection worked
Map.centerObject(geometry,9);
Map.addLayer(clippedCol.median(), rgbVis, 'RGB');

//function to calculate NDVI
var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4'])
  .rename('NDVI')
  .copyProperties(image,['system:time_start']);
  return image.addBands(ndvi);

};

//NDVI to the clipped image collection
var withNDVI = clippedCol.map(addNDVI).select('NDVI');


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


//analyze images from image collection collection
var listOfImages = withNDVI.toList(withNDVI.size());
var listOfNumbers =[0,1,2];

var myImage = withNDVI.first()

var collectData = function(feature){
  var myGeom = feature.geometry()
  var myReduction = myImage.reduceRegion({
                  reducer: ee.Reducer.mean(),
                  geometry: myGeom,
                  scale: 30            // Change to match your need
    })
  return(ee.Feature(myGeom,{mean: myReduction}))
}


var myNewFeatures = geometry.map(collectData) ;



for (var i in listOfNumbers) {
  var image = ee.Image(listOfImages.get(listOfNumbers[i]));
  var meanDictionary = image.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry:myNewFeatures.geometry()
  });

 var STDDictionary = image.reduceRegion({
  reducer: ee.Reducer.stdDev(),
  geometry:myNewFeatures.geometry()

});

// print(i,'mean',meanDictionary,'standard deviation',STDDictionary);

var std2 = ee.Number(STDDictionary.get("NDVI")).divide(2);
var mean1 = ee.Number(meanDictionary.get("NDVI"));

var negBorder=mean1.subtract(std2);
var posBorder=mean1.add(std2);

var imageNDVI=image.select('NDVI');
var gtPOS=imageNDVI.gt(posBorder).selfMask().rename('PositiveBorder');
var ltNEG=imageNDVI.lt(negBorder).selfMask().rename('NegativeBorder');
var betMEAN=imageNDVI.gt(negBorder).and(imageNDVI.lt(posBorder)).selfMask().rename('MeanBorder');

var PositiveCOL = {
  palette: [
  '006622'
  ],
};

var NegativeCOL = {
  palette: [
  'ff0000'
  ],
};

var MeanCOL = {
  palette: [
  '00ff00'
  ],
};





Map.addLayer(gtPOS,PositiveCOL,i);
Map.addLayer(ltNEG,NegativeCOL,i);
Map.addLayer(betMEAN,MeanCOL,i);



 }



  • 1
    Are you looking to add the result of your analysis to each plot, or use the result independently of the shape file. It seems you want to say "calculate the NDVI" for each plot independently. Please clarify. – Sean Roulet Aug 25 at 14:40
  • I want to have in the end different calculations for each plot. meaning, I want for example to have the mean NDVI of each plot and not the mean of all the plots together – Reut Aug 25 at 15:50
  • 1
    Understood, Add your code to the question for the calculation that you require, and then the moderator will remove the on-hold and I can show you how to do it with a feature collection. – Sean Roulet Aug 25 at 16:15
  • @SeanRoulet I have added the code though – Reut Aug 29 at 13:03
  • Is your shapefile one multipart polygon or are n registers? – aldo_tapia Aug 29 at 21:04
1

Don't have your featureCollection, but I'll assume it's called myFeatureCollection. and the imageCollection is withNDVI

var myImage = withNDVI.first()

var collectData = function(feature){
  var myGeom = feature.geometry()
  var myReduction = myImage.reduceRegion({
                  reducer: ee.Reducer.mean(),
                  geometry: myGeom,
                  scale: 30            // Change to match your need
    })
  return(ee.Feature(myGeom,{mean: myReduction}))
}


var myNewFeatures = myFeatureCollection.map(collectData) 
  • maybe one of the problems are that I don't have feature collection? should I change my uploaded shapfille (geometry) to besomehow feature collection? – Reut Aug 31 at 21:13
  • When you print(geometry) does it say in the console that it is a feature collection? – Sean Roulet Sep 1 at 0:41
  • it does- type: FeatureCollection – Reut Sep 1 at 11:50
  • Then replacing the "myFeatureCollection" in the above code with "geometry" should work fine. – Sean Roulet Sep 1 at 12:21
  • so just to make sure I understood correct, does the new variable ''myNewFeatures'' in the end is actually the splited plots?from my results it looks like it is but want to make sure I understood what you did – Reut Sep 1 at 12:31

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.