0

I'm using Google Earth Engine and I know how to import and export imges giving on a coordinates from a shapefile, but let's say I have a shapefile with different regions where each region has its own coordinates as polygons. I need to know how can I get image collection for each coordinates rather than one giant image that covers all the coordinates in the shapefile.

I thought about looping through the coordinates in the shapefile and take each coordinates in each row, but had some issues there too. I have been searching for this for a couple of days now and nothing is quite getting me there.

Edit Okay, This is what I made so far.

var S2 = ee.ImageCollection("COPERNICUS/S2"),
boundary = ee.FeatureCollection("users/rashagh22/mygeodata");
var vis_params = {bands:["B4", "B3", "B2"], max:2000};
var start_date = '2017-01-01';
var end_date = '2018-01-01';

//Extract the coordinates from the feature collcetion
var multi_polygons =  ee.Geometry(boundary.geometry());
var geometries = multi_polygons.geometries();

print(geometries.get(0));


//Function that takes roi and returns image collection
var image_coll = function(s2, roi, start_date, end_date) {
  return s2.filterDate(start_date, end_date).filterBounds(roi)
  .filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'less_than', 1);
};
print(geometries.size())

var addImageCollections = function(feature) {
  return feature.set({images: S2.filterDate(start_date, end_date).filterBounds(feature.geometry())
  .filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'less_than', 1)});
};

var imagesAdded = boundary.map(addImageCollections);

print(imagesAdded);

Noe I have a feature collection with image collection as one of its columns. But not sure how to access them now

5
  • Have you tried creating a function that creates an image collection, and then mapping this function to your feature collection? Aug 18 '20 at 13:49
  • Hmm never occured to me but I'll certaily try it. I have just started working with gee a week a go, but it seems reasonable. thank you, I'll try it and let you know :)
    – salRad
    Aug 18 '20 at 14:02
  • Please Edit the Question to help us help you by specifying what you tried, and how it didn't work.
    – Vince
    Aug 18 '20 at 23:43
  • You seem to have coding well in hand. I started writing a doc about how to use Earth engine (specially the client/server particularities) docs.google.com/document/d/… Aug 19 '20 at 7:13
  • @SeanRoulet thank you. This looks really usefull!
    – salRad
    Aug 19 '20 at 7:56
0

Earth Engine has very powerful capabilities that make doing what you need to do very straight forward. I don't have access to your FeatureCollection, so I can't give you a complete working solution as I can't test it. But this is how I would do it.

var S2 = ee.ImageCollection("COPERNICUS/S2");
var boundary = ee.FeatureCollection("users/rashagh22/mygeodata");
var vis_params = {bands:["B4", "B3", "B2"], max:2000};
var start_date = '2017-01-01';
var end_date = '2018-01-01';
    
//This function will take a feature and use the geometry to filter the S2
// image Collection and return an imageCollection
var get_Image_Coll = function(feature){
  var myImageCollection = S2.filterDate(start_date,end_date)
                  .filterBounds(feature.geometry())
                  .filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'less_than', 1);
  
  return ee.ImageCollection(myImageCollection);  
}

// here we "map" the function to each feature in the collection.
// we "flatten" it so we don't have a collection of collections.
var myImages = ee.ImageCollection(boundary.map(get_Image_Coll)).flatten();

In Earth Engine, we don't loop. Earth Engine takes our function and farms it out to its servers independently. Giving a result very quickly depending on availability.

Try this out in your script to see if you can get it working.

To export, you will have to use the normal looping from Javascript, as it is a client process.

3
  • Thanks a lot I edited my code just a second before reading your answer. Your code seems to give me exactly what I need. Thanks!!
    – salRad
    Aug 19 '20 at 6:58
  • Just one more thing. I'm still not sure how to get each image collection. And by the way I'm having this error when implementing your solution FeatureCollection (Error) Collection query aborted after accumulating over 5000 elements.
    – salRad
    Aug 19 '20 at 7:47
  • Ah, yes. This is a limit so that things don't take too long. You have too many points. Limits are higher if you export. (or you can limit the print with .limit(10) if you want to see what comes out.) Aug 19 '20 at 9:01
0

So I know I already accepted an answer by @Sean Roulet which was a great help, but I end up implementing the following code, because the previous was takeing `feature.geometry()) which also menat taking the whole boundary rather than each polygon in the table.

var S2 = ee.ImageCollection("COPERNICUS/S2"),
    boundary = ee.FeatureCollection("users/rashagh22/mygeodata"),
    table = ee.FeatureCollection("users/rashagh22/ethiopia");

var vis_params = {bands:["B4", "B3", "B2"], max:2000, color: 'green'};
var start_date = '2017-01-01';
var end_date = '2018-01-01';

//Extract the coordinates from the feature collcetion
var multi_polygons =  ee.Geometry(table.geometry());
var geometries = multi_polygons.geometries();

print(geometries);

// map over the list
var image_list = (function(x) { 
  // you could filter on the property 'system:time_start' using filterDate()
  var images = S2.filterBounds(x)
  .filterDate(start_date, end_date)
  .filterMetadata("CLOUD_COVERAGE_ASSESSMENT", "less_than", 1).median();
  images = images.clip(x)
  return images;
});

var images = geometries.map(image_list);
print(images)

var collection = ee.ImageCollection(images);
print(collection.first());

Map.centerObject(table, 20);
Map.addLayer(collection,  vis_params, 'sample_1');

So what the previous code does basically is taking the coordinates and storing them in geometries then I created list of images for each of these polygons. I clip the images and now we have image collection for each area of interest from the geojson file.

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.