keepType#

geetools.ee_geometry.GeometryAccessor.keepType(type, exclude=False)#

Only keep the geometries of the given type from a GeometryCollection.

Parameters:
  • type (str | tuple[str, ...]) – The type or types of geometries to keep. Can be one or more of: Point, LineString, Polygon.

  • exclude (bool) – If True, remove the given geometry types instead of keeping them.

Returns:

The geometries matching the selected types.

Return type:

ee.Geometry

Examples

import ee, geetools
from geetools.utils import initialize_documentation

initialize_documentation()

# generate multiple geometries of different types
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")

# create a geometry collection from them
geometryCollection = ee.Algorithms.GeometryConstructors.MultiGeometry(
    [multiPoly, poly0, poly1, point0, line],
    crs="EPSG:4326",
    geodesic=True,
    maxError=1
)

# extract only the LineString geometries from the collection
geom = geometryCollection.geetools.keepType('LineString')
geom.getInfo()

# extract the Point and LineString geometries from the collection
geom = geometryCollection.geetools.keepType(('Point', 'LineString'))
geom.getInfo()
{'type': 'GeometryCollection',
 'geometries': [{'type': 'Point', 'coordinates': [0, 0]},
  {'type': 'LineString', 'coordinates': [[0, 1], [0, 0]]}]}