In Earth Engine, I am trying to extract the random IDs assigned by the connectedComponents tool to the objects (or 'regions') of a LULC dataset at the locations defined by a FeatureCollection. The method is pretty simple.

  1. Get the DynamicWorld LULC dataset at a specific location and time
  2. Build a single image out of it by taking the majority (mode) of the classes for every pixel and for the entire period considered.
  3. Assign an ID to each region of connected pixels of same value.
  4. Extract the ID at the original point location (see 1).

Here is a snippet of my code:

// Load sample location and data and convert to geometry
var myptdataset = ee.FeatureCollection('users/pathtoasset')//
// Extract single point for illustration
var sample_pt = myptdataset.filter(ee.Filter.eq('Site_ID', 'VC1'));
// Define Bounds
var geometry = sample_pt.geometry().buffer(50000);

// Get collection and Build filters for DW data
var DW_FILTER = ee.Filter([
ee.Filter.date('2020-05-01', '2020-09-30'),

// Subset collections using filter
var dwCol = ee.ImageCollection('GOOGLE/DYNAMICWORLD/V1').filter(DW_FILTER);

// Build single image with majority of classes
var dwImage = dwCol.select('label').reduce(ee.Reducer.mode())
// Visualize
var label_vis = dwImage.randomVisualizer()
Map.addLayer(label_vis, null, 'DW LULC Classification');

// Assign random IDs to regions (connected px of same values)
var dwImageId = dwImage.connectedComponents({
  connectedness: ee.Kernel.plus(1),
  maxSize: 1024});
// Visualize
var label_vis = dwImageId.randomVisualizer()
 label_vis,null,'DW Random IDs');
// Retrieve and save ID at point geometry
var pt_labels = dwImageId.reduceRegions({
print(pt_labels, 'Region ID at point')

// Map point geometry

My expectation is that the ID (here 'labels') assigned to the point of the FeatureCollection should be identical than when clicking with the Inspector tool right next to it. The output is however different:

Here are the Map, inspector and console:

Resulting Map with random color assigned to IDs Inspector Output with label value next to point feature (red) Console output with label value as retrieved by reduceRegions

Is this behavior expected?

The next steps in my method would be to assign the size of the labeled regions to the corresponding location points.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy