I'm not completely clear what you want to do, and your assets are still not shared. The below script could maybe give you some ideas, even if it doesn't do exactly what you're after. It does the following:
- Create a collection of NDSI images
- Take the last two (non-zero)
tBreak
values
- Convert them to unix time millis images
- Build two NDSI mosaics, one for each of the time images. Each a quality mosaic with the distance from the time image as quality band.
- The NDSI change is the difference between the two NDSI mosaic.
Script:
var change = ee.Image('users/wiell/CCDCExamples/segments')
var blist = change.select('tBreak')
blist = blist.arrayMask(blist.gt(0))
var region = blist.geometry()
var l5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
.select(['B2', 'B5', 'pixel_qa'], ['green', 'swir1', 'pixel_qa'])
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.select(['B3', 'B6', 'pixel_qa'], ['green', 'swir1', 'pixel_qa'])
var ndsiCollection = l5.merge(l8)
.filterBounds(region)
.map(function (image) {
return image
.select([]).addBands( // Replace bands to keep properties
image.normalizedDifference(['green', 'swir1'])
)
.updateMask(landsatMask(image))
.rename('ndsi')
})
var segmentCount = blist.arrayLength(0)
var time1 = toUnixMillis(segmentCount.subtract(2))
var time2 = toUnixMillis(segmentCount.subtract(1))
var ndsi1 = ndsiForTime(time1)
var ndsi2 = ndsiForTime(time2)
var ndsiChange = ndsi2.subtract(ndsi1)
.rename('ndsiChange')
Map.addLayer(ndsiChange, {min: -0.5, max: 0.5, palette: 'red,orange,yellow,green,blue'}, 'ndsi change')
Map.centerObject(blist)
function ndsiForTime(time) {
return ndsiCollection
.map(function (image) {
var timeDiff = time.subtract(image.date().millis())
.abs().multiply(-1)
.rename('closeness')
return image.addBands(timeDiff)
})
.qualityMosaic('closeness')
.select('ndsi')
}
function toUnixMillis(index) {
var t = blist.arrayGet([index])
.updateMask(segmentCount.gte(2)) // Need to have at least 2 segments
var epochDay = 719529
return t.subtract(epochDay).multiply(1000).multiply(3600).multiply(24)
}
function landsatMask(image) {
var qa = image.select('pixel_qa')
var clean = qa.bitwiseAnd(1 << 1)
var water = qa.bitwiseAnd(1 << 2)
return clean.or(water)
}
https://code.earthengine.google.com/c9dd75397677fbc1966107b0aa21cf66
change
andtable
assets are not shared publicly, so it is difficult to experiment with a solution. Can you please share these assets so it is easier for folks interested in answering your question to do so. If the assets are not public, perhaps you can make mock examples. Thanks!