Skip to content

Class 9

Understanding Client vs. Server in Google Earth Engine (GEE)

GEE employs a client-server architecture, where your local machine (client) interacts with Google's powerful servers for geospatial analysis. This distinction is essential for crafting efficient and effective GEE scripts.

Client-Side (Local) Functions

  • Reside in your web browser or development environment.
  • Focus on tasks that don't require heavy processing or access to Earth Engine's geospatial capabilities.
  • Common examples:
    • Debugging: Use print() to inspect variable values during script development:
      var myValue = 10;
      print(myValue); // Outputs: 10 (seen in the console)
      
    • Visualization: Employ Map() to display results on the Earth Engine map interface for visual exploration:
      var myImage = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140408');
      Map.setCenter(-122.33, 37.86, 10); // Set map center
      Map.addLayer(myImage); // Add image to map for viewing
      
    • User Interaction: Create client-side functions to accept user input for filtering, calculations, or modifying map displays:
      var myCollection = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED");
      var userSelectedYear = parseInt(prompt("Enter a year (e.g., 2020):"));
      var filteredCollection = myCollection.filter(ee.Filter.eq('year', userSelectedYear));
      // ... use filteredCollection further in your script
      

Server-Side (Remote) Functions

  • Execute on Google's high-performance servers specifically designed for geospatial processing of large datasets.
  • Examples encompassing essential GEE operations:
    • Data Loading: Use ee.Image() and ee.FeatureCollection() to access and manipulate imagery and feature datasets:
      var landsatImage = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140408');
      var forestFeatures = ee.FeatureCollection('FAO/FRA/2015/global_forest_extent');
      
    • Image Processing: Utilize methods like select(), normalizedDifference(), clip(), mask(), and various mathematical operators for image analysis:
      var ndvi = landsatImage.normalizedDifference(['nir', 'red']);
      var clippedNDVI = ndvi.clip(forestFeatures); // Clip NDVI to forest area
      
    • Feature Analysis: Leverage methods like filter(), map(), and reduction operations (sum(), mean()) to analyze feature collections:
      var filteredForests = forestFeatures.filter(ee.Filter.gte('tree_cover', 30)); // Filter for high tree cover
      var forestArea = filteredForests.reduceAreas().get('areas'); // Calculate total forest area
      

Earth Engine Proxy Objects and Data Locality

It's crucial to distinguish Earth Engine objects from other JavaScript objects or primitives in your code. You can't directly manipulate data on the server. Instead, you interact with server-side objects through client-side "proxy" objects in your script. These proxy objects, recognizable by anything starting with ee. , act as handles for the actual data residing on Google's servers. They don't contain the data themselves but facilitate communication between your local environment and the server.

Key Considerations and Best Practices

  • Prioritize Server-Side: Focus on using server-side functions (starting with ee.) for the core of your geospatial analysis tasks, leveraging Google's computational power.
  • Client-Side for Presentation and Interaction: Keep client-side functions for visualizing results, debugging, handling user input, and refining map displays.
  • Clear Separation: Structure your code with a clear distinction between client-side and server-side operations to enhance readability and maintainability. Use comments to explain specific sections of your code.
  • Efficiency: When possible, minimize client-side processing by pre-computing results server-side and transmitting only necessary data to the client for visualization.
  • Avoid Mixing: Keep client-side and server-side logic separate whenever feasible to prevent unintended consequences or performance issues.

Working with Server-Side Objects in Google Earth Engine (GEE)

Google Earth Engine (GEE) utilizes a functional programming approach where operations are performed using methods associated with each server-side object. This structure promotes clear and efficient manipulation of geospatial data. Here's a breakdown of essential server-side objects and their operations:

1. ee.Number(): Performing Arithmetic and Logical Operations

The ee.Number() object represents numerical values in GEE. It offers methods for performing arithmetic and logical operations on these values.

  • Arithmetic Operations:

    • add(): Addition (e.g., num1.add(num2))
    • subtract(): Subtraction (e.g., num1.subtract(num2))
    • multiply(): Multiplication (e.g., num1.multiply(num2))
    • divide(): Division (e.g., num1.divide(num2))
  • Logical Operations (Comparisons):

    • Logical comparisons are performed using methods like gt(), lt(), eq(), etc., within ee.Comparison. These methods evaluate to true or false and are crucial for conditional statements and filtering operations.
      • gt(): Greater than (e.g., num1.gt(num2))
      • lt(): Less than (e.g., num1.lt(num2))
      • eq(): Equal to (e.g., num1.eq(num2))

Example:

// Define ee.Number objects
var num1 = ee.Number(5);
var num2 = ee.Number(3);

// Arithmetic operations
var sum = num1.add(num2);
var difference = num1.subtract(num2);

// Logical operations
var isGreaterThan = num1.gt(num2); // True
var isEqual = num1.eq(num2); // False

print("Arithmetic Operations:");
print("Sum:", sum);
print("Difference:", difference);

print("\nLogical Operations:");
print("Is num1 greater than num2?", isGreaterThan);
print("Are num1 and num2 equal?", isEqual);

2. ee.List(): Managing Ordered Collections

An ee.List object represents an ordered collection of elements of any data type in GEE. It's a versatile structure for storing and manipulating various kinds of information within your scripts.

  • Common Operations:
    • ee.List([element1, element2, ...]): Create a list.
    • get(index): Access elements by their position (0-based indexing).
    • size(): Get the number of elements in the list.
    • add(element): Add an element to the end of the list.
    • Additional methods: Explore methods for filtering, mapping, and iterating over lists.

Example:

// Define an ee.List
var myList = ee.List([10, "apple", ee.Number(3.14)]);

// Accessing elements
var firstElement = myList.get(0); // 10
var secondElement = myList.get(1); // "apple"

// Length of the list
var listLength = myList.size(); // 3

print("List Operations:");
print("First element:", firstElement);
print("List length:", listLength);

3. ee.Dictionary(): Storing Key-Value Pairs

An ee.Dictionary object represents a collection of key-value pairs in GEE. Keys must be strings, while values can be any data type. It's useful for storing configuration options or associating metadata with features.

  • Common Operations:
    • ee.Dictionary({key1: value1, key2: value2, ...}): Create a dictionary.
    • get(key): Access a value by its key.
    • contains(key): Check if a key exists in the dictionary.
    • set(key, value): Add or update a key-value pair.
    • remove(key): Remove a key-value pair.

Example:

// Define an ee.Dictionary
var myDict = ee.Dictionary({
  "name": "My Image",
  "resolution": 30,
  "bands": ee.List(["red", "green", "blue"])
});

// Accessing values
var imageName = myDict.get("name"); // Access value by key: "My Image"

// Checking if a key exists
var containsResolution = myDict.contains("resolution"); // true

// Adding a new key-value pair
myDict = myDict.set("year", 2022);

// Removing a key-value pair
myDict = myDict.remove(["bands"]);

// Display the dictionary
print("Dictionary:", myDict);

Assignment on Class 9

{
  "name":"Your_name",
  "hobby":"reading",
  "numList":[2,3,4,5,6,7,9]
}
Convert above key-value pair into a Dictionary object. Get the numList from the dictionary. Apply reducer to the list and print the sum of the numList. Upload the GEE code link in your github repo. Submit your repo link. -3

[4,5,7,8,,9] Convert the list into a GEE List object. Apply the map method and multiply the value of each element of the list by 4. Finally print the multiplied list. Upload the GEE code link in your github repo. Submit your repo link. -2

Upload upazila boundary shape file of Bangladesh to GEE asset tab. Filter out your desired upazila, and print the area, and perimeter of your upazila. Finally, show your upazila in the map. Take a screenshot of the map. Upload the screenshot, GEE code link in your github repo. Submit your repo link. -5

Submision link: Check your email

Dead line: 17 March, 2024