2

I'm trying to get the mean value of pixels from an NDVI raster, that intersect the geometry of a FeatureCollection, that is a multipart polygon which represents my sample area, but I can't find the way.

Here's the code:

 // Load geometry
var limites_BSHC = ee.FeatureCollection ('users/charlieswall/tesis/BSHC_LIMITES');
var poligonos = ee.FeatureCollection('users/charlieswall/tesis/POLIGONO_BSCH_DATOS_COMPLETO');
var p_inicio = ee.FeatureCollection('users/charlieswall/tesis/puntos_fecha_BSHC');


// load and filter the collection
var Landsat8_1 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA');
var Landsat8_1_Filtrada =   Landsat8_1.filterBounds(limites_BSHC)
.filterDate('2015-05-4', '2015-08-31')
.filterMetadata('CLOUD_COVER','less_than', 10);


//filter by bounds
var Landsat8_2 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA');
var Landsat8_2_Filtrada =   Landsat8_1.filterBounds(limites_BSHC)
.filterDate('2015-08-20', '2015-10-01')
.filterMetadata('CLOUD_COVER','less_than', 10);

//////getting imagery in to variables
//junio
var imagen1 = ee.Image ('LANDSAT/LC08/C01/T1_TOA/LC08_226075_20150807');
var imagen2 = ee.Image ('LANDSAT/LC08/C01/T1_TOA/LC08_226076_20150807');

//septiembre
var imagen3 = ee.Image ('LANDSAT/LC08/C01/T1_TOA/LC08_226075_20150924');
var imagen4 = ee.Image ('LANDSAT/LC08/C01/T1_TOA/LC08_226076_20150924');

//Create mosaics per period    
var mosaic_junio = ee.ImageCollection ([ imagen1, imagen2]).mosaic().clip(limites_BSHC);    
var mosaic_septiembre = ee.ImageCollection([imagen3,imagen4]).mosaic().clip(limites_BSHC);

//Compute NDVI per mosaic    
// Compute the Normalized Difference Vegetation Index (NDVI).
var nir_junio = mosaic_junio.select('B5');
var red_junio = mosaic_junio.select('B4');
var ndvi_junio = nir_junio.subtract(red_junio).divide(nir_junio.add(red_junio )).rename('NDVI_jun');

var nir_septiembre = mosaic_septiembre.select('B5');
var red_septiembre = mosaic_septiembre.select('B4');
var ndvi_septiembre = nir_septiembre.subtract(red_septiembre).divide(nir_septiembre.add(red_septiembre )).rename('NDVI');

print ( ndvi_septiembre);

//Extracting pixel values from sample poligons  
var prueba = ndvi_septiembre.gt(poligonos);
//mask pixels
var septiembre = mosaic_septiembre.updateMask(prueba). addBands(ndvi_septiembre);

var ndvif = function(image){
  //add the NDVI band to the image
  var ndvi = ndvi_septiembre.rename('NDVI');
  //isolate the NDVI band
  var quality = ndvi.select('NDVI');
  //get pixels above the POLYGON
  var ndvi01 = quality.gt(poligonos);
  //create a mask from high likelihood pixels
  var ndvimask = image.mask().and(ndvi01);
  //mask those pixels from the image
  return image.updateMask(ndvimask).addBands(ndvi);
};
var collection= ndvi_septiembre.map(ndvif);

It returns an error that says: ndvi_septiembre.map is not a function

0

I had to try some others ways, and finally got the values by .reduceRegions script, then I wanted to know, how engine computes the pixels by feature so, it's important to have in mind that the scale affects that 'how' depending if the centroid of each pixel is in the area of that feature or not, to be computed. In this way, I used the sampleRegions script in order to get the centroids by feature, to then be able to calculate the mean manually and it works well. Here's the code:

// Load vector files
var limites_BSHC = ee.FeatureCollection ('users/charlieswall/tesis/BSHC_LIMITES');
var poligonos = ee.FeatureCollection('users/charlieswall/tesis/POLIGONO_BSCH_DATOS_COMPLETO');
var p_inicio = ee.FeatureCollection('users/charlieswall/tesis/puntos_fecha_BSHC');

//Call and filter imagery first period
var Landsat8_1 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA');
var Landsat8_1_Filtrada =   Landsat8_1.filterBounds(limites_BSHC)
.filterDate('2015-05-4', '2015-08-31')
.filterMetadata('CLOUD_COVER','less_than', 10);


//Call and filter imagery, second period
var Landsat8_2 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA');
var Landsat8_2_Filtrada =   Landsat8_1.filterBounds(limites_BSHC)
.filterDate('2015-08-20', '2015-10-01')
.filterMetadata('CLOUD_COVER','less_than', 10);


//Print properties
print (Landsat8_1_Filtrada,'junio', Landsat8_2_Filtrada, 'septiembre'); 

//Save imagery into variables
//June
var imagen1 = ee.Image ('LANDSAT/LC08/C01/T1_TOA/LC08_226075_20150807');
var imagen2 = ee.Image ('LANDSAT/LC08/C01/T1_TOA/LC08_226076_20150807');

//September
var imagen3 = ee.Image ('LANDSAT/LC08/C01/T1_TOA/LC08_226075_20150924');
var imagen4 = ee.Image ('LANDSAT/LC08/C01/T1_TOA/LC08_226076_20150924');

//Create mosaics

var mosaic_agosto = ee.ImageCollection ([ imagen1, imagen2]).mosaic().clip(limites_BSHC);

var mosaic_septiembre = ee.ImageCollection([imagen3,imagen4]).mosaic().clip(limites_BSHC);


//Compute ndvi values per mosaic

var ndvi_ago = mosaic_agosto.normalizedDifference(['B5','B4']).rename('NDVI_AGO');
var ndvi_sep = mosaic_septiembre.normalizedDifference(['B5','B4']);

//Get ndvi values per polygon and centroid points

var muestreo = ndvi_ago.sampleRegions({
  collection:poligonos,
  properties:['BIOMASA__T','CODIGO_DE:','fecha'],
  scale:30,
  geometries:true
  });

//Get ndvi mean values
var reduciendo = ndvi_ago.reduceRegions({
  collection: poligonos,
  reducer: ee.Reducer.mean(),
  scale: 30,
});


Map.addLayer (reduciendo);
Map.addLayer (ndvi_ago);
Map.centerObject (limites_BSHC);


  print(ndvi_ago);
  print (reduciendo);
  print(muestreo);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.