Google Earth Engine – How to Check for Empty Geometries in a FeatureCollection

google-earth-enginegoogle-earth-engine-python-api

I try to export a FeatureCollection to my assets, this featurecollection is build as such:

surface_deforested_coffee = ee.Image.pixelArea().divide(10000).multiply(deforestation_total)
res_existing = surface_deforested_coffee.reduceRegions(
  collection = fc,
  reducer = ee.Reducer.sum().setOutputs(["confirmed_deforestation"]),
  scale = 50,
)

with fc being a collection of farm circles.

When I start the task in GEE:

task = ee.batch.Export.table.toAsset(
  collection = res_existing,
  description = "deforestation_on_existing_coffee",
  assetId = "users/bornToBeAlive/ldc_test/Son_La_deforestation"
)
task.start()

I get the following error:

Error: Unable to export features with empty geometry. (Error code: 3)

which surprises me bacause I manually created this feature collection by buffering Points. There should be no empty geometry.

So to debug my code I would like to know if it's possible to check for empty geometries in a ee.FeatureCollection?

Best Answer

reduceRegions just copies the input feature and adds some properties to it, so in order to have empty geometries, you had to start out with them that way. Buffering something with no geometry results in no geometry.

Unfortunately, most of the methods for detecting null properties wont work for geometries. None of these work (I filed a bug for them):

  • ee.Filter.hasType(".geo", "Geometry")
  • ee.Filter.notNull([".geo"])
  • ee.Filter.area(0, 1e12)
  • ee.Filter.eq(".geo", null)

What does seem to work is filtering to see if the geometry is contained in a global polygon (use a 6-point poly to ensure it spans the whole globe).

WORLD = ee.Geometry.Polygon([-180, 88, 0, 88, 180, 88, 180, -88, 0, -88, -180, -88])
collection.filter(ee.Filter.isContained(".geo", WORLD))

The downside is that that is an expensive test. That might take a while (or, if you have really complex geometries, it might OOM). If you know the collection only contains points, lines or Polygons, then something like this works:

  • ee.Filter.hasType(".geo", "Point")

But you'll probably miss the complex geometries like MultiPolygon, GeometryCollection, etc, so that's perilous.

https://code.earthengine.google.com/dca73d8b9d6ac3ea2ce980d3b8e6a630