Visualizing Hillshades in Google Earth Engine

SHARE:

Learn how to generate dynamic hillshades in Google Earth Engine to visualize terrain with sun angle variations using SRTM DEM.

Visualizing terrain is a fundamental aspect of geographic data analysis. A hillshade is a 2D representation of a 3D surface, illuminated by a light source, creating highlights and shadows that reveal topographic features. This article will guide you through a Google Earth Engine (GEE) script that computes hillshades from a digital elevation model (DEM), demonstrating how to enhance terrain visualization for better understanding and interpretation.

Hillshades Script in Google Earth Engine


What This Script Does

This Google Earth Engine script calculates and displays multiple hillshade layers from the SRTM DEM (Digital Elevation Model) for a specific location. The script defines functions to convert degrees to radians and to compute the hillshade itself, given sun azimuth and zenith angles. It then calculates slope and aspect from the DEM, which are required inputs for the hillshade calculation. Finally, it uses a client-side for loop to generate hillshades with different sun azimuth angles and adds each resulting hillshade as a separate layer to the map. This allows for a dynamic visualization of the terrain as if it were illuminated from different directions.

Google Earth Engine (GEE) Code for Hillshade


// Define a function to convert from degrees to radians.
function radians(img) {
  return img.toFloat().multiply(Math.PI).divide(180);
}
// Define a function to compute a hillshade from terrain data
// for the given sun azimuth and elevation.
function hillshade(az, ze, slope, aspect) {
  // Convert angles to radians.
  var azimuth = radians(ee.Image(az));
  var zenith = radians(ee.Image(ze));
  // Note that methods on images are needed to do the computation.
  // i.e. JavaScript operators (e.g. +, -, /, *) do not work on images.
  // The following implements:
  // Hillshade = cos(Azimuth - Aspect) * sin(Slope) * sin(Zenith) +
  //    cos(Zenith) * cos(Slope)
  return azimuth.subtract(aspect).cos()
    .multiply(slope.sin())
    .multiply(zenith.sin())
    .add(
      zenith.cos().multiply(slope.cos()));
}
// Compute terrain measures from the SRTM DEM.
var terrain = ee.Algorithms.Terrain(ee.Image('CGIAR/SRTM90_V4'));
var slope = radians(terrain.select('slope'));
var aspect = radians(terrain.select('aspect'));
// For loops are needed for control-flow operations on client-side
// operations.  Here Map.addLayer() is a client operation that needs
// to be performed in a for loop.  In general, avoid for loops
// for any server-side operation.
Map.setCenter(-121.767, 46.852, 11);
for (var i = 0; i < 360; i += 60) {
  Map.addLayer(hillshade(i, 60, slope, aspect), {}, i + ' deg');
}

Step-by-Step Explanation:

Here's a breakdown of the code:

  1. Define the radians function:

    The radians function takes an image as input, converts it to floating-point data type, and then converts the pixel values from degrees to radians using the formula: radians = degrees * π / 180.

    This function is necessary because the trigonometric functions in the hillshade calculation require angles to be in radians.

  2. Define the hillshade function:

    The hillshade function calculates the hillshade value for each pixel, given the sun's azimuth (horizontal angle) and zenith (vertical angle), and the terrain's slope and aspect.

    It first converts the azimuth and zenith angles to radians using the radians function.

    It then applies the hillshade formula:

    Hillshade = cos(Azimuth - Aspect) * sin(Slope) * sin(Zenith) + cos(Zenith) * cos(Slope)

    This formula calculates the intensity of illumination for each pixel based on the angles of the sun and the orientation of the terrain.

  3. Compute terrain measures:

    var terrain = ee.Algorithms.Terrain(ee.Image('CGIAR/SRTM90_V4')); loads the SRTM 90m Digital Elevation Model and calculates terrain attributes (slope, aspect) using ee.Algorithms.Terrain.

    var slope = radians(terrain.select('slope')); extracts the slope band from the terrain data and converts it to radians using the radians function. Slope represents the steepness of the terrain.

    var aspect = radians(terrain.select('aspect')); extracts the aspect band from the terrain data and converts it to radians. Aspect represents the direction of the steepest slope.

  4. Display hillshades with varying azimuth:

    Map.setCenter(-121.767, 46.852, 11); centers the map view on Mount Adams, Washington, USA.

    The for loop iterates through azimuth angles from 0 to 300 degrees in 60-degree increments.

    Inside the loop:

    Map.addLayer(hillshade(i, 60, slope, aspect), {}, i + ' deg'); calculates the hillshade for the current azimuth angle i (with a constant zenith angle of 60 degrees) and adds it as a layer to the map.

    The third argument to Map.addLayer is an empty object {} for visualization parameters (using default visualization).

    The fourth argument, i + ' deg', is the name of the layer, indicating the azimuth angle.

