Making it easier to reuse code with Earth Engine script modules

Google Earth
Google Earth and Earth Engine
2 min readSep 28, 2017

By Andrew Chang, Software Engineer, Google Earth Engine

Starting today, you can organize your Earth Engine scripts into modules — reusable, interchangeable, and shareable components that can be included across your scripts.

Modules

A big challenge to using the Earth Engine Code Editor has been that your entire script has to fit in a single file. This can lead to very large scripts that are hard to manage and causes lots of copying and pasting between scripts if you want to repeat something that has already been done somewhere else.

To help with this problem, we’ve added a simple module loading system to help you reuse code. A “module” is just an ordinary script that’s identified some things it wants to export (see below). To use a module in your code, you use the require() function and provide the name of the module you want to load. require takes in a string representing the path to your module, such as: users/homeFolder/repo:path/to/file and returns an object that allows you to access the module’s exported functions and variables.

Creating a module is almost as easy. Anything a script wants to make available for other scripts to use just gets added to a special global exports variable. (Note: it’s always a good idea to document your functions). For example, in the users/eeblogdemos/moduleDemo repository is a file named composites with the following code in it:

Landsat 8 monthly composite animation, exported to video with Earth Engine

New Script Manager

To make it easy to share and include modules through repositories, we’ve also updated the Code Editor’s Scripts Manager. A repository is just a top level folder (linked to a git repo), but one that you can make sharable.

The biggest change you’ll see from this addition is that your “Private” collection of scripts is now found in a repository named users/username/default. You can make new repositories under your users/username prefix to make it easier to organize and share them with others. But they’re still private until you decide to share them. Shared repositories are sorted by access level so it’s easy to see who can access what.

Existing scripts you have access to but do not own will go into a separate “Archive” folder and will be migrated the next time the script owner logs in, and you’ll still be able to reach all of your scripts at https://earthengine.googlesource.com.

--

--