天天看點

GEE基礎學習——Hillshade山陰的計算和顯示

很多時候我們需要計算山陰主要通過ee.Terrain.hillshade()來實作,具體代碼如下:

// Hillshade example.  This is a demonstration of computing
// a hillshade from terrain data and displaying multiple
// layers based on multiple view geometries.  Hillshade
// creation is also provided by ee.Terrain.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 meaasures 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');
}      

這個代碼的具體思路是首先,及建立一個函數,完成有角度向弧度的轉換!

其次就是定義一個新的函數,來計算太陽方位角和高程并通過傳回值計算 Hillshade = cos(Azimuth - Aspect) * sin(Slope) * sin(Zenith) +cos(Zenith) * cos(Slope),另外需要注意,我們算數的(+, -, /, *)是不能執行的,因為這裡沒有用到image.expression進行,也沒有合适的波段去進行選擇,是以要用英文字母的加減乘除。

剩餘的地方基本上就是通過影像分别計算slope和aspect然後進行函數的hillshade的函數計算。

最後進行圖像的中心位置顯示。