Datasets Used

SRTM 90m Digital Elevation Model: (CGIAR/SRTM90_V4)

This dataset provides elevation data used to derive the slope and aspect information needed for the hillshade calculation.

Applications

Hillshade visualizations are used in a variety of applications, including:

  • Terrain Visualization: Enhancing the visual representation of topography for better understanding of landforms.
  • Geologic Mapping: Identifying geological features such as faults, folds, and volcanic structures.
  • Hydrological Modeling: Analyzing drainage patterns, identifying watersheds, and mapping stream networks.
  • Forestry Management: Mapping forest canopy structure and identifying areas of varying slope and aspect, which can influence tree growth.
  • Land Use Planning: Assessing the suitability of land for different uses based on terrain characteristics.
  • Archaeological Studies: Visualizing subtle topographic features that may indicate the presence of archaeological sites.
  • Cartography: Creating visually appealing and informative maps.

Visualization Example in Google Earth Engine

The script will display multiple hillshade layers in Google Earth Engine, each representing the terrain illuminated from a different direction (azimuth angle). By toggling the layers on and off, you can interactively explore how the appearance of the terrain changes with varying sun angles. This provides a more comprehensive understanding of the topography than a single hillshade image.

Visualization of Hillshade created using JS script in GEE

Visualization of Hillshade created using JS script in Google Earth Engine GEE


Notes

  • The ee.Algorithms.Terrain function efficiently calculates slope and aspect from the DEM.
  • The for loop is used here for a client-side operation (Map.addLayer). For server-side operations on the image data itself, it's crucial to use GEE's functional programming methods (e.g., map(), iterate()) instead of JavaScript loops.
  • The zenith angle (60 degrees) is held constant in this example, but you can experiment with different zenith angles to simulate different sun elevations.
  • The hillshade calculation enhances the visualization of terrain, but the underlying elevation data remains unchanged.


Prepared by: Jamal Chaaouan | GEE Academy @ GeoJamal.com

COMMENTS

Name

CHIRPS,1,Climate,1,Cloud masking,1,DEM,1,EVI,3,Export,2,Fire forest,1,Forest,1,GEE,1,GEE Academy,3,GEE script,1,Image analysis,6,JRC,1,Landsat,1,LST,1,MODIS,4,NDVI,5,NOAA,1,remote sensing,2,Sentinel,2,Sentinel-2,1,Snow,1,SRTM,3,temperature,1,time series,1,vegetation index,3,VIIRS,1,
ltr
item
GEE Academy: Visualizing Hillshades in Google Earth Engine
Visualizing Hillshades in Google Earth Engine
Learn how to generate dynamic hillshades in Google Earth Engine to visualize terrain with sun angle variations using SRTM DEM.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRL6O7XWVs8pak8o3EoJV8wPw6iI88IdNpOAcIsyWKkezhyphenhyphenhSLG79MEoyeTf4BJONozoKiFtdOxGzGg-D35Tnj6nGEdf4i9pDRnLporjD3gNGNYMV28TcBLVvRgZo-fIaBDtw2WgABtvCef7xX27Y5xaOkz45IpWmN120RMiGFSnKPsrpPOPnmPX6Dnw0/w640-h464/HILLSHADE-SCRIPT-GEOJAMAL-GOOGLEEARTHENGINE-GEE.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRL6O7XWVs8pak8o3EoJV8wPw6iI88IdNpOAcIsyWKkezhyphenhyphenhSLG79MEoyeTf4BJONozoKiFtdOxGzGg-D35Tnj6nGEdf4i9pDRnLporjD3gNGNYMV28TcBLVvRgZo-fIaBDtw2WgABtvCef7xX27Y5xaOkz45IpWmN120RMiGFSnKPsrpPOPnmPX6Dnw0/s72-w640-c-h464/HILLSHADE-SCRIPT-GEOJAMAL-GOOGLEEARTHENGINE-GEE.png
GEE Academy
https://gee.geojamal.com/2025/04/illuminating-terrain-visualizing.html
https://gee.geojamal.com/
https://gee.geojamal.com/
https://gee.geojamal.com/2025/04/illuminating-terrain-visualizing.html
true
3421025227311197355
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content