"

5 Part I Exercises

Alright, now that you’ve gained a mastery in the basics of JavaScripting in GEE, let’s do some practice to see how well you can recall what you’ve learned.  After completing this section of the book, you should be able to:

  • Use the print() function, including its use with variables.
  • Create a the following types of variables:
    • Variables with text.
    • Variables with numbers.
    • Variables as an array.
    • Variables as an object.
  • Center the map using the Map.setCenter() function.
  • Create a point at a given longitude, latitude and display it using the following functions: ee.Geometry.Point() and Map.addLayer().
  • Identify a library and how to call a function from a library.
  • Create comments in a script.

If any of the above look unfamiliar, go back and review the appropriate chapters in Part I. Remember, practice makes permanent.  So let’s do some exercises to help you practice what you’ve learned so far.

Exercise 1.1.

For each of the problems below, create a separate script.

  1. In the console, print “Sensei Jeff will teach me confidence in JavaScripting.”
  2. Create a variable with the same text statement as no. 1.  Have it print the variable in the console.
  3. Create a variable meaning_of_life equal to the number 42.  Print the variable in the console.
  4. Create an array of variables called united_kingdom equal to England, Scotland, Wales, and Northern Ireland, in that order.  Have it print the second and third variables in the array.
  5. Create an object called Scotland.  Have it include the following components:
    1. a variable called “name” equal to Scotland
    2. a variable called “size_km2” equal to 77910
    3. an array called “best_known_for” equal to haggis, bagpipes, and kilts.
  6. Have it print a statement, in the console, “Scotland is 77910 square kilometers and is best known for haggis, bagpipes, and kilts.” using variables when possible (*see hint below).
  7. Create a point 3.1883° W and 55.9533° N called Edinburgh.  Add the point to the map (**see hint below).
  8. Add comments to the your script for #7.

The hints below are designed to give you a hand if you need it:

*Hint (Print): Although we don’t use print very often to do analyses in GEE, it can be useful in debugging routines. If you want to print multiple things on multiple lines, try using print(variable1 + variable2).  Realize that if you want a space, you’re going to have to ask for it, and it’s going to look something like this: print(variable1 + “ “+ variable2).  You can also add a string in there like this: print(variable1 + “ is not the same as “ + variable2).  Note the extra spacing in the string, otherwise your words will be jumbled together.  There’s no doubt in this example, it would be WAY easier to just type it out, but it makes for a good exercise.

Here’s the key: print(Scotland.name+” is “+Scotland.size_km2+” square kilometers and is best known for “+Scotland.best_known_for[0]+”, “+Scotland.best_known_for[1]+”, and “+Scotland.best_known_for[2]);.  Don’t be afraid to put things on multiple lines.

**Hint(Finding your point): Previously, you learned Map.setCenter, which allowed you to center on a point you defined.  While that’s all well and good, it’s often more efficient to just go directly to the variable you’ve created. In this case, try using Map.centerObject.  For this example Map.centerObject(Edinburgh) should do the trick, provided you’ve defined the point ‘Edinburgh’ correctly.  Don’t like the level of zoom? Good news, you can manually set that sucker by just passing it as another part of Map.centerObject.  Try a level 7 zoom: Map.centerObject(Edinburgh, 7).

 

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.