1

I've been given the script below on Google Earth Engine to extract data along a transect. (https://code.earthengine.google.com/e31179d9e7143235092d6b4fa29a12fd) In the GEE code editor the top of the scipt has an import flag (picture attached).

Multiple references to 'line' are made, which I understand to be a variable that has been declared, but I can't find it. I've looked in the GEE documentation, and in a JavaScript reference to determine if it's a method or some such like but I can't work it out.

The imported data is declared as 'transect', so it's not that.

enter image description here

/***
 * Reduces image values along the given line string geometry using given reducer.
 * 
 * Samples image values using image native scale, or opt_scale
 */
function reduceImageProfile(image, line, reducer, scale, crs) {
  var length = line.length();
  var distances = ee.List.sequence(0, length, scale)
  var lines = line.cutLines(distances, ee.Number(scale).divide(5)).geometries();
  lines = lines.zip(distances).map(function(l) { 
    l = ee.List(l)
    
    var geom = ee.Geometry(l.get(0))
    var distance = ee.Number(l.get(1))
    
    geom = ee.Geometry.LineString(geom.coordinates())
    
    return ee.Feature(geom, {distance: distance})
  })
  lines = ee.FeatureCollection(lines)

  // reduce image for every segment
  var values = image.reduceRegions( {
    collection: ee.FeatureCollection(lines), 
    reducer: reducer, 
    scale: scale, 
    crs: crs
  })
  
  return values
}


// Define a line across the Olympic Peninsula, USA.

// Import a digital surface model and add latitude and longitude bands.
var elevImg = ee.Image('JAXA/ALOS/AW3D30/V2_2').select('AVE_DSM');

var profile = reduceImageProfile(elevImg, transect, ee.Reducer.mean(), 100)

print(ui.Chart.feature.byFeature(profile, 'distance', ['mean']))
strangecharm
  • 186
  • 11
  • `line` is passed in the arguments: `function reduceImageProfile(image, line, reducer, scale, crs)`. The variables in the argument list are automatically declared as local variables to the function. – Teemu May 02 '21 at 10:13

1 Answers1

1

line isn't a variable, it's a parameter. Parameters are very similar to local variables within the function, but instead of being declared with var, let, or const, they're declared in the function's parameter list:

function reduceImageProfile(image, line, reducer, scale, crs) {
// here −−−−−−−−−−−−−−−−−−−−−−−−−−−^

The parameter's value is filled in each time the function is called using the corresponding argument in the function call. Let's take a simpler example:

function example(a, b) {
//               ^−−^−−−−−−−−−− parameter declarations
    return a + b;
}

//                  vv−−−−−−−−− argument for `a`
console.log(example(40, 2));
//                      ^−−−−−− argument for `b`

//                  vv−−−−−−−−− argument for `a`
console.log(example(60, 7));
//                      ^−−−−−− argument for `b`

In the first call to example, the a parameter receives the value 40 and the b parameter receives the value 2 from the call arguments. In the second call, the a parameter receives the value 60 and the b parameter receives the value 7 from the call arguments.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875