filtergoogle-earth-engine

Filter FeatureCollection by Image Value in the Google Earth Engine (GEE)


I have imported a *FeatureCollection *into the Google Earth Engine (a hexagonal grid covering Europe). I want to get a subset of those grid cells which are located above land and exclude the ones which are covering water only.

Currently, I am using the Terra Land Water Mask ('MODIS/006/MOD44W') dataset to differentiate in between land and water but fail at converting it into a geometry which can be used to filter the FeatureCollection. I am open to use another dataset to differentiate in between land and water if it makes things easier.

Hence, the following questions arise:

  1. In general: How can I use the land-water information to filter my FeatureCollection?
  2. Is it necessary to convert the Land Water Mask of type ee.Image into a geometry to use it to filter the FeatureCollection or can it be used directly in the form of an ee.Image? How?
  3. If it is necessary to convert it into a geometry: which type is needed and how do I get there?
  4. Which kind of filter do I apply ot the FeatureCollection?

Below, I am posting my preliminary code including what i tried so far. The code can also be retrieved under: [https://code.earthengine.google.com/6d7bbd3c25a8f8c41f53a17aba3b04e7]

Any help is highly appreciated :)

// LOAD HEXAGONAL GRID
var grid = ee.FeatureCollection("users/livfritsche/reki_grid_epsg4326");
Map.addLayer(grid, {}, 'grid', false)
print('grid', grid)


// LOAD LAND-WATER MASK
var wm = ee.ImageCollection('MODIS/006/MOD44W')
                  .filter(ee.Filter.date('2015-01-01', '2015-05-01'));
var waterMask = wm.first().select('water_mask');
// compute and plot land mask
var landMask = waterMask.eq(0);
Map.addLayer(landMask, {}, 'Land Mask', false)


// FILTER HEXAGONAL GRID CELLS ACCORDING TO LANDCOVER

// try and fail: convert land mask to polygon, then apply polygon to filter grid

// land mask to polygon
var landMaskGeom = landMask.reduceToVectors({
  scale: 10000,
  geometry: grid,
  geometryType: 'polygon'
});
print('land mask geometry', landMaskGeom)
Map.addLayer(landMaskGeom, {}, 'land mask geometry')

var grid_land = grid.filterBounds(landMaskGeom)
print('grid_land', grid_land)
Map.addLayer(grid_land, {}, 'grid_land')

// output: grid_land still contains all the features from the original FeatureCollection, there was no filtering applied?
// Is 'filterBounds' the right function to use? What shall I use instead?


Solution

  • Found the issue! I will leave the question online in case it helps somebody else.

    The filtering of the FeatureCollection landMaskGeom was missing. Only the Features with the property label == 1 shall be used.

    var landMaskGeomFilt = landMaskGeom.filter(ee.Filter.eq('label', 1))