3

I came to the issue of Out of Memory Error when I tried to do a supervised classification of an image using a shapefile polygons as underlying featureCollection in GEE. It turned out that some of the polygons that I used for sampling the underlying pixels are due to some reason not of "polygon" type but of "GeometryCollection" type (mostly combination of polygon and LineString). I wonder if it is possible to correct these non-polygons and correct their geometry type into "polygon" without producing other side effects?

enter image description here

Please see the attached image of one 'bad' geometry example. This should be a "Polygon" but it has a sliver in it which turned the polygon into "GeometryCollection" consisting of a 'Polygon' + 'LineString'. Unfortunately about one-third of my featureCollection is populated with such geometries! I need to somehow get rid of these artifacts.

1
  • The question is really invariant to the code. It is rather related to optimal handling of a featureCollection in GEE. Just imagine that I have a featureCollection with 2 features. The geometry of feature_1 is of type "Polygon" and the geometry of feature_2 is of type "GeometryCollection". The question is that, is it possible to turn the GeometryCollection into a Polygon or not? Commented Feb 1 at 14:13

1 Answer 1

3

You can get the polygon within the GeometryCollection using (if there is one):

var list = geometryCollection.geometries().filter(ee.Filter.hasType('item','Polygon'))
var polygon = ee.Geometry.Polygon(ee.Geometry(list.get(0)).coordinates())

or a multi polygon if the collection has multiple polygons:

var list = geometryCollection.geometries().filter(ee.Filter.hasType('item','Polygon'))
var multiPolygon = ee.Geometry.MultiPolygon(list)
1
  • Thanks @Kadir Şahbaz. Yes "Filter.hasType" returns the right geometry (in my case Polygon) . I need to take one more step and replace this geometry (polygon) with the old one (GeometryCollection). I think I need to use Feature.setGeometry(). however this gives me an error ! Commented Feb 3 at 16:01

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.