Skip to content

Commit 312e540

Browse files
committed
Add eob scripts
1 parent 372168e commit 312e540

File tree

7 files changed

+196
-106
lines changed

7 files changed

+196
-106
lines changed

planet_scope/max_ndvi/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ grand_parent: Planet
55
layout: script
66
permalink: /planet_scope/max_ndvi/
77
nav_exclude: true
8+
scripts:
9+
- - Visualization
10+
- script.js
11+
- - EO Browser
12+
- eob.js
13+
- - Raw Values
14+
- raw.js
815
examples:
916
- zoom: '15'
1017
lat: '-16.60556'

planet_scope/max_ndvi/eob.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//VERSION=3
2+
3+
//Basic initialization setup function
4+
function setup() {
5+
return {
6+
//List of all bands, that will be used in the script, either for visualization or for choosing best pixel
7+
input: [{
8+
bands: [
9+
"Red",
10+
"NIR",
11+
]
12+
}],
13+
//This can always be the same if one is doing RGB images
14+
output: [
15+
{ id: "default", bands: 4 },
16+
{ id: "index", bands: 1, sampleType: "FLOAT32" },
17+
{ id: "eobrowserStats", bands: 2, sampleType: "FLOAT32" },
18+
{ id: "dataMask", bands: 1 }
19+
],
20+
mosaicking: "ORBIT"
21+
}
22+
}
23+
24+
/*
25+
In this function we limit the scenes, which are used for processing.
26+
These are based also on input variables.
27+
E.g. if one sets date "2017-03-01" ("TO date") and cloud coverage filter 30%,
28+
all scenes older than 2017-03-01 with cloud coverage 30% will be checked against
29+
further conditions in this function (in this function it is currently limited to 3 months).
30+
The more scenes there are, longer it will take to process the data.
31+
After 60 seconds of processing, there will be a timeout.
32+
*/
33+
34+
function preProcessScenes(collections) {
35+
collections.scenes.orbits = collections.scenes.orbits.filter(function (orbit) {
36+
var orbitDateFrom = new Date(orbit.dateFrom)
37+
return orbitDateFrom.getTime() >= (collections.to.getTime() - 3 * 31 * 24 * 3600 * 1000);
38+
})
39+
return collections
40+
}
41+
42+
function calcNDVI(sample) {
43+
var denom = sample.Red + sample.NIR;
44+
return ((denom != 0) ? (sample.NIR - sample.Red) / denom : NaN);
45+
}
46+
function evaluatePixel(samples) {
47+
var max = Number.NEGATIVE_INFINITY;
48+
for (let i = 0; i < samples.length; i++) {
49+
var ndvi = calcNDVI(samples[i]);
50+
max = ndvi > max ? ndvi : max;
51+
}
52+
let max_exists = 0;
53+
if (isFinite(max)) {
54+
max_exists = 1;
55+
}
56+
let imgVals;
57+
if (max < -1.1) { imgVals = [0, 0, 0]; }
58+
else if (max < - 0.2) { imgVals = [0.75, 0.75, 0.75]; }
59+
else if (max < - 0.1) { imgVals = [0.86, 0.86, 0.86]; }
60+
else if (max < 0) { imgVals = [1, 1, 0.88]; }
61+
else if (max < 0.025) { imgVals = [1, 0.98, 0.8]; }
62+
else if (max < 0.05) { imgVals = [0.93, 0.91, 0.71]; }
63+
else if (max < 0.075) { imgVals = [0.87, 0.85, 0.61]; }
64+
else if (max < 0.1) { imgVals = [0.8, 0.78, 0.51]; }
65+
else if (max < 0.125) { imgVals = [0.74, 0.72, 0.42]; }
66+
else if (max < 0.15) { imgVals = [0.69, 0.76, 0.38]; }
67+
else if (max < 0.175) { imgVals = [0.64, 0.8, 0.35]; }
68+
else if (max < 0.2) { imgVals = [0.57, 0.75, 0.32]; }
69+
else if (max < 0.25) { imgVals = [0.5, 0.7, 0.28]; }
70+
else if (max < 0.3) { imgVals = [0.44, 0.64, 0.25]; }
71+
else if (max < 0.35) { imgVals = [0.38, 0.59, 0.21]; }
72+
else if (max < 0.4) { imgVals = [0.31, 0.54, 0.18]; }
73+
else if (max < 0.45) { imgVals = [0.25, 0.49, 0.14]; }
74+
else if (max < 0.5) { imgVals = [0.19, 0.43, 0.11]; }
75+
else if (max < 0.55) { imgVals = [0.13, 0.38, 0.07]; }
76+
else if (max < 0.6) { imgVals = [0.06, 0.33, 0.04]; }
77+
else { imgVals = [0, 0.27, 0]; }
78+
return {
79+
default: imgVals.concat(max_exists),
80+
index: [max],
81+
eobrowserStats: [max, max_exists],
82+
dataMask: [max_exists]
83+
}
84+
}

planet_scope/max_ndvi/raw.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
//Basic initialization setup function
44
function setup() {
55
return {
6-
//List of all bands, that will be used in the script, either for visualization or for choosing best pixel
6+
//List of all bands, that will be used in the script, either for visualization or for choosing best pixel
77
input: [{
88
bands: [
9-
"Red",
10-
"NIR"
9+
"Red",
10+
"NIR"
1111
]
1212
}],
13-
//This can always be the same if one is doing RGB images
14-
output: { bands: 3 },
13+
//This can always be the same if one is doing RGB images
14+
output: { bands: 1 },
1515
mosaicking: "ORBIT"
1616
}
1717
}
@@ -26,23 +26,23 @@ The more scenes there are, longer it will take to process the data.
2626
After 60 seconds of processing, there will be a timeout.
2727
*/
2828

29-
function preProcessScenes (collections) {
30-
collections.scenes.orbits = collections.scenes.orbits.filter(function (orbit) {
31-
var orbitDateFrom = new Date(orbit.dateFrom)
32-
return orbitDateFrom.getTime() >= (collections.to.getTime()-3*31*24*3600*1000);
33-
})
34-
return collections
29+
function preProcessScenes(collections) {
30+
collections.scenes.orbits = collections.scenes.orbits.filter(function (orbit) {
31+
var orbitDateFrom = new Date(orbit.dateFrom)
32+
return orbitDateFrom.getTime() >= (collections.to.getTime() - 3 * 31 * 24 * 3600 * 1000);
33+
})
34+
return collections
3535
}
3636

3737
function calcNDVI(sample) {
3838
var denom = sample.Red + sample.NIR;
3939
return ((denom != 0) ? (sample.NIR - sample.Red) / denom : NaN);
4040
}
41-
function evaluatePixel(samples) {
42-
var max = 0;
43-
for (var i= 0; i<samples.length;i++) {
44-
var ndvi = calcNDVI(samples[i]);
45-
max = ndvi > max ? ndvi:max;
46-
return [max];
41+
function evaluatePixel(samples) {
42+
var max = Number.NEGATIVE_INFINITY;
43+
for (let i = 0; i < samples.length; i++) {
44+
var ndvi = calcNDVI(samples[i]);
45+
max = ndvi > max ? ndvi : max;
4746
}
47+
return [max];
4848
}

planet_scope/max_ndvi/script.js

+41-38
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
//Basic initialization setup function
44
function setup() {
55
return {
6-
//List of all bands, that will be used in the script, either for visualization or for choosing best pixel
6+
//List of all bands, that will be used in the script, either for visualization or for choosing best pixel
77
input: [{
88
bands: [
9-
"Red",
10-
"NIR"
9+
"Red",
10+
"NIR"
1111
]
1212
}],
13-
//This can always be the same if one is doing RGB images
14-
output: { bands: 3 },
13+
//This can always be the same if one is doing RGB images
14+
output: { bands: 4 },
1515
mosaicking: "ORBIT"
1616
}
1717
}
@@ -26,44 +26,47 @@ The more scenes there are, longer it will take to process the data.
2626
After 60 seconds of processing, there will be a timeout.
2727
*/
2828

29-
function preProcessScenes (collections) {
30-
collections.scenes.orbits = collections.scenes.orbits.filter(function (orbit) {
31-
var orbitDateFrom = new Date(orbit.dateFrom)
32-
return orbitDateFrom.getTime() >= (collections.to.getTime()-3*31*24*3600*1000);
33-
})
34-
return collections
29+
function preProcessScenes(collections) {
30+
collections.scenes.orbits = collections.scenes.orbits.filter(function (orbit) {
31+
var orbitDateFrom = new Date(orbit.dateFrom)
32+
return orbitDateFrom.getTime() >= (collections.to.getTime() - 3 * 31 * 24 * 3600 * 1000);
33+
})
34+
return collections
3535
}
3636

3737
function calcNDVI(sample) {
3838
var denom = sample.Red + sample.NIR;
3939
return ((denom != 0) ? (sample.NIR - sample.Red) / denom : NaN);
4040
}
41-
function evaluatePixel(samples) {
42-
var max = 0;
43-
for (var i= 0; i<samples.length;i++) {
44-
var ndvi = calcNDVI(samples[i]);
45-
max = ndvi > max ? ndvi:max;
41+
function evaluatePixel(samples) {
42+
var max = Number.NEGATIVE_INFINITY;
43+
for (let i = 0; i < samples.length; i++) {
44+
var ndvi = calcNDVI(samples[i]);
45+
max = ndvi > max ? ndvi : max;
4646
}
47-
if (max < -1.1) return [0,0,0];
48-
else if (max <- 0.2) return [0.75,0.75,0.75];
49-
else if (max <- 0.1) return [0.86,0.86,0.86];
50-
else if (max < 0) return [1,1,0.88];
51-
else if (max < 0.025) return [1,0.98,0.8];
52-
else if (max < 0.05) return [0.93,0.91,0.71];
53-
else if (max < 0.075) return [0.87,0.85,0.61];
54-
else if (max < 0.1) return [0.8,0.78,0.51];
55-
else if (max < 0.125) return [0.74,0.72,0.42];
56-
else if (max < 0.15) return [0.69,0.76,0.38];
57-
else if (max < 0.175) return [0.64,0.8,0.35];
58-
else if (max < 0.2) return [0.57,0.75,0.32];
59-
else if (max < 0.25) return [0.5,0.7,0.28];
60-
else if (max < 0.3) return [0.44,0.64,0.25];
61-
else if (max < 0.35) return [0.38,0.59,0.21];
62-
else if (max < 0.4) return [0.31,0.54,0.18];
63-
else if (max < 0.45) return [0.25,0.49,0.14];
64-
else if (max < 0.5) return [0.19,0.43,0.11];
65-
else if (max < 0.55) return [0.13,0.38,0.07];
66-
else if (max < 0.6) return [0.06,0.33,0.04];
67-
else return [0,0.27,0];
68-
47+
let max_exists = 0;
48+
if (isFinite(max)) {
49+
max_exists = 1;
50+
}
51+
if (max < -1.1) { return [0, 0, 0, max_exists]; }
52+
else if (max < - 0.2) { return [0.75, 0.75, 0.75, max_exists]; }
53+
else if (max < - 0.1) { return [0.86, 0.86, 0.86, max_exists]; }
54+
else if (max < 0) { return [1, 1, 0.88, max_exists]; }
55+
else if (max < 0.025) { return [1, 0.98, 0.8, max_exists]; }
56+
else if (max < 0.05) { return [0.93, 0.91, 0.71, max_exists]; }
57+
else if (max < 0.075) { return [0.87, 0.85, 0.61, max_exists]; }
58+
else if (max < 0.1) { return [0.8, 0.78, 0.51, max_exists]; }
59+
else if (max < 0.125) { return [0.74, 0.72, 0.42, max_exists]; }
60+
else if (max < 0.15) { return [0.69, 0.76, 0.38, max_exists]; }
61+
else if (max < 0.175) { return [0.64, 0.8, 0.35, max_exists]; }
62+
else if (max < 0.2) { return [0.57, 0.75, 0.32, max_exists]; }
63+
else if (max < 0.25) { return [0.5, 0.7, 0.28, max_exists]; }
64+
else if (max < 0.3) { return [0.44, 0.64, 0.25, max_exists]; }
65+
else if (max < 0.35) { return [0.38, 0.59, 0.21, max_exists]; }
66+
else if (max < 0.4) { return [0.31, 0.54, 0.18, max_exists]; }
67+
else if (max < 0.45) { return [0.25, 0.49, 0.14, max_exists]; }
68+
else if (max < 0.5) { return [0.19, 0.43, 0.11, max_exists]; }
69+
else if (max < 0.55) { return [0.13, 0.38, 0.07, max_exists]; }
70+
else if (max < 0.6) { return [0.06, 0.33, 0.04, max_exists]; }
71+
else { return [0, 0.27, 0, max_exists]; }
6972
}

planet_scope/ndvi_difference/eob.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function setup() {
1010
output: [
1111
{ id: "default", bands: 4 },
1212
{ id: "index", bands: 1, sampleType: "FLOAT32" },
13-
{ id: "eobrowserStats", bands: 1, sampleType: "FLOAT32" },
13+
{ id: "eobrowserStats", bands: 2, sampleType: "FLOAT32" },
1414
{ id: "dataMask", bands: 1 }
1515
],
1616
mosaicking: Mosaicking.ORBIT
@@ -37,12 +37,10 @@ function evaluatePixel(samples) {
3737
]
3838
const visualizer = new ColorRampVisualizer(ramps);
3939
let imgVals = visualizer.process(diff);
40-
imgVals.push(dataMask)
41-
4240
return {
43-
default: imgVals,
41+
default: imgVals.concat(dataMask),
4442
index: [diff],
45-
eobrowserStats: [diff],
43+
eobrowserStats: [diff, dataMask],
4644
dataMask: [dataMask]
4745
};
4846
}

planet_scope/ndvi_difference/raw.js

+43-43
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,51 @@
22
// Script to extract NDVI difference between the latest acquisition and the acquisition 10-day prior to the latest within a specified time range
33
// Returns raw (Float32) values
44
function setup() {
5-
return {
6-
input: [{
7-
bands: ["Red", "NIR", "dataMask"],
8-
units: "DN"
9-
}],
10-
output: {
11-
bands: 1,
12-
sampleType: SampleType.FLOAT32
13-
},
14-
mosaicking: Mosaicking.ORBIT
15-
}
16-
17-
}
18-
19-
function evaluatePixel(samples) {
20-
// ndvi difference
21-
let latest = samples[0];
22-
let prior = samples[1];
23-
let combinedMask = latest.dataMask * prior.dataMask;
24-
const diff = combinedMask === 1 ? index(latest.NIR, latest.Red) - index(prior.NIR, prior.Red) : NaN;
25-
return [diff];
5+
return {
6+
input: [{
7+
bands: ["Red", "NIR", "dataMask"],
8+
units: "DN"
9+
}],
10+
output: {
11+
bands: 1,
12+
sampleType: SampleType.FLOAT32
13+
},
14+
mosaicking: Mosaicking.ORBIT
2615
}
2716

28-
function preProcessScenes (collections) {
29-
// sort from most recent to least recent
30-
collections.scenes.orbits = collections.scenes.orbits.sort(
31-
(s1, s2) => new Date(s2.dateFrom) - new Date(s1.dateFrom)
32-
);
33-
34-
let scenes = collections.scenes.orbits;
35-
let latest;
36-
let closest;
37-
latest = closest = scenes[0];
17+
}
18+
19+
function evaluatePixel(samples) {
20+
// ndvi difference
21+
let latest = samples[0];
22+
let prior = samples[1];
23+
let combinedMask = latest.dataMask * prior.dataMask;
24+
const diff = combinedMask === 1 ? index(latest.NIR, latest.Red) - index(prior.NIR, prior.Red) : NaN;
25+
return [diff];
26+
}
3827

39-
// timestamp of 10-day prior to latest acquisition
40-
let target = new Date(new Date(latest.dateFrom).getTime() - 10*24*3600*1000);
28+
function preProcessScenes(collections) {
29+
// sort from most recent to least recent
30+
collections.scenes.orbits = collections.scenes.orbits.sort(
31+
(s1, s2) => new Date(s2.dateFrom) - new Date(s1.dateFrom)
32+
);
4133

42-
// find closet timestamp to the target
43-
let diff = Number.POSITIVE_INFINITY;
44-
for (let i = 1; i < scenes.length; i++) {
45-
current = new Date(scenes[i].dateFrom);
46-
if (Math.abs(current - target) >= diff) {break;}
47-
diff = Math.abs(current - target);
48-
closest = scenes[i];
49-
}
50-
collections.scenes.orbits = [latest, closest];
51-
return collections
34+
let scenes = collections.scenes.orbits;
35+
let latest;
36+
let closest;
37+
latest = closest = scenes[0];
38+
39+
// timestamp of 10-day prior to latest acquisition
40+
let target = new Date(new Date(latest.dateFrom).getTime() - 10 * 24 * 3600 * 1000);
41+
42+
// find closet timestamp to the target
43+
let diff = Number.POSITIVE_INFINITY;
44+
for (let i = 1; i < scenes.length; i++) {
45+
current = new Date(scenes[i].dateFrom);
46+
if (Math.abs(current - target) >= diff) { break; }
47+
diff = Math.abs(current - target);
48+
closest = scenes[i];
49+
}
50+
collections.scenes.orbits = [latest, closest];
51+
return collections
5252
}

planet_scope/ndvi_difference/script.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ function evaluatePixel(samples) {
3232
]
3333
const visualizer = new ColorRampVisualizer(ramps);
3434
let imgVals = visualizer.process(diff);
35-
imgVals.push(dataMask)
36-
37-
return imgVals
35+
return imgVals.concat(dataMask)
3836
}
3937

4038
function preProcessScenes(collections) {

0 commit comments

Comments
 (0)