0

I tried the following code in the Google Earth Engine through the various countries which have different dry season. But I cannot understand how to iterate the code. can anyone generate iteration using this code over any country of the world?

//clip boundary for each and every collection
var clipBoundary = function(image)
                        {
                          return image.clip(countries.filterMetadata("ADM0_NAME","equals","India"))
                        }
//create a time band for linear regression
var createTimeBand = function(image) {
   // in the linear regression output.
  return image.addBands(image.metadata('system:time_start'));
};

 var startdate = ee.Date('2017-12-01');
  var enddate = ee.Date('2018-05-31');
var collection = ee.ImageCollection('MODIS/006/MOD13Q1').select('NDVI')
                        .filterDate(startdate,enddate)
                        .map(clipBoundary)
                        .map(createTimeBand)
                        .select(['system:time_start', 'NDVI'])
                        .reduce(ee.Reducer.linearFit())
                        .select('scale');
 print(collection)
  Map.centerObject(collection, 6)
 Map.addLayer(collection)
  • what program/system are you using? – Ian Turton May 17 at 8:31
0

I guess you want to iterate over all the countries? As you asset countries was not shared/accessible, I made the countries from the asset "USDOS/LSIB_SIMPLE/2017".

You can first get all the country names using:

// get a list of country names. Use "ADM0_NAME" in your case
var countryNames = ee.List(countries.aggregate_array('country_na'));

You can then map over those country names and get the scale from the reducer output using:

// map over the countries to get a image collection per country
var mappedCountries = ee.ImageCollection.fromImages(countryNames.map(function(country){
  country = ee.String(country);
  // clip the colleciton to the country
  var clippedCollection = collection.map(function(image){
    return clipBoundary(image, country);
  });
  // reduce image collection and get scale
  var scale = clippedCollection.reduce(ee.Reducer.linearFit())
                      .select('scale');
  return scale.set('country', country);
}));

Note I made the function you defined function instead of variables so you can put them at the bottom of the script. Also note I prefiltered the collection on you start and end date. See the link

  • I'll go through the code and check this match for my requirement – pavi May 17 at 9:09
  • Can I use the code, again and again, to filter more countries simultaneously? – pavi May 17 at 10:02
  • I mean countries which have different dry seasons – pavi May 17 at 10:02
  • The output is one image with scale paramater from the reduced collection of all the countries inside the table "countries" – Kuik May 17 at 11:48
  • can we filter another country without executing the same code at a different time? – pavi May 21 at 1:51

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.