google-earth-engine

How to convert a drawn multipolygon to a list of polygon in google earth engine?


I want to know if this is possible to convert a multipolygon to a list of seperated polygons. because I drew several polygons as multipolygon in code editor and now i separate for training-testing procedure. any advice is appreciated.


Solution

  • there might be several options on the table. but if you want to handle that on the server side you can do it in this way:

    // a function which converts a multipolygon to a list of polygons
    var multipoly2polylist = function(multipoly){
        var size = multipoly.coordinates().size()
        var polylist = ee.List.sequence(0, size.add(-1), 1)
        var polylist = polylist.map(function(listelem){
            return ee.Geometry.Polygon(multipoly.coordinates().get(listelem))
        })
        return polylist
    }
    

    (if we suppose that you drew multipolygon named farm), then you can use this function for farm multipolygon object.

    var farm_polylist = multipoly2polylist(farm)
    

    I think it should work.