Học Google Earth Engine 12. Earth Engine Objects

Học Google Earth Engine 12. Earth Engine Objects

4.1. Earth Engine Objects

Tập lệnh này giới thiệu các khái niệm cơ bản về API Earth Engine. Khi lập trình trong Earth Engine, bạn phải sử dụng API Earth Engine để các tính toán của bạn có thể sử dụng máy chủ Google Earth Engine. Để tìm hiểu thêm, hãy truy cập phần Đối tượng và phương pháp của Earth Engine (Earth Engine Objects and Methods) trong Hướng dẫn sử dụng Earth Engine.

// Let’s see how to take a list of numbers and add 1 to each element

var myList = ee.List.sequence(1, 10);

 

// Define a function that takes a number and adds 1 to it – Định nghĩa phép toàn + 1

var myFunction = function(number) {

  return number + 1;

}

print(myFunction(1),’my funcition’);

 

//Re-Define a function using Earth Engine API – Định nghĩa lại function sử dụng GE API

var myFunction = function(number) { 

  return ee.Number(number).add(1);

}

 

// Map the function of the list

var newList = myList.map(myFunction); //new list = 1-10 tất cả + 1

print(newList,’new list’); 

 

// Extracting value from a list – Lấy 1 giá trị từ danh sách

var value = newList.get(0) //Lấy giá trị đầu tiên từ new list

print(value) //Kết quả in ra sẽ là 2

 

// Casting

// Let’s try to do some computation on the extracted value

//var newValue = value.add(1)

//print(newValue)

 

// You get an error because Earth Engine doesn’t know what is the type of ‘value’

// We need to cast it to appropriate type first

var value = ee.Number(value)// lấy ra giá trị của biến value đã đặt ở trên

var newValue = value.add(1)// hàm value + 1 (2+1)

print(newValue) //in gia trị mới 

 

// Dates

// For any date computation, you should use ee.Date module – Hàm giá trị thời gian

var date = ee.Date(‘2019-01-01’) //Đặt giá trị thời gian cho date

var futureDate = date.advance(1, ‘year’) //Hàm này có nghĩa là date + 1 năm

print(futureDate) //in ra kết quả

 

Theo nguyên tắc chung, bạn phải luôn sử dụng các phương thức API của Earth Engine trong mã của mình, có một ngoại lệ mà bạn sẽ cần sử dụng phương pháp Javascript phía máy khách. Nếu bạn muốn lấy thời gian hiện tại, máy chủ không biết thời gian của bạn. Bạn cần sử dụng phương thức javascript và truyền nó tới một đối tượng Earth Engine.

var now = Date.now()

print(now)

var now = ee.Date(now)

print(now)

4.1.1. Bài tập

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

var geometry = ee.Geometry.Point([77.60412933051538, 12.952912912328241]);

 

var now = Date.now()

var now = ee.Date(now) 

// Apply another filter to the collection below to filter images

// collected in the last 1-month

// Do not hard-code the dates, it should always show images

// from the past 1-month whenever you run the script

// Hint: Use ee.Date.advance() function

//   to compute the date 1 month before now

//Áp dụng một bộ lọc khác cho bộ sưu tập dưới đây để lọc ảnh

//Hình ảnh thu thập trong vòng 1 tháng qua

//Không nên đặt cứng ngày ngày cụ thể, để ảnh luôn hiện

//Từ 1 tháng đã qua bất kỳ khi bạn nhấn nút run. Điều này nghĩa là cứ khi bạn nhấn nút run nó sẽ tính ngày hiện tại lùi lại 1 tháng. Kết quả phải show ra là như trong vòng 1 tháng qua có bao nhiêu ảnh.

//Gợi ý sử dụng hàm ee.Date.advance()

//Tính toán cho 1 tháng trước đó

 

 

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

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

4.1.2. Kết quả

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

var geometry = ee.Geometry.Point([107.45659042915429, 15.726611146798092]);

Map.centerObject(geometry,5)

var now = Date.now()

var now = ee.Date(now)

var last_month = now.advance(-1, ‘month’)

// print(now,’now’);

// print(last_month,’last’)

 

 

// Apply another filter to the collection below to filter images

// collected in the last 1-month

// Do not hard-code the dates, it should always show images

// from the past 1-month whenever you run the script

// Hint: Use ee.Date.advance() function

//   to compute the date 1 month before now

//Áp dụng một bộ lọc khác cho bộ sưu tập dưới đây để lọc ảnh

//Hình ảnh thu thập trong vòng 1 tháng qua

//Không nên đặt cứng ngày ngày cụ thể, để ảnh luôn hiện

//Từ 1 tháng đã qua bất kỳ khi bạn nhấn nút run. Điều này nghĩa là cứ khi bạn nhấn nút run nó sẽ tính ngày hiện tại lùi lại 1 tháng. Kết quả phải show ra là như trong vòng 1 tháng qua có bao nhiêu ảnh.

//Gợi ý sử dụng hàm ee.Date.advance()

//Tính toán cho 1 tháng trước đó

 

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

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

  .filter(ee.Filter.date(last_month, now))

 

// print(filtered, ‘all’)

 

// var image = ee.Image(filtered.first());

// print(image, ‘first’)

 

var rgbVis = {

  min: 0.0,

  max: 3000,

  bands: [‘B4’, ‘B3’, ‘B2’],

};

Map.addLayer(filtered, rgbVis, ‘Filtered’)

Recommended For You

About the Author: Admin