From 312e5409bb2fc0ed23cd59b3d98a899c9a36a59d Mon Sep 17 00:00:00 2001 From: chorng Date: Mon, 1 Jul 2024 15:25:18 +0200 Subject: [PATCH] Add eob scripts --- planet_scope/max_ndvi/README.md | 7 +++ planet_scope/max_ndvi/eob.js | 84 +++++++++++++++++++++++++ planet_scope/max_ndvi/raw.js | 34 +++++----- planet_scope/max_ndvi/script.js | 79 +++++++++++------------ planet_scope/ndvi_difference/eob.js | 8 +-- planet_scope/ndvi_difference/raw.js | 86 +++++++++++++------------- planet_scope/ndvi_difference/script.js | 4 +- 7 files changed, 196 insertions(+), 106 deletions(-) create mode 100644 planet_scope/max_ndvi/eob.js diff --git a/planet_scope/max_ndvi/README.md b/planet_scope/max_ndvi/README.md index ba81e080..2f18ae83 100644 --- a/planet_scope/max_ndvi/README.md +++ b/planet_scope/max_ndvi/README.md @@ -5,6 +5,13 @@ grand_parent: Planet layout: script permalink: /planet_scope/max_ndvi/ nav_exclude: true +scripts: +- - Visualization + - script.js +- - EO Browser + - eob.js +- - Raw Values + - raw.js examples: - zoom: '15' lat: '-16.60556' diff --git a/planet_scope/max_ndvi/eob.js b/planet_scope/max_ndvi/eob.js new file mode 100644 index 00000000..6de34df4 --- /dev/null +++ b/planet_scope/max_ndvi/eob.js @@ -0,0 +1,84 @@ +//VERSION=3 + +//Basic initialization setup function +function setup() { + return { + //List of all bands, that will be used in the script, either for visualization or for choosing best pixel + input: [{ + bands: [ + "Red", + "NIR", + ] + }], + //This can always be the same if one is doing RGB images + output: [ + { id: "default", bands: 4 }, + { id: "index", bands: 1, sampleType: "FLOAT32" }, + { id: "eobrowserStats", bands: 2, sampleType: "FLOAT32" }, + { id: "dataMask", bands: 1 } + ], + mosaicking: "ORBIT" + } +} + +/* +In this function we limit the scenes, which are used for processing. +These are based also on input variables. +E.g. if one sets date "2017-03-01" ("TO date") and cloud coverage filter 30%, +all scenes older than 2017-03-01 with cloud coverage 30% will be checked against +further conditions in this function (in this function it is currently limited to 3 months). +The more scenes there are, longer it will take to process the data. +After 60 seconds of processing, there will be a timeout. +*/ + +function preProcessScenes(collections) { + collections.scenes.orbits = collections.scenes.orbits.filter(function (orbit) { + var orbitDateFrom = new Date(orbit.dateFrom) + return orbitDateFrom.getTime() >= (collections.to.getTime() - 3 * 31 * 24 * 3600 * 1000); + }) + return collections +} + +function calcNDVI(sample) { + var denom = sample.Red + sample.NIR; + return ((denom != 0) ? (sample.NIR - sample.Red) / denom : NaN); +} +function evaluatePixel(samples) { + var max = Number.NEGATIVE_INFINITY; + for (let i = 0; i < samples.length; i++) { + var ndvi = calcNDVI(samples[i]); + max = ndvi > max ? ndvi : max; + } + let max_exists = 0; + if (isFinite(max)) { + max_exists = 1; + } + let imgVals; + if (max < -1.1) { imgVals = [0, 0, 0]; } + else if (max < - 0.2) { imgVals = [0.75, 0.75, 0.75]; } + else if (max < - 0.1) { imgVals = [0.86, 0.86, 0.86]; } + else if (max < 0) { imgVals = [1, 1, 0.88]; } + else if (max < 0.025) { imgVals = [1, 0.98, 0.8]; } + else if (max < 0.05) { imgVals = [0.93, 0.91, 0.71]; } + else if (max < 0.075) { imgVals = [0.87, 0.85, 0.61]; } + else if (max < 0.1) { imgVals = [0.8, 0.78, 0.51]; } + else if (max < 0.125) { imgVals = [0.74, 0.72, 0.42]; } + else if (max < 0.15) { imgVals = [0.69, 0.76, 0.38]; } + else if (max < 0.175) { imgVals = [0.64, 0.8, 0.35]; } + else if (max < 0.2) { imgVals = [0.57, 0.75, 0.32]; } + else if (max < 0.25) { imgVals = [0.5, 0.7, 0.28]; } + else if (max < 0.3) { imgVals = [0.44, 0.64, 0.25]; } + else if (max < 0.35) { imgVals = [0.38, 0.59, 0.21]; } + else if (max < 0.4) { imgVals = [0.31, 0.54, 0.18]; } + else if (max < 0.45) { imgVals = [0.25, 0.49, 0.14]; } + else if (max < 0.5) { imgVals = [0.19, 0.43, 0.11]; } + else if (max < 0.55) { imgVals = [0.13, 0.38, 0.07]; } + else if (max < 0.6) { imgVals = [0.06, 0.33, 0.04]; } + else { imgVals = [0, 0.27, 0]; } + return { + default: imgVals.concat(max_exists), + index: [max], + eobrowserStats: [max, max_exists], + dataMask: [max_exists] + } +} \ No newline at end of file diff --git a/planet_scope/max_ndvi/raw.js b/planet_scope/max_ndvi/raw.js index 86cfb704..c8132d34 100644 --- a/planet_scope/max_ndvi/raw.js +++ b/planet_scope/max_ndvi/raw.js @@ -3,15 +3,15 @@ //Basic initialization setup function function setup() { return { - //List of all bands, that will be used in the script, either for visualization or for choosing best pixel + //List of all bands, that will be used in the script, either for visualization or for choosing best pixel input: [{ bands: [ - "Red", - "NIR" + "Red", + "NIR" ] }], - //This can always be the same if one is doing RGB images - output: { bands: 3 }, + //This can always be the same if one is doing RGB images + output: { bands: 1 }, mosaicking: "ORBIT" } } @@ -26,23 +26,23 @@ The more scenes there are, longer it will take to process the data. After 60 seconds of processing, there will be a timeout. */ -function preProcessScenes (collections) { - collections.scenes.orbits = collections.scenes.orbits.filter(function (orbit) { - var orbitDateFrom = new Date(orbit.dateFrom) - return orbitDateFrom.getTime() >= (collections.to.getTime()-3*31*24*3600*1000); - }) - return collections +function preProcessScenes(collections) { + collections.scenes.orbits = collections.scenes.orbits.filter(function (orbit) { + var orbitDateFrom = new Date(orbit.dateFrom) + return orbitDateFrom.getTime() >= (collections.to.getTime() - 3 * 31 * 24 * 3600 * 1000); + }) + return collections } function calcNDVI(sample) { var denom = sample.Red + sample.NIR; return ((denom != 0) ? (sample.NIR - sample.Red) / denom : NaN); } -function evaluatePixel(samples) { - var max = 0; - for (var i= 0; i max ? ndvi:max; - return [max]; +function evaluatePixel(samples) { + var max = Number.NEGATIVE_INFINITY; + for (let i = 0; i < samples.length; i++) { + var ndvi = calcNDVI(samples[i]); + max = ndvi > max ? ndvi : max; } + return [max]; } \ No newline at end of file diff --git a/planet_scope/max_ndvi/script.js b/planet_scope/max_ndvi/script.js index 2407af12..77513ac9 100644 --- a/planet_scope/max_ndvi/script.js +++ b/planet_scope/max_ndvi/script.js @@ -3,15 +3,15 @@ //Basic initialization setup function function setup() { return { - //List of all bands, that will be used in the script, either for visualization or for choosing best pixel + //List of all bands, that will be used in the script, either for visualization or for choosing best pixel input: [{ bands: [ - "Red", - "NIR" + "Red", + "NIR" ] }], - //This can always be the same if one is doing RGB images - output: { bands: 3 }, + //This can always be the same if one is doing RGB images + output: { bands: 4 }, mosaicking: "ORBIT" } } @@ -26,44 +26,47 @@ The more scenes there are, longer it will take to process the data. After 60 seconds of processing, there will be a timeout. */ -function preProcessScenes (collections) { - collections.scenes.orbits = collections.scenes.orbits.filter(function (orbit) { - var orbitDateFrom = new Date(orbit.dateFrom) - return orbitDateFrom.getTime() >= (collections.to.getTime()-3*31*24*3600*1000); - }) - return collections +function preProcessScenes(collections) { + collections.scenes.orbits = collections.scenes.orbits.filter(function (orbit) { + var orbitDateFrom = new Date(orbit.dateFrom) + return orbitDateFrom.getTime() >= (collections.to.getTime() - 3 * 31 * 24 * 3600 * 1000); + }) + return collections } function calcNDVI(sample) { var denom = sample.Red + sample.NIR; return ((denom != 0) ? (sample.NIR - sample.Red) / denom : NaN); } -function evaluatePixel(samples) { - var max = 0; - for (var i= 0; i max ? ndvi:max; +function evaluatePixel(samples) { + var max = Number.NEGATIVE_INFINITY; + for (let i = 0; i < samples.length; i++) { + var ndvi = calcNDVI(samples[i]); + max = ndvi > max ? ndvi : max; } -if (max < -1.1) return [0,0,0]; -else if (max <- 0.2) return [0.75,0.75,0.75]; -else if (max <- 0.1) return [0.86,0.86,0.86]; -else if (max < 0) return [1,1,0.88]; -else if (max < 0.025) return [1,0.98,0.8]; -else if (max < 0.05) return [0.93,0.91,0.71]; -else if (max < 0.075) return [0.87,0.85,0.61]; -else if (max < 0.1) return [0.8,0.78,0.51]; -else if (max < 0.125) return [0.74,0.72,0.42]; -else if (max < 0.15) return [0.69,0.76,0.38]; -else if (max < 0.175) return [0.64,0.8,0.35]; -else if (max < 0.2) return [0.57,0.75,0.32]; -else if (max < 0.25) return [0.5,0.7,0.28]; -else if (max < 0.3) return [0.44,0.64,0.25]; -else if (max < 0.35) return [0.38,0.59,0.21]; -else if (max < 0.4) return [0.31,0.54,0.18]; -else if (max < 0.45) return [0.25,0.49,0.14]; -else if (max < 0.5) return [0.19,0.43,0.11]; -else if (max < 0.55) return [0.13,0.38,0.07]; -else if (max < 0.6) return [0.06,0.33,0.04]; -else return [0,0.27,0]; - + let max_exists = 0; + if (isFinite(max)) { + max_exists = 1; + } + if (max < -1.1) { return [0, 0, 0, max_exists]; } + else if (max < - 0.2) { return [0.75, 0.75, 0.75, max_exists]; } + else if (max < - 0.1) { return [0.86, 0.86, 0.86, max_exists]; } + else if (max < 0) { return [1, 1, 0.88, max_exists]; } + else if (max < 0.025) { return [1, 0.98, 0.8, max_exists]; } + else if (max < 0.05) { return [0.93, 0.91, 0.71, max_exists]; } + else if (max < 0.075) { return [0.87, 0.85, 0.61, max_exists]; } + else if (max < 0.1) { return [0.8, 0.78, 0.51, max_exists]; } + else if (max < 0.125) { return [0.74, 0.72, 0.42, max_exists]; } + else if (max < 0.15) { return [0.69, 0.76, 0.38, max_exists]; } + else if (max < 0.175) { return [0.64, 0.8, 0.35, max_exists]; } + else if (max < 0.2) { return [0.57, 0.75, 0.32, max_exists]; } + else if (max < 0.25) { return [0.5, 0.7, 0.28, max_exists]; } + else if (max < 0.3) { return [0.44, 0.64, 0.25, max_exists]; } + else if (max < 0.35) { return [0.38, 0.59, 0.21, max_exists]; } + else if (max < 0.4) { return [0.31, 0.54, 0.18, max_exists]; } + else if (max < 0.45) { return [0.25, 0.49, 0.14, max_exists]; } + else if (max < 0.5) { return [0.19, 0.43, 0.11, max_exists]; } + else if (max < 0.55) { return [0.13, 0.38, 0.07, max_exists]; } + else if (max < 0.6) { return [0.06, 0.33, 0.04, max_exists]; } + else { return [0, 0.27, 0, max_exists]; } } \ No newline at end of file diff --git a/planet_scope/ndvi_difference/eob.js b/planet_scope/ndvi_difference/eob.js index 5d547acd..0d3a7299 100644 --- a/planet_scope/ndvi_difference/eob.js +++ b/planet_scope/ndvi_difference/eob.js @@ -10,7 +10,7 @@ function setup() { output: [ { id: "default", bands: 4 }, { id: "index", bands: 1, sampleType: "FLOAT32" }, - { id: "eobrowserStats", bands: 1, sampleType: "FLOAT32" }, + { id: "eobrowserStats", bands: 2, sampleType: "FLOAT32" }, { id: "dataMask", bands: 1 } ], mosaicking: Mosaicking.ORBIT @@ -37,12 +37,10 @@ function evaluatePixel(samples) { ] const visualizer = new ColorRampVisualizer(ramps); let imgVals = visualizer.process(diff); - imgVals.push(dataMask) - return { - default: imgVals, + default: imgVals.concat(dataMask), index: [diff], - eobrowserStats: [diff], + eobrowserStats: [diff, dataMask], dataMask: [dataMask] }; } diff --git a/planet_scope/ndvi_difference/raw.js b/planet_scope/ndvi_difference/raw.js index cdac9ad5..4069553a 100644 --- a/planet_scope/ndvi_difference/raw.js +++ b/planet_scope/ndvi_difference/raw.js @@ -2,51 +2,51 @@ // Script to extract NDVI difference between the latest acquisition and the acquisition 10-day prior to the latest within a specified time range // Returns raw (Float32) values function setup() { - return { - input: [{ - bands: ["Red", "NIR", "dataMask"], - units: "DN" - }], - output: { - bands: 1, - sampleType: SampleType.FLOAT32 - }, - mosaicking: Mosaicking.ORBIT - } - - } - - function evaluatePixel(samples) { - // ndvi difference - let latest = samples[0]; - let prior = samples[1]; - let combinedMask = latest.dataMask * prior.dataMask; - const diff = combinedMask === 1 ? index(latest.NIR, latest.Red) - index(prior.NIR, prior.Red) : NaN; - return [diff]; + return { + input: [{ + bands: ["Red", "NIR", "dataMask"], + units: "DN" + }], + output: { + bands: 1, + sampleType: SampleType.FLOAT32 + }, + mosaicking: Mosaicking.ORBIT } - 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]; +} + +function evaluatePixel(samples) { + // ndvi difference + let latest = samples[0]; + let prior = samples[1]; + let combinedMask = latest.dataMask * prior.dataMask; + const diff = combinedMask === 1 ? index(latest.NIR, latest.Red) - index(prior.NIR, prior.Red) : NaN; + return [diff]; +} - // timestamp of 10-day prior to latest acquisition - let target = new Date(new Date(latest.dateFrom).getTime() - 10*24*3600*1000); +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) + ); - // 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]; - } - collections.scenes.orbits = [latest, closest]; - return collections + 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]; + } + collections.scenes.orbits = [latest, closest]; + return collections } diff --git a/planet_scope/ndvi_difference/script.js b/planet_scope/ndvi_difference/script.js index 5c1f0d5f..a99911cc 100644 --- a/planet_scope/ndvi_difference/script.js +++ b/planet_scope/ndvi_difference/script.js @@ -32,9 +32,7 @@ function evaluatePixel(samples) { ] const visualizer = new ColorRampVisualizer(ramps); let imgVals = visualizer.process(diff); - imgVals.push(dataMask) - - return imgVals + return imgVals.concat(dataMask) } function preProcessScenes(collections) {