Bạn đang tìm cách trích xuất dữ liệu NDVI (Chỉ số thực vật chuẩn hóa) từ Google Earth Engine và lưu trữ dưới dạng GeoTIFF? Bài viết này sẽ cung cấp cho bạn hướng dẫn chi tiết từng bước, kèm theo code mẫu và giải thích rõ ràng, giúp bạn dễ dàng thực hiện quy trình này. Việc trích xuất và phân tích dữ liệu NDVI là vô cùng quan trọng trong các lĩnh vực như nông nghiệp, lâm nghiệp và quản lý tài nguyên thiên nhiên.
Quy trình trích xuất NDVI GeoTIFF từ Google Earth Engine bao gồm các bước chính sau:
Trước khi bắt đầu, hãy đảm bảo bạn đã có tài khoản Google Earth Engine và đã kích hoạt nó. Tiếp theo, bạn cần xác định các tham số quan trọng như khu vực quan tâm, thời gian phân tích và các mùa (khô, ẩm) nếu cần thiết. Ví dụ, chúng ta có thể thiết lập khu vực Laikipia, Kenya và thời gian từ năm 2000 đến 2024.
// Filter Laikipia from FAO/GAUL level 2 dataset
var laikipia = ee.FeatureCollection("FAO/GAUL/2015/level2")
.filter(ee.Filter.eq('ADM1_NAME', 'Laikipia'));
// Start and end year for analysis
var startYear = 2000;
var endYear = 2024;
// Seasonal months for dry and wet season
var drySeasonMonths = [1, 2, 6, 7, 9];
var wetSeasonMonths = [3, 4, 5, 10, 11, 12];
Để đảm bảo chất lượng dữ liệu, chúng ta cần loại bỏ ảnh hưởng của mây. Hàm `maskCloudsL2` sẽ giúp bạn thực hiện việc này bằng cách sử dụng thông tin từ lớp QA_PIXEL của Landsat Collection 2 Level-2. Tiếp theo, hàm `computeNDVI` sẽ tính toán chỉ số NDVI từ các kênh phổ đỏ và cận hồng ngoại, đồng thời phân loại thảm thực vật dựa trên giá trị NDVI.
// Cloud mask function for Landsat Collection 2 Level-2
function maskCloudsL2(image) {
var qa = image.select('QA_PIXEL');
var cloud = qa.bitwiseAnd(1 << 3).eq(0) // Cloud
.and(qa.bitwiseAnd(1 << 4).eq(0)); // Cloud shadow
return image.updateMask(cloud);
}
// NDVI computation for each Landsat image
function computeNDVI(image, redBand, nirBand) {
var red = image.select(redBand).multiply(0.0000275).add(-0.2);
var nir = image.select(nirBand).multiply(0.0000275).add(-0.2);
var ndvi = nir.subtract(red).divide(nir.add(red)).rename('NDVI');
// Add vegetation classification based on NDVI values
var ndviClass = ndvi.expression(
"(ndvi >= 0.6) ? 5" +
": (ndvi >= 0.4) ? 4" +
": (ndvi >= 0.2) ? 3" +
": (ndvi >= 0.1) ? 2" +
": 1", {ndvi: ndvi}
).rename('NDVI_Class');
return image.addBands(ndvi).addBands(ndviClass).select(['NDVI', 'NDVI_Class']);
}
Hàm `getLandsatCollection` sẽ giúp bạn tự động chọn bộ sưu tập Landsat phù hợp dựa trên năm phân tích. Hàm này cũng áp dụng hàm `computeNDVI` và `maskCloudsL2` để tiền xử lý dữ liệu.
// Function to select Landsat collection depending on the year
function getLandsatCollection(year) {
if (year <= 2011) {
return ee.ImageCollection('LANDSAT/LT05/C02/T1_L2')
.map(function(img) { return computeNDVI(maskCloudsL2(img), 'SR_B3', 'SR_B4'); });
} else if (year === 2012) {
return ee.ImageCollection('LANDSAT/LE07/C02/T1_L2')
.map(function(img) { return computeNDVI(maskCloudsL2(img), 'SR_B3', 'SR_B4'); });
} else {
return ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
.map(function(img) { return computeNDVI(maskCloudsL2(img), 'SR_B4', 'SR_B5'); });
}
}
Hàm `filterByMonths` cho phép bạn lọc dữ liệu theo mùa (khô hoặc ẩm). Hàm `clipToRegion` đảm bảo rằng dữ liệu chỉ bao gồm khu vực nghiên cứu của bạn (ví dụ: Laikipia). Điều này giúp giảm dung lượng dữ liệu và tăng tốc độ xử lý.
// Function to filter by months (dry or wet season)
function filterByMonths(collection, months) {
return collection.filter(ee.Filter.inList('month', months));
}
// Function to ensure the geometry is valid before clipping
function clipToRegion(image, region) {
var validRegion = region.filterBounds(image.geometry());
return validRegion.size().gt(0) ? image.clip(region) : image;
}
Sử dụng vòng lặp để xử lý từng năm trong khoảng thời gian đã chọn. Với mỗi năm, bạn sẽ lọc bộ sưu tập Landsat, tính toán NDVI trung bình cho cả năm, mùa khô và mùa ẩm, sau đó xuất dữ liệu dưới dạng file GeoTIFF.
// Loop through each year and process NDVI for full year, dry season, and wet season
ee.List.sequence(startYear, endYear).getInfo().forEach(function(year) {
var start = ee.Date.fromYMD(year, 1, 1);
var end = ee.Date.fromYMD(year, 12, 31);
// Filter Landsat collection for the given year and region
var collection = getLandsatCollection(year)
.filterBounds(laikipia)
.filterDate(start, end)
.map(function(img) {
return img.set('month', ee.Date(img.get('system:time_start')).get('month'));
});
// Compute NDVI for full year, dry season, and wet season
var fullNDVI = collection.select(['NDVI', 'NDVI_Class']).mean();
var dryNDVI = filterByMonths(collection, drySeasonMonths).select(['NDVI', 'NDVI_Class']).mean();
var wetNDVI = filterByMonths(collection, wetSeasonMonths).select(['NDVI', 'NDVI_Class']).mean();
// Clip to Laikipia geometry and ensure the region is valid
fullNDVI = clipToRegion(fullNDVI, laikipia);
dryNDVI = clipToRegion(dryNDVI, laikipia);
wetNDVI = clipToRegion(wetNDVI, laikipia);
// Export the NDVI images for full year, dry season, and wet season
Export.image.toDrive({
image: fullNDVI,
description: 'NDVI_' + year + '_FullYear',
scale: 30,
region: laikipia.geometry(),
fileFormat: 'GeoTIFF'
});
Export.image.toDrive({
image: dryNDVI,
description: 'NDVI_' + year + '_DrySeason',
scale: 30,
region: laikipia.geometry(),
fileFormat: 'GeoTIFF'
});
Export.image.toDrive({
image: wetNDVI,
description: 'NDVI_' + year + '_WetSeason',
scale: 30,
region: laikipia.geometry(),
fileFormat: 'GeoTIFF'
});
});
Dữ liệu NDVI GeoTIFF có thể được sử dụng trong nhiều ứng dụng khác nhau, bao gồm:
Bài viết này đã cung cấp cho bạn hướng dẫn chi tiết về cách trích xuất NDVI GeoTIFF từ Google Earth Engine. Với kiến thức và code mẫu được cung cấp, bạn có thể dễ dàng áp dụng quy trình này vào các dự án nghiên cứu và ứng dụng thực tế của mình. Việc sử dụng Google Earth Engine giúp bạn tiết kiệm thời gian và công sức trong việc xử lý dữ liệu không gian, đồng thời mở ra nhiều cơ hội mới trong việc khám phá và hiểu rõ hơn về thế giới xung quanh chúng ta. Hãy bắt đầu khám phá sức mạnh của NDVI GeoTIFF ngay hôm nay!
Bài viết liên quan