// Create a geometry representing an export region. var HtiPolygon = ee.Geometry.Polygon([[[-74.300537109375, 19.202241064923054], [-74.6795654296875, 18.19282519773319], [-73.465576171875, 17.86374708440522], [-73.3941650390625, 18.104087015773956], [-71.5704345703125, 17.97873309555617], [-71.685791015625, 18.531700307384043], [-71.6583251953125, 18.890695349102117], [-71.5704345703125, 19.254108316440032], [-71.60888671875, 19.694314241825747], [-71.9384765625, 19.89588839936505], [-72.6416015625, 20.086888505561017], [-73.1195068359375, 20.15394153657739], [-73.65234375, 19.880391767822505]]]); //Map.addLayer(geometry); // Start with an image collection for a 1 year period. var collection =ee.ImageCollection('LANDSAT/LE07/C01/T1_SR') .filterBounds(Haiti) .filterDate('1999-12-30', '2001-01-28') .set('SENSOR_ID', 'OLI_TIRS'); //function to remove cloud var maskClear = function(image) { // Bits 3 and 5 are cloud shadow and cloud, respectively. var cloudShadowBitMask = ee.Number(2).pow(3).int(); var cloudsBitMask = ee.Number(2).pow(5).int(); // Get the pixel QA band. var qa = image.select('pixel_qa'); // Both flags should be set to zero, indicating clear conditions. var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0) .and(qa.bitwiseAnd(cloudsBitMask).eq(0)); // Return the masked image, scaled to [0, 1]. return image.updateMask(mask).divide(10000); }; // Map the cloud masking function over the collection. var collection1 = collection.map(maskClear); print('collection1',collection1); var compositeClip = collection1.median() .select(ee.List([0,1,2,3,4,5,6,7]), ee.List(['B1', 'B2', 'B3', 'B4', 'B5', 'B6' , 'B7','B8'])); //print(compositeClip) Map.addLayer(compositeClip,{min:0,max: 0.25, bands: ['B4','B3','B2']}); // subsample training polygons with random points // this ensures all classes have same sample size // also EE can't handle more than ~5000 cells in training areas var trainingLayers =[TreeCover, Shrubs, Agriculture, Plantation, Pasture, BuildUp, BareGround, Water]; var n = 500; var trainingData; var testingData; // loop over training layers for (var i = 0; i < trainingLayers.length; i++) { // sample points within training polygons var pts = ee.FeatureCollection .randomPoints(trainingLayers[i].geometry(), n); // add class var thisclass = trainingLayers[i].get('class'); pts = pts.map(function(f) { return f.set({class: thisclass}); }); // extract raster cell values var trainTestData = compositeClip.sampleRegions(pts, ['class'], 30); //split the samples into training and testing var srsRandomized = trainTestData.randomColumn('random'); var CUTOFF = 0.7; // Filter Random Sample for Visualization var testing = srsRandomized.filter(ee.Filter.gte('random', CUTOFF)); var training = srsRandomized.filter(ee.Filter.lt('random', CUTOFF)); //combine trainging regions together if (i === 0) { trainingData = training; testingData = testing; } else { trainingData = trainingData.merge(training); testingData = testingData.merge(testing); } } //// classify with random forests // use bands 1-8 var bands = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6','B7', 'B8']; // fit a random forests model var classifierModel = ee.Classifier.randomForest(30) .train(trainingData, 'class', bands); //Classify the validation data. var validated = testingData.classify(classifierModel); print(validated); //Get a confusion matrix representing expected accuracy. var testAccuracy = validated.errorMatrix('class', 'classification'); print('Validation error matrix: ', testAccuracy); print('Validation overall accuracy: ', testAccuracy.accuracy()); print('Validation Producer accuracy: ', testAccuracy.producersAccuracy()); print('Validation Consumer accuracy: ', testAccuracy.consumersAccuracy()); print('Kappa:', testAccuracy.kappa()); var table = ee.FeatureCollection([ ee.Feature(null, { 'error matrix': testAccuracy.array(), 'overall accuracy': testAccuracy.accuracy(), 'producer accuracy': testAccuracy.producersAccuracy(), 'consumer accuracy': testAccuracy.consumersAccuracy(), 'kappa' : testAccuracy.kappa() }) ]); Export.table(table, 'exportTabletestAccuracy', {fileFormat: 'CSV'}); // produce the forest change map var classified = compositeClip.classify(classifierModel); var p = ['107113', '5cff08', '5d0511','fff704','f28311','777575', 'e618ff', '0446ff']; print(classified); // display Map.addLayer(classified, {palette: p, min: 0, max: 7}, 'classification'); Export.image(classified,'classified',{ scale: 100, maxPixles: 1e10 });