Recently, I was trying to sample raster data to create point features. I found an example online and used the code, but even with almost identical code, my exported collection shows 0 elements. I have verified that my input data contains valid pixels, and the band name is 'b1'. I also tried changing the region to a smaller ROI, but the result remained the same.
Can anyone help me resolve this confusion?
Here are my codes:
var filteredInBuffer = filtered.updateMask(bufferZone);
var waterQualityInBuffer = filteredInBuffer
.gt(-9999) // 判断是否有值
.multiply(1) // true→1,false→0
.unmask(0);
// 获取波段名称
//var bandNames = waterQualityInBuffer.bandNames();
// 打印波段名称到控制台
//print('波段名称:', bandNames);
// 栅格转点阵(带经纬度和像元值)
var points = waterQualityInBuffer.addBands(ee.Image.pixelLonLat())
.sample({
region: ee.Geometry.Rectangle([-180, -90, 180, 90]),
geometries: true
//scale: filtered.projection().nominalScale()
})
.map(function(sample) {
var lon = sample.get('longitude');
var lat = sample.get('latitude');
var value = sample.get('b1');
return ee.Feature(ee.Geometry.Point([lon, lat]), {value: value});
});
print(points);
Here are online codes:
var roi = ee.Geometry.Polygon(
[[[-71.65781521358379, 42.881740327133244],
[-71.65591084518321, 42.87907123590606],
[-71.65169977702983, 42.88032128122396],
[-71.65436589279064, 42.88199189973334]]]);
// SRTM数字高程(DEM)数据
var image = ee.Image("USGS/SRTMGL1_003"); // 波段名为 elevation
// 利用 .sample() 和 .map() 实现栅格转点阵并且传递像元值
var points = image.addBands(ee.Image.pixelLonLat()) // 为图像加入像元经纬度两个层
// 用 .sample() 将 ee.Image 转为 ee.FeatureCollection
.sample({
region: roi, // 以roi为采样区,不设置其它参数则采样全部像元
geometries: true // 使生成的点阵中的每个点都置于像元中心
})
// 用 .map() 对要素集中所有的要素逐个处理
.map(function(sample){
var lat = sample.get('latitude');
var lon = sample.get('longitude');
var value = sample.get('elevation');
// 硬定义返回的要素为矢量点,点的位置由经纬度创建,数据为上一行的value
return ee.Feature(ee.Geometry.Point([lon, lat]), {value: value});
});