Học Google Earth Engine 14. Tính toán các chỉ số trên bộ sưu tập ảnh composite

Học Google Earth Engine 14. Tính toán các chỉ số trên bộ sưu tập ảnh composite

4.3. Tính toán trên bộ sưu tập ảnh Computation on ImageCollections

Cho đến nay, chúng tôi đã học cách chạy tính toán trên các hình ảnh đơn lẻ. Nếu bạn muốn áp dụng một số phép tính – chẳng hạn như tính một chỉ số – cho nhiều hình ảnh, bạn cần sử dụng map(). Trước tiên, bạn xác định một hàm lấy 1 hình ảnh và trả về kết quả của phép tính trên hình ảnh đó. Sau đó, bạn có thể map() hàm đó qua ImageCollection, kết quả là ImageCollection mới với kết quả tính toán. Điều này tương tự như vòng lặp for (for-loop) mà bạn có thể quen thuộc – nhưng sử dụng map() cho phép tính toán chạy song song. Tìm hiểu thêm tại Mapping over an ImageCollection.

var s2 = ee.ImageCollection(“COPERNICUS/S2”);

var admin1 = ee.FeatureCollection(“FAO/GAUL_SIMPLIFIED_500m/2015/level1”);

    

var karnataka = admin1.filter(ee.Filter.eq(‘ADM1_NAME’, ‘Karnataka’))

var geometry = karnataka.geometry()

var rgbVis = {min: 0.0, max: 3000, bands: [‘B4’, ‘B3’, ‘B2’]};

 

var filtered = s2.filter(ee.Filter.lt(‘CLOUDY_PIXEL_PERCENTAGE’, 30))

  .filter(ee.Filter.date(‘2019-01-01’, ‘2019-12-31’))

  .filter(ee.Filter.bounds(geometry))

 

var composite = filtered.median().clip(karnataka)

Map.addLayer(composite, rgbVis, ‘Karnataka Composite’)  

 

 

// Write a function that computes NDVI for an image and adds it as a band

function addNDVI(image) {

  var ndvi = image.normalizedDifference([‘B8’, ‘B4’]).rename(‘ndvi’);

  return image.addBands(ndvi);

}

 

// Map the function over the collection

var withNdvi = filtered.map(addNDVI);

 

var composite = withNdvi.median()

 

var ndviComposite = composite.select(‘ndvi’).clip(karnataka)

 

var palette = [

  ‘FFFFFF’, ‘CE7E45’, ‘DF923D’, ‘F1B555’, ‘FCD163’, ’99B718′,

  ’74A901′, ’66A000′, ‘529400’, ‘3E8601’, ‘207401’, ‘056201’,

  ‘004C00’, ‘023B01’, ‘012E01’, ‘011D01’, ‘011301’];

 

var ndviVis = {min:0, max:0.5, palette: palette }

Map.addLayer(ndviComposite, ndviVis, ‘ndvi’)

 

NDVI computation on an ImageCollection 

4.3.1. Bài tập

var s2 = ee.ImageCollection(“COPERNICUS/S2”);

var admin1 = ee.FeatureCollection(“FAO/GAUL_SIMPLIFIED_500m/2015/level1”);

var karnataka = admin1.filter(ee.Filter.eq(‘ADM1_NAME’, ‘Karnataka’))

var geometry = karnataka.geometry()

var rgbVis = {min: 0.0, max: 3000, bands: [‘B4’, ‘B3’, ‘B2’]};

 

var filtered = s2.filter(ee.Filter.lt(‘CLOUDY_PIXEL_PERCENTAGE’, 30))

  .filter(ee.Filter.date(‘2019-01-01’, ‘2019-12-31’))

  .filter(ee.Filter.bounds(geometry))

 

   

var composite = filtered.median().clip(karnataka)

Map.addLayer(composite, rgbVis, ‘Karnataka Composite’)  

 

// This function calculates both NDVI an d NDWI indices

// and returns an image with 2 new bands added to the original image.

function addIndices(image) {

  var ndvi = image.normalizedDifference([‘B8’, ‘B4’]).rename(‘ndvi’);

  var ndwi = image.normalizedDifference([‘B3’, ‘B8’]).rename(‘ndwi’);

  return image.addBands(ndvi).addBands(ndwi);

}

 

// Map the function over the collection

var withIndices = filtered.map(addIndices);

 

// Composite

var composite = withIndices.median()

print(composite)

 

// Extract the ‘ndwi’ band and display a NDWI map

// use the palette [‘white’, ‘blue’]

// Giải nén band ‘ndwi’ và hiển thị bản đồ NDWI

// sử dụng bảng màu [‘white’, ‘blue’]

 

4.3.2. Kết quả

var s2 = ee.ImageCollection(“COPERNICUS/S2”);

var admin1 = ee.FeatureCollection(“FAO/GAUL_SIMPLIFIED_500m/2015/level1”);

var hanoi= admin1.filter(ee.Filter.eq(‘ADM1_NAME’, ‘Ha Noi City’))

var geometry = hanoi.geometry()

var rgbVis = {min: 0.0, max: 3000, bands: [‘B4’, ‘B3’, ‘B2’]};

 

var filtered = s2.filter(ee.Filter.lt(‘CLOUDY_PIXEL_PERCENTAGE’, 30))

  .filter(ee.Filter.date(‘2019-01-01’, ‘2019-12-31’))

  .filter(ee.Filter.bounds(geometry))

 

   

var composite = filtered.median().clip(hanoi)

Map.addLayer(composite, rgbVis, ‘Hanoi Composite’)  

 

// This function calculates both NDVI an d NDWI indices

// and returns an image with 2 new bands added to the original image.

function addIndices(image) {

  var ndvi = image.normalizedDifference([‘B8’, ‘B4’]).rename(‘ndvi’);

  var ndwi = image.normalizedDifference([‘B3’, ‘B8’]).rename(‘ndwi’);

  return image.addBands(ndvi).addBands(ndwi);

}

 

// Map the function over the collection

var withIndices = filtered.map(addIndices);

 

// Composite

var composite = withIndices.median()

print(composite)

 

// Extract the ‘ndwi’ band and display a NDWI map

// use the palette [‘white’, ‘blue’]

 

var extract1 = composite.select(‘ndwi’);

var extract = extract1.clip(geometry);

var ndwiVis = {min:0, max:1, palette: [‘white’, ‘blue’]}

Map.addLayer(extract, ndwiVis, ‘ndwi’)

Map.centerObject(geometry,10)

Xem video

Recommended For You

About the Author: Admin