Requesting programming help / guidance In Google Earth Engine, to find how tall each connected patch of elevation data is in the flowing image "LabledSlopeElev" with 4 bands of information.  The first band is "labels" with unique names from "connectedComponents".  For each patch of connected Components I want to find the Max and Min value from the "elevation" band and calculate the height of the patch by subtracting the Max from the Min.  Finally, I would like to add a new band storing that height value for every pixel of the patch, such that when you click on a pixel in a patch you can see the height of the patch in the Inspector.

Here's my code so far. I'm pretty new to this style of programming.

var USNED = ee.Image('USGS/NED'); 
Map.setCenter(-76.816138, 39.315564, 16); // Daniles Maryland

// Load a table of state boundaries and filter.
var fc = ee.FeatureCollection('TIGER/2016/States')
    .filter(ee.Filter.eq('NAME', 'Maryland') );
var USNEDclip = USNED.clipToCollection(fc); //selected elevation data

var slope = ee.Terrain.slope(USNEDclip); 

// Create a "Low elevation mask" revaling all the high slope areas
var High1 = slope.gte(38.0);
Map.addLayer(High1.updateMask(High1), {palette:'fc03f4'}, 'High Elev', true); // purple is high slope areas

// Uniquely label the patches.
var patchid = High1.connectedComponents(ee.Kernel.square(1), 256);

//Concatenate the given images together, bands will be - label, slope:int, elevation, slope_1:float
var LabledSlopeElev = ee.Image.cat([patchid, USNEDclip, slope]); print('LabledSlopeElev', LabledSlopeElev);
Map.addLayer(LabledSlopeElev, {} , 'Labled Slope Elev', false);

You can use reduceConnectedComponents for this. It will create bands with a constant value in each patch. Then you simply subtract them.

var minMax = LabledSlopeElev.select("labels", "elevation")
    .reduceConnectedComponents(ee.Reducer.minMax(), "labels", 256)
var diff = minMax.select("elevation_max")
Map.addLayer(diff, {} , 'Height', false);


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.