Hi there. I am looking for info on how to make a script that creates a difference image between two dates, using Sentinel 2 imagery. For example: I would like the script to create and display the difference (change) between the most recent Sentinel 2 image and the image from 10 days prior.
Hi @cycadmedia ,
You could make use of mosaicking: “ORBIT” and select the scenes you’re interested in to calculate the difference.
Below is an example evalscript which finds the most recent acquisition and the nearest acquisition 10-day prior to the most recent one and computes the difference of NDVI between the two acquisitions.
//VERSION=3
// Script to extract NDVI difference between the latest acquisition and the acquisition 10-day prior to the latest within a specified time range
function setup() {
return {
input: [{
bands: ["B04", "B08"],
units: "DN"
}],
output: {
bands: 1,
sampleType: SampleType.FLOAT32
},
mosaicking: Mosaicking.ORBIT
}
}
function evaluatePixel(samples, scenes) {
let latest = samples[0];
let prior = samples[1];
return [index(latest.B08, latest.B04) - index(prior.B08, prior.B04)];
}
function preProcessScenes (collections) {
// sort from most recent to least recent
collections.scenes.orbits = collections.scenes.orbits.sort(
(s1, s2) => new Date(s2.dateFrom) - new Date(s1.dateFrom)
);
let scenes = collections.scenes.orbits;
let latest;
let closest;
latest = closest = scenes[0];
// timestamp of 10-day prior to latest acquisition
let target = new Date(new Date(latest.dateFrom).getTime() - 10*24*3600*1000);
// find closet timestamp to the target
let diff = Number.POSITIVE_INFINITY;
for (let i = 1; i < scenes.length; i++) {
current = new Date(scenes[i].dateFrom);
if (Math.abs(current - target) >= diff) {break;}
diff = Math.abs(current - target);
closest = scenes[i];
}
// filter collections to keep the latest acquisition and the closest acquisitions to the target
collections.scenes.orbits = collections.scenes.orbits.filter(function (orbit) {
var orbitDateFrom = orbit.dateFrom;
return [latest.dateFrom, closest.dateFrom].includes(orbitDateFrom);
})
return collections
}
Thank you kindly. I have just tried this script in the EO Browser and am getting the following error message:
“An error has occurred while fetching images: Format image/png does not support sample type FLOAT32”.
Can you help to solve this error?
Hi @cycadmedia ,
EO Browser is designed for visualisation and it does not support FLOAT32 as mentioned in the error message.
To visualise NDVI difference with EO Browser, you could use the ColorMapVisualizer to set the color from a discrete color map or use the ColorGradientVisualizer to interpolates a color based on interval. You could also simply assign a color to a value using conditional statement as the EO Browser NDVI layer.
Hi @cycadmedia ,
We updated the script with visualisation output and added it to the NDVI difference between two dates page in our custom scripts repository.
Hope this would help!
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.