1

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.

3
  • Please always provide errors, and output as formatted text rather than as pictures of text.
    – PolyGeo
    Commented Jan 24, 2020 at 7:09
  • Please share a link to your feature collection. Probably, you can first map over all features and set the type of geometry using ee.Feature(feat).geometry().type(), then you can apply a filter to handle the multiPoint and multiPolygon apart.
    – Kuik
    Commented Jan 24, 2020 at 18:03
  • A related question and answer: gis.stackexchange.com/q/348362/68792 Commented Jan 25, 2020 at 1:20

1 Answer 1

2

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.