0

I have a Sentinel 2 satellite image and am rating it. I already classified all the native vegetation separated into a shapefile. Now I need to classify the anthropic uses, I would like the GEE to classify this image but ignore the vegetation areas indicated in the shapefile, how could I do this. I tried this solution Using FeatureCollection to mask an image in Google Earth Engine?, bbut I couldn't replicate,because GEE says "ifc is not a function". How to make it?

Ny code:

var img1 = ee.ImageCollection("COPERNICUS/S2_SR")
.filterMetadata('MGRS_TILE', 'equals', '21KYT')
.filterMetadata('CLOUDY_PIXEL_PERCENTAGE','less_than', 1) 
//.filterMetadata('CLOUD_COVER', 'less_than', 0.01)
.filterDate('2020-01-01','2020-12-26')
.select(['B4', 'B3', 'B2'])
.median();

var img2 = img1.clip(table)

//add a região de interesse
var regiao = img1;

//add info iniciais para o treinamento
var treinamento = img1.sample({
 region: table,
 scale: 10,
 numPixels: 450,
});

//var agrupamento = ee.Clusterer.wekaLVQ(9, 0.1).train(treinamento);//6 6,1
//var agrupamento2 = ee.Clusterer.wekaLVQ(9, 0.7).train(treinamento);//6 6,1
var agrupamento3 = ee.Clusterer.wekaLVQ(9, 1).train(treinamento);//6 6,1

//var resultado = img1.cluster(agrupamento);
//var resultado2 = img1.cluster(agrupamento2);
var resultado3 = img1.cluster(agrupamento3);


//Map.addLayer(resultado, {min:0, max: 6, palette: ['#8668d7', '#03a2b8', '#607f72', 'red', '#5d8f5b', '#e624b6', ]}, 'Resultado_Unsup');
//Map.addLayer(resultado2, {min:0, max: 6, palette: ['#8668d7', '#03a2b8', '#607f72', 'red', '#5d8f5b', '#e624b6', ]}, 'Resultado_Unsup2');
Map.addLayer(resultado3, {min:0, max: 6, palette: ['#8668d7', '#03a2b8', '#607f72', 'red', '#5d8f5b', '#e624b6', ]}, 'Resultado_Unsup3');

//Map.addLayer(image, {bands: ['B4', 'B3', 'B2'], min:178, max:1110},  'S2');
// Export the image, specifying scale and region.
Export.image.toDrive({
 image: resultado3.float(),
  description: 'unsup_Sentinel_9',
  scale: 10,
  region: table,
  folder: 'GEE_Ceroula_3',
  //crs: 'EPSG:32721'
  });  

var styling = {color: 'yellow', fillColor: '00000000'};
Map.addLayer(table.style(styling));

Map.centerObject(table, 10);

1 Answer 1

1

The solution for your problem lies in a three-step processing:

  1. Rasterizing your table (a FeatureCollection) according to a given numerical property (i.e. landuse_type)
  2. Create a mask which will contain all pixels which do belong to a given class (i.e.: the vegetation class).

Small note: It is important to remember that the vegetation class in each pixel will be a value, and not a string.

  1. Finally, make an UpdateMask using your created maks over your ImageCollection (i.e.: Sentinel images)

After these three steps, the rest of your training code will work just fine.

Here is an example which you can check it out:

https://code.earthengine.google.com/9fc631eba38a3f6d7e16deffb0b80fc2

Below I paste the respective link's code:

table = table.map(function(feat){
  
  
  var CD_GEOCME = ee.Number.parse(ee.String(feat.get('CD_GEOCME')).slice(0,4));
  
  return feat.set('CD_GEOCME',CD_GEOCME )});

print(table);

var Image_from_table = table.reduceToImage(['CD_GEOCME'], ee.Reducer.first() ).rename('CD_GEOCME');

print(Image_from_table, 'Image_from_table');

Map.addLayer(Image_from_table, imageVisParam, 'Image_from_table');

var class_number = 1505; 
var mask = Image_from_table.neq(class_number) // preserves all classes that are not 1505

var img1 = ee.ImageCollection("COPERNICUS/S2")
           .filterBounds(table)
           .mosaic()
           .updateMask(mask);

//add info iniciais para o treinamento
var treinamento = img1.sample({
 region: table,
 tileScale :12,
 
 numPixels:15, 
 scale:50000 
 
});

//var agrupamento = ee.Clusterer.wekaLVQ(9, 0.1).train(treinamento);//6 6,1
//var agrupamento2 = ee.Clusterer.wekaLVQ(9, 0.7).train(treinamento);//6 6,1
var agrupamento3 = ee.Clusterer.wekaLVQ(9, 1).train(treinamento);//6 6,1

//var resultado = img1.cluster(agrupamento);
//var resultado2 = img1.cluster(agrupamento2);
var resultado3 = img1.cluster(agrupamento3);

print(resultado3, 'resultado3')
//Map.addLayer(resultado, {min:0, max: 6, palette: ['#8668d7', '#03a2b8', '#607f72', 'red', '#5d8f5b', '#e624b6', ]}, 'Resultado_Unsup');
//Map.addLayer(resultado2, {min:0, max: 6, palette: ['#8668d7', '#03a2b8', '#607f72', 'red', '#5d8f5b', '#e624b6', ]}, 'Resultado_Unsup2');
Map.addLayer(resultado3, {min:0, max: 6, palette: ['#8668d7', '#03a2b8', '#607f72', 'red', '#5d8f5b', '#e624b6', ]}, 'Resultado_Unsup3');

//Map.addLayer(image, {bands: ['B4', 'B3', 'B2'], min:178, max:1110},  'S2');
// Export the image, specifying scale and region.
Export.image.toDrive({
 image: resultado3.float(),
  description: 'unsup_Sentinel_9',
  scale: 10,
  region: table,
  folder: 'GEE_Ceroula_3',
  //crs: 'EPSG:32721'
  });  

var styling = {color: 'yellow', fillColor: '00000000'};
Map.addLayer(table.style(styling), {}, 'Polygon borders', false);

Map.centerObject(table, 5);
1
  • hello Philip. Your answer helped me when you mentioned the need to convert the shapefile to a raster. But I was unable to replicate your code. But his help was a light, as he was working only with shapefile. Thank you very much!!!! Dec 16, 2020 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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