[GIS] Converting Feature Collection of one element (consisting of Multipolygon geometries) into feature collection of many features

google-earth-enginegoogle-earth-engine-javascript-api

I took help from Converting Feature Collection of one element (Multi Point Geometry) into Feature Collection of different elements

I tried the code suggested in the above question but i get the following error: Error in map(ID=0):

GeometryConstructors.Polygon, argument 'coordinates': Invalid type.
Expected: List. Actual: Feature.

The feature collection has coordinates property which consists of List of geometries but its still giving the error. do i need to provide anything specific to apply the same code for Multipolygon as against Multipoint suggested in the question.

The FeatureCollection looks as follows when printed:

enter image description here

the code is as follows:

var mp = ee.FeatureCollection(result.map(function(p){
var poly = ee.Feature(ee.Geometry.Polygon(p), {})
return poly;}))

where result is my featureCollection.

Best Answer

Here is a toy example of exploding multi-geometry features in a feature collection into individual features (in a feature collection).

// Make a multi-point geometry.
var multiPoint = ee.Geometry.MultiPoint(
  [[-117, 37],
   [-118, 38],
   [-119, 39]]
);

// Make a feature collection from the geometry, add two to demonstrate mapping
// the multi-geometry explosion operation.
var fc = ee.FeatureCollection([
  ee.Feature(multiPoint),
  ee.Feature(multiPoint)
]);
print(fc);

// Make feature collection a list and map over the elements (features)
var multiGeomExplodeList = fc.toList(fc.size()).map(function(feature) {
  // Cast the input as a feature since it is coming from a list, get its
  // geometry, then split geometries into a list.
  var geomList = ee.Feature(feature).geometry().geometries();
  // Map over the geometry list.
  var featureList = geomList.map(function(geom) {
    // Return a feature representation of the geometry.
    return ee.Feature(ee.Geometry(geom));
  });
  // Return a list of all the features making up a potential multi-geometry,
  // this is a list. 
  return featureList;
})
// The result is a list of feature lists - flatten to a single list of features.
.flatten();

// Convert the list of features to a featureCollection.
var multiGeomExplodeFc = ee.FeatureCollection(multiGeomExplodeList);
print(multiGeomExplodeFc);

Code Editor script

enter image description here

Note: this solution relies on the .toList() method, which is resource intensive, so may not work for large feature collections or features that include many coordinates (large/complex/precise).

Related Question