本文记录了一个将一个影像的所有像素的不同波段值和经纬度坐标保存到csv的代码。
数据集合:COPERNICUS/S2_SR_HARMONIZED
代码链接:
https://code.earthengine.google.com/087dc979836b2c9b9b82982c6d09d18f?noload=true
var scale = 10
var bands = ['B2', 'B3']
var collection = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterDate('2022-07-01', '2022-07-15')
.filterBounds(geometry)
.map(extractPixels)
.flatten()
print(collection)
Export.table.toDrive({
collection: collection,
selectors: ['date', 'latitude', 'longitude'].concat(bands)
})
function extractPixels(image) {
var mask = ee.Image(1) //创建一些掩模,比如去云掩模等等
var pixels = image
.select(bands)
.addBands(ee.Image.pixelLonLat())
.updateMask(image.mask().reduce(ee.Reducer.min()))
.updateMask(mask)
.reduceRegion({
reducer: ee.Reducer.toList(),
geometry: geometry,
scale: scale,
maxPixels: 1e13
})
return ee.FeatureCollection(
ee.Array(pixels.values())
.transpose()
.toList()
.map(function (values) {
var properties = ee.Dictionary.fromLists(pixels.keys(), values)
return ee.Feature(null, properties)
.set('date', image.date().format('yyyy-MM-dd'))
})
)
}