将栅格转换为多边形,然后在Google Earht Engine

时间:2019-07-17 23:25:37

标签: javascript google-earth-engine

这是我用来估算NDVI的所有代码。

初始值

var roi =/* color: #98ff00 *//* displayProperties: [{"type": "rectangle"}] */
    ee.Geometry.Polygon(
        [[[-72.3734130976651, -13.430548306629259],
          [-72.3734130976651, -14.326448420293916],
          [-70.7254638789151, -14.326448420293916],
          [-70.7254638789151, -13.430548306629259]]], null, false);
var startDate = ee.Date('2018-08-23');
var endDate = ee.Date('2018-12-21');
var cloud = 20

选择卫星 0:(2015年6月23日至2019年7月17日)前哨2级1C

var selector = 0;
var Sentinel = "";

NDVI

function getNDVI(image){
    var isLandsat75 = (selector !== 0);
    var NDVI = image.expression('(nir - red)/(nir + red)', 
    {'nir':image.select(isLandsat75 ? 'B4' : 'B8'), 
    'red':image.select(isLandsat75 ? 'B8' : 'B4')}); 
    //print(NDVI);
    return NDVI;
}

选择卫星

switch(selector){ case 0: Sentinel =  'COPERNICUS/S2'}

收藏

var collection = ee.ImageCollection(Sentinel) 
  .filter(ee.Filter.lt("CLOUDY_PIXEL_PERCENTAGE", cloud))
  .filterDate(startDate, endDate)
  .filterBounds(roi);
print(collection) 

计算NDVI

var ndviFiltered = collection.map(getNDVI).qualityMosaic('B8').clip(roi);

重新分类栅格

var DTstring = ['1) root 9999 9999 9999',
'2) B8<=0.2 9999 9999 1 *',
'3) B8>0.2 9999 9999 9999',
'6) B8<=0.4 9999 9999 2 *',
'7) B8>0.4 9999 9999 9999',
'14) B8<=0.6 9999 9999 3 *',
'15) B8>0.6 9999 9999 9999',
'30) B8<=0.8 9999 9999 4 *',
'31) B8>0.8 9999 9999 5 *'].join("\n");

var classifier = ee.Classifier.decisionTree(DTstring);
var reclass = ndviFiltered.select('B8').classify(classifier);
print(reclass)

Map.addLayer(reclass,{min:1,max:5}, 'Reclass');

转换为向量

var vectors = ndviFiltered.reduceToVectors({
  geometry: roi,
  scale: 1000,
  geometryType: 'polygon',
  reducer: ee.Reducer.countEvery()
});
print(vectors)

在这里,我收到此错误“ FeatureCollection(错误) Image.reduceToVectors:图像的第一个波段('B8')必须是整数” 频段B8已经是不可或缺的

1 个答案:

答案 0 :(得分:0)

我正面临类似的问题。
NDVI值为连续值,因此无法以这种格式将其转换为向量。
您必须定义离散区域并用空白图像遮盖。
参见此link