I'd do something like this:
// Landsat 5 TM Raw Image Collection
var lt5 = ee.ImageCollection("LANDSAT/LT5_L1T");
// Filter by date and location (Landsat Path and Row Designation)
var lakeTiticaca_Early = lt5.filterDate('1985-01-01', '1986-01-01')
.filter(ee.Filter.eq('WRS_PATH', 2))
.filter(ee.Filter.eq('WRS_ROW', 71));
// Selecting Images with least cloud cover
var leastCloud = ee.Image(lakeTiticaca_Early.sort('CLOUD_COVER').first());
// Filter by date and location (Landsat Path and Row Designation)
var lakeTiticaca_Later = lt5.filterDate('2011-01-01', '2012-01-01')
.filter(ee.Filter.eq('WRS_PATH', 2))
.filter(ee.Filter.eq('WRS_ROW', 71));
// Selecting Images with least cloud cover
var leastCloud2 = ee.Image(lakeTiticaca_Later.sort('CLOUD_COVER').first());
// Applying MNDWI Function to Least Cloudy Images
var mNDWI_Early = leastCloud.expression(
'(Green - MIR) / (Green + MIR)', {
'Green': leastCloud.select('B2'),
'MIR': leastCloud.select('B5')
});
var mNDWI_Later = leastCloud2.expression(
'(Green - MIR) / (Green + MIR)', {
'Green': leastCloud2.select('B2'),
'MIR': leastCloud2.select('B5')
});
// Creating an image collection from the MNDWI calculated Images
var constant1 = ee.Image(mNDWI_Early).select([0],["mNDWI"]);
Map.addLayer(leastCloud,{bands:["B4","B5","B3"]},"before")
var constant2 = ee.Image(mNDWI_Later).select([0],["mNDWI"]);
Map.addLayer(leastCloud2,{bands:["B4","B5","B3"]},"after")
var dif = constant2.subtract(constant1)
Map.addLayer(dif,{},"dif")
var img1 = leastCloud.addBands(constant1)
var img2 = leastCloud2.addBands(constant2)
var final = ee.Image.cat(img1,img2)
Map.addLayer(final,{},"final")
//* Supervised Image Classification *//
var training = ee.FeatureCollection([water_nochange, water_gain, water_loss, land_nochange]).flatten()
var sample = final.sampleRegions(training, ["class"], 30)
var trained = ee.Classifier.cart().train(sample, "class")
var classified = final.classify(trained)
Map.addLayer(classified,{min:1, max:4, palette:["#b3b3ff","#0000e6", "#ff0000", "#009900"]},"classified")
You can access the feature collections in the following link:
https://code.earthengine.google.com/3f4c2908fcf7c469357b80783f3ef26f
You should mask clouds, use another path/row to include the missing part of the lake, and make more training points. You can also try another classifier like svm, randomForest, etc.
The randomness in stratifiedSample is based on the pixel's location and the seed. Your best bet is to use some portion of the image's metadata as the seed. If there's nothing there to use (like a start time), then assign a random number to each image using randomColumn and use that (multiplied by some large constant):
var sampler = function(image){
var seed = image.getNumber('random').multiply(1000000).int()
var sample = image.stratifiedSample({
seed: seed
...
})
}
var mySamples = ic.randomColumn().map(sampler)
Best Answer
Like Noel said, use
randomColumn()
: