Google Earth Engine (GEE) 的基本语法主要涉及JavaScript,并结合了GEE特有的函数和方法。以下是一些常用的GEE基本语法和操作:
1. 基本数据类型
- Number:
ee.Number(42)
- String:
ee.String('Hello, World!')
- List:
ee.List([1, 2, 3, 4, 5])
- Dictionary:
ee.Dictionary({'key': 'value'})
- Date:
ee.Date('2020-01-01')
- Geometry:
ee.Geometry.Point([longitude, latitude])
- Feature:
ee.Feature(geometry, {properties})
- FeatureCollection:
ee.FeatureCollection([features])
- Image:
ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_123032_20200709')
- ImageCollection:
ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
2. 常用操作和方法
加载和查看影像
var image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_123032_20200709');
Map.centerObject(image, 10);
Map.addLayer(image, {bands: ['B4', 'B3', 'B2'], min: 0, max: 0.3}, 'True Color');
影像集合的筛选
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate('2020-01-01', '2020-12-31')
.filterBounds(ee.Geometry.Point([longitude, latitude]));
数学和统计运算
var meanImage = collection.mean();
var minImage = collection.min();
var maxImage = collection.max();
var medianImage = collection.median();
影像的波段选择和计算
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
var ndviParams = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
Map.addLayer(ndvi, ndviParams, 'NDVI');
几何对象的创建和操作
var point = ee.Geometry.Point([longitude, latitude]);
var buffer = point.buffer(1000); // 创建一个1000米的缓冲区
var bounds = buffer.bounds(); // 获取缓冲区的边界
Map.addLayer(buffer, {}, 'Buffer');
特征和特征集合
var feature = ee.Feature(point, {name: 'Sample Point'});
var featureCollection = ee.FeatureCollection([feature]);
Map.addLayer(featureCollection, {}, 'Feature Collection');
映射函数和自定义函数
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA');
var addNDVI = function(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
return image.addBands(ndvi);
};
var withNDVI = collection.map(addNDVI);
打印和导出
var image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_123032_20200709');
print('Image Information:', image);
Export.image.toDrive({
image: image,
description: 'Landsat8_TOA',
scale: 30,
region: regionGeometry
});
3. 代码示例
// 加载Landsat 8影像集合
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate('2020-01-01', '2020-12-31')
.filterBounds(ee.Geometry.Point([longitude, latitude]));
// 计算NDVI并添加为波段
var addNDVI = function(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
return image.addBands(ndvi);
};
var withNDVI = collection.map(addNDVI);
// 选择一个影像并可视化
var image = withNDVI.first();
var ndviParams = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
Map.centerObject(image, 10);
Map.addLayer(image.select('NDVI'), ndviParams, 'NDVI');