toPolygons#

geetools.ee_feature_collection.FeatureCollectionAccessor.toPolygons()#

Drop any geometry that is not a Polygon or a multipolygon.

This method is made to avoid errors when performing zonal statistics and/or other surfaces operations. These operations won’t work on geometries that are Lines or points. The methods remove these geometry types from GeometryCollections and remove features that don’t have any polygon geometry.

Returns:

The parsed collection with only polygon/MultiPolygon geometries.

Return type:

ee.FeatureCollection

Example

import ee, geetools
from geetools.utils import initialize_documentation
from matplotlib import pyplot as plt

initialize_documentation()

point0 = ee.Geometry.Point([0,0], proj="EPSG:4326")
point1 = ee.Geometry.Point([0,1], proj="EPSG:4326")
poly0 = point0.buffer(1, proj="EPSG:4326")
poly1 = point1.buffer(1, proj="EPSG:4326").bounds(proj="EPSG:4326")
line = ee.Geometry.LineString([point1, point0], proj="EPSG:4326")
multiPoly = ee.Geometry.MultiPolygon([poly0, poly1], proj="EPSG:4326")
geometryCol = ee.Algorithms.GeometryConstructors.MultiGeometry([multiPoly, poly0, poly1, point0, line], crs="EPSG:4326", geodesic=True, maxError=1)

fc = ee.FeatureCollection([geometryCol])
fc = fc.geetools.toPolygons()

fig, ax = plt.subplots(figsize=(5, 10))
fc.geetools.plot(boundaries=True, ax=ax)