"

3 Inputs and Outputs

(Note: A video tutorial is available at the end of this Chapter.)

Scripting requires you to create variables, utilize calls and functions.  A very simple, classic example, is to display the term Hello World.

Example 1.1.

  1. print(“Hello World”);

 

Please note, syntax is CRITICAL.  You cannot type it any other way, you can’t capitalize, you can’t add different types of brackets.  The function print(), also expects an argument in the ().  If you’re printing words, not numbers, you need to use a set of “ “ before and after the statement.  You also notice the presence of a semicolon (;) at the end of the function.  This tells the program to close the function and move on to the next task.  Go ahead and hit run to tell the computer to process the script:

GEE Interface Run Button

We can also take it a step further, and do this by creating variables.  We need to tell the computer we are creating a variable by starting a statement with var.  Var is followed by the name of the variable.  You can pick the name of the variable, but it must be one word.  You can cheat by adding an underscore (_).

For example:

Examples 1.2.

  1. var first_word = “Hello”;
  2. var second_word = “World”;
  3. print(first_word);
  4. print(second_word);

You should notice that in the print() function, we can call these variables we created.  Because they are variables, we should not use quotes “”).  It is a bit ridiculous to use two sets of print(), when we can just combine both variables within one function:

Example 1.3.

  1. print(first_word, second_word);

Hopefully this is starting to make sense.  We are going to be using many different functions, like print(), to perform a task on a variable.   Keep in mind, functions may require additional parameters, which could also be defined as variables!  Before we move on, there are other types of variables we can create.  This includes arrays:

Example 1.4.

  1. var myfavoritethings = [‘bagpipes’, ‘bouzoukis’, ‘concertinas’]

An array should contain values that are related. Again note the syntax!  Here I’m using brackets [] and values are separated by a comma.  Like before I end the statement with a semicolon (;).  Now if I want to call a value out of my array, it expects me to tell you which place the value I want sits in.  The first value listed, bagpipes, gets the 0 position; bouzoukis gets the 1 position; and concertinas take the 2 position.  Computers start their number lines as ZERO, not one.  So, if I want to print bagpipes, I would need to use the following statement:

Example 1.5.

  1. print(myfavoritethings[0]);

Now, let’s really complicate things by throwing in objects.  Don’t worry bro, nothing to sweat.  Objects combine multiple types of variables that are related.  For example:

Example 1.6.

  1. var onecooldude = {
  2.        name:’Jeff’,
  3.        age: Math.sqrt(2025),
  4.        eye_color: ‘grey’,
  5.        favoritefoods: [‘taters’, ‘steak’, ‘donuts’],
  6.        weakness: ‘copper’
  7. };

Oooooo.  Lots of stuff going on here.  We’re going to set this object up like any other variable with var then giving a name, in this case onecooldude.  So we’re creating an object because all of the values relate directly to onecooldude.  That’s who we’re talking about. After we give the object its name (onecooldude), we do = {values}.   These curly brackets ({}) are telling GEE we want to create a list of variables.   You’ll also notice we continue to give each variable its own line.  We could have smashed them all on one line, but it’s easier to read like this and better organized.  Most of the variables are straightforward:

  • name: is a text string, so we used quotes
  • eye_color: also a text string so quotes obligatory
  • favoritefoods: array.  you can tell because it contains [].  Values are strings, so quotes people!
  • weakness: also a text string, so quotes.

Let’s get to the elephant in the room.  Here, I could have just set the age variable of the object to a number, like this:

  • age: 33,

And moved forward.  But I’m not going to make it easy.  Here I make a call to the Math package, which contains a whole load of functions.  I’m going to use the square root function (sqrt).  To do this, I’m going to have to use a package.function format.  In this case, Math.sqrt().  Again, the () tells me I’m going to need an argument here, so I just provide the number 2025.  Now, instead of just telling you my age, I’m going to make the computer do a square root of a number to determine it.

  • Math.sqrt(2025),

Now to print something from this object, we need to call it by name:

Example 1.7.

  1. print(onecooldude.name);
  2. print(onecooldue.favoritefoods[1]);

In the next chapter, you’ll see how to apply these inputs to define parameters to create outputs in the console and map area of the API.

 

Below is a video tutorial designed to help you work through this chapter.

 

License

Icon for the Creative Commons Attribution-NonCommercial 4.0 International License

Zen and the Art of Scripting in Google Earth Engine Copyright © by College of Southern Idaho is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License, except where otherwise noted.