sampleRegions采样的时候,千万千万别有空值

前一阵子,徐老师问了我们一个问题,说他的样本数据采样后都变成空的了,然而实际是有影像的。

后来,他把代码发给了我们。我们对其进行了检查,发现在对时序数据采样时,经常会出现这样的问题。主要原因是去云后时序数据很容易出现空值,而一旦出现空值,sampleRegions函数得到的featureCollection数据集就会有样本丢失,甚至可能为空集的情况。因此,在使用sampleRegions函数的时候,一定要时刻留意采样影像是否有空值。

下面的代码部分,大家一定要留意,值得细细品味。

// A Sentinel-2 surface reflectance image.
var img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG');
Map.setCenter(-122.503881, 37.765588, 18);
Map.addLayer(img, {bands: ['B11', 'B8', 'B3'], min: 100, max: 4500}, 'img');

// A feature collection with two polygon regions each intersecting 36
// pixels at 10 m scale.
var fcPolygon = ee.FeatureCollection([
  ee.Feature(ee.Geometry.Rectangle(
    -122.50620929, 37.76502806, -122.50552264, 37.76556663), {id: 0}),
  ee.Feature(ee.Geometry.Rectangle(
    -122.50530270, 37.76565568, -122.50460533, 37.76619425), {id: 1})
]);
Map.addLayer(fcPolygon, {color: 'yellow'}, 'fcPolygon');

var fcPolygonSamp = img.sampleRegions({
  collection: fcPolygon,
  scale: 10,
  geometries: true
});
// Note that 7 pixels are missing from the sample. If a pixel contains a masked
// band value it will be excluded from the sample. In this case, the TCI_B band
// is masked for each unsampled pixel.
print('A feature per pixel (at given scale) in each region', fcPolygonSamp);
Map.addLayer(fcPolygonSamp, {color: 'purple'}, 'fcPolygonSamp');

// A feature collection with two points intersecting two different pixels.
// This example is included to show the behavior for point geometries. In
// practice, if the feature collection is all points, ee.Image.reduceRegions
// should be used instead to save memory.
var fcPoint = ee.FeatureCollection([
  ee.Feature(ee.Geometry.Point([-122.50309256, 37.76605006]), {id: 0}),
  ee.Feature(ee.Geometry.Point([-122.50344661, 37.76560903]), {id: 1})
]);
Map.addLayer(fcPoint, {color: 'cyan'}, 'fcPoint');

var fcPointSamp = img.sampleRegions({
  collection: fcPoint,
  scale: 10
});
print('A feature per point', fcPointSamp);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值