4 Controlling the Map Interface
(Note: A video tutorial is available at the end of this Chapter.)
Remember in the last chapter, you became familiar with package.function() format? You’re going to see much more of this. It’s nothing to be afraid of. Here’s an example:
Example 1.8.
- //Set the center of the map at 47.555 N, 14.890 E. Note that the longitude is first.
- Map.setCenter(14.890, 47.555);
Are you confused and scared? I told you, don’t be afraid! A couple of things here. Do you see the //? That’s a comment that I’m making to remind my brain later of what I’ve done. I don’t want the computer to think I want it to do something, so I use // to ‘remove’ it from the script. It’s not actually gone, but if the // exists ahead of a statement, the computer ignores it. If I have loads to write, like a paragraph, I can also start with /* and end with a */.
Now the second part of the statement. This makes a call from the Map library. Specifically, I want the setCenter function to center my map at a specific latitude and longitude. Without knowing much, we really don’t know where this is. But, I can throw another argument in the mix to provide a zoom. In this case a 7.5x zoom:
Example 1.9.
- Map.setCenter(14.890, 47.555, 7.5);
You can now tell that this is somewhere in one of my favorite countries, Austria. I’m going to want more details here, so I’m going to need that latitude and longitude more than once. It only makes sense to create them as variables instead of typing the numbers out repeatedly. So let’s rewrite this:
Example 1.10.
- // Set the latitude and longitude as variables
- var lat = 47.555;
- var long = 14.890;
- var zoom_lvl = 7.5;
- // Center the map at this location
- Map.setCenter(long, lat, zoom_lvl);
See my gratuitous use of comments? These come in handy! Now, let’s see exactly where this is at:
Example 1.11.
- //Set the latitude and longitude as variables
- var lat = 47.555;
- var long = 14.890;
- var zoom_lvl = 7.5;
- //Center the map at this location
- Map.setCenter(long, lat, zoom_lvl);
- //Add the lat and lon as a point.
- var point = ee.Geometry.Point(long, lat);
- Map.addLayer(point);
Did you get an error? If you did, you probably forgot to capitalize something that needed to be or vice versa. Just double check your script and run it again.
If you’re curious, that latitude and longitude represents the (rounded) geographic center of Austria. I did this using another GEE script. Don’t worry, I’m not expecting you to understand this, but to rather to demonstrate the power of the computer:
Example 1.12.
- // Load Austria boundary data from GEE’s built-in datasets
- var austria = ee.FeatureCollection(‘USDOS/LSIB_SIMPLE/2017’).filter(ee.Filter.eq(‘country_na’, ‘Austria’));
- // Calculate the centroid of Austria
- var centroid = austria.geometry().centroid();
- // Center the map on the centroid of Austria and zoom 7.5x
- Map.centerObject(centroid, 7.5);
- // Add the centroid as a point on the map
- Map.addLayer(centroid, {color: ‘red’}, ‘Centroid of Austria’);
- // Print the latitude and longitude of the centroid
- print(‘Latitude of centroid:’, centroid.coordinates().get(1));
- print(‘Longitude of centroid:’, centroid.coordinates().get(0));
Hopefully, this gets you excited and not terrified! Now let’s move on to do some exercises to work some of this into your memory.
Below is a video tutorial designed to help you work through this chapter.