Skip to content

Commit

Permalink
[API Samples] Add example for ee.Number.expression JS API
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 383886116
  • Loading branch information
jdbcode authored and copybara-github committed Jul 9, 2021
1 parent f8a3a39 commit 40710a3
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions samples/javascript/apidocs/ee-number-expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright 2021 The Google Earth Engine Community Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START earthengine__apidocs__ee_number_expression]
// A dictionary of variables to use in expression.
var variables = {x: 5, y: 10};

// Arithmetic operators.
print('x + y',
ee.Number.expression('x + y', variables));
print('x - y',
ee.Number.expression('x - y', variables));
print('x * y',
ee.Number.expression('x * y', variables));
print('x / y',
ee.Number.expression('x / y', variables));
print('x ** y',
ee.Number.expression('x ** y', variables));
print('x % y',
ee.Number.expression('x % y', variables));

// Logical operators.
print('x || y',
ee.Number.expression('x || y', variables));
print('x && y',
ee.Number.expression('x && y', variables));
print('!(x)',
ee.Number.expression('!(x)', variables));

// Relational operators.
print('x > y',
ee.Number.expression('x > y', variables));
print('x >= y',
ee.Number.expression('x >= y', variables));
print('x < y',
ee.Number.expression('x < y', variables));
print('x <= y',
ee.Number.expression('x <= y', variables));
print('x == y',
ee.Number.expression('x == y', variables));
print('x != y',
ee.Number.expression('x != y', variables));

// Conditional (ternary) operator.
print('(x < y) ? 100 : 1000)',
ee.Number.expression('(x < y) ? 100 : 1000', variables));

// Constants in the expression.
print('100 * (x + y)',
ee.Number.expression('100 * (x + y)', variables));

// JavaScript Math constants.
print('Math.PI',
ee.Number.expression('Math.PI', null));
print('Math.E',
ee.Number.expression('Math.E', null));

// Dot notation to call on child elements.
print('Use dot notation to call on child elements',
ee.Number.expression('vals.x * vals.y', {vals: variables}));

// ee.Number functions.
print('Use ee.Number add: add(x, y)',
ee.Number.expression('add(x, y)', variables));
print('Use ee.Number add and subtract: subtract(add(x, y), 5)',
ee.Number.expression('subtract(add(x, y), 5)', variables));
// [END earthengine__apidocs__ee_number_expression]

0 comments on commit 40710a3

Please sign in to comment.