-1

I have a list of features containing two Polygons that belong to a MultiPolygon feature:

var polygon_lst = ee.Feature(MP_feat).geometry().coordinates(); //MP_feat is a MultiPolygon

I want to take every element of this list(two polygons), calculate their corresponding polygon.area() and return the index of the element whose polygon has the maximum area.

I think I need to use reducers at some point but I am not sure how. Any comment on this?

1 Answer 1

1

Here's how I would approach this problem.

  1. Get a list of geometries from the MultiPolygon. This will be easier to deal with than a list of lists of coordinates.

    var polygon_list = MP_feat.geometries();
    
  2. Get a corresponding list with the area of each polygon using a map. Note that you need to specify a maxError when calculating area.

    var area_list = polygon_list.map(function(f) {return ee.Geometry(f).area(1)});
    
  3. Calculate the largest area in the list using a max reducer.

    var largest_area = areas.reduce(ee.Reducer.max());
    
  4. Get the index of the largest area identified above. Because the area list corresponds to the polygon list, this is also the index of the largest polygon.

    var largest_index = area_list.indexOf(largest_area);
    
  5. Optionally, get the largest polygon using the index.

    var largest_polygon = ee.Geometry(polygon_list.get(largest_index));
    

Here's a script to demonstrate with some dummy data: https://code.earthengine.google.com/2100c0f01bf74e5a101b1f162de5cf58.

2
  • Thanks Aaron. Correct and very informative for me as a GEE beginner :) Commented Feb 5, 2024 at 9:38
  • Indeed that one more step (.coordinates()) adds to the complexity rather than simplifying the procedure. Commented Feb 5, 2024 at 9:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.