AnesG
AnesG

Reputation: 275

Convert raster to polygon a reclassify NDVI raster on Google Earht Engine

This all the code I am using to estimate the NDVI.

Initial values

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

Select Satilite 0 : (Jun 23, 2015 - Jul 17, 2019) Sentinel 2 Level 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;
}

choose satelite

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

collection

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

Calculate NDVI

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

Reclassify raster

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');

Convert to vector

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

Here i get this error "FeatureCollection (Error) Image.reduceToVectors: First band ('B8') of image must be integral" The band B8 is already a integral

Upvotes: 0

Views: 2674

Answers (1)

Payam Sajadi
Payam Sajadi

Reputation: 26

I am facing a similar issue.
The NDVI value is continuous value and it is not possible to convert it to the vector in this format.
You have to define discrete zones and mask it by empty image.
See this link.

Upvotes: 1

Related Questions