I have a array image:
var blist = change.select('tBreak');
Each pixel value in the image is a time.The time format uses is the serial date, starting from 0000年01月01日.
And I have an image collection of NDSI values per day.
var imgcollection = ee.ImageCollection('LANDSAT/LT05/C01/T1')
.filterDate('2000-02-14','2010-01-01').filterBounds(table)
.map(function(image){return image.clip(table)}) ;
function NDSI_V1(img) {
var ndsi = img.normalizedDifference(["B2","B5"]);
return ndsi;
}
var ndsi = imgcollection.map(NDSI_V1);
I want to get an image where each pixel is the ndsi difference of the time provided by blist.For example, there is a pixel blist whose value is [t1, t2]. Then, the pixel value of the target image is the value of ndsi at time t2 subtract the value of ndsi at time t1. I hope someone can help me.Thanks
1 Answer 1
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
-
Thank you very much for your answer, it completely solved my problemYYu– YYu2020年03月18日 19:51:27 +00:00Commented Mar 18, 2020 at 19:51
-
Sorry to bother you again. How do I get all the change segments? and Does segmentCount record segments with several changes?YYu– YYu2020年03月21日 19:30:57 +00:00Commented Mar 21, 2020 at 19:30
-
In what sense, get all change segments? segmentCount is the number of segments with detected breaks.Daniel Wiell– Daniel Wiell2020年03月23日 14:24:45 +00:00Commented Mar 23, 2020 at 14:24
-
Sorry, I have a related question I want to ask you again.I want to get the NDSI average value of each pixel in the image between time1 and time2.I don't know how to express the image between time1 and time2 of each pixel.Hope you can help me.Many thanksYYu– YYu2020年06月26日 17:10:44 +00:00Commented Jun 26, 2020 at 17:10
-
I used your code and asked this question, you can answer thereYYu– YYu2020年06月26日 17:24:06 +00:00Commented Jun 26, 2020 at 17:24
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!