-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGEE Script: dNDVI (fast)
134 lines (99 loc) · 5.24 KB
/
GEE Script: dNDVI (fast)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Author: Erin Lindsay. erin.lindsay@ntnu.no
// Created: 21.02.2022
//Instructions for use:
// This script is to be run in Google Earth Engine Code Editor.
// This requires a free user account which can be created here: https://earthengine.google.com/new_#/
// Fill in the required user inputs, then click Run.
// The result can be downloaded as a Geotiff by clicking on the Tasks tab, in the panel to the right.
// The images include 5 bands: 'B2' = blue, 'B3' = green, 'B4' = red, 'B8' = near-infrared, 'NDVI' = Normalised Difference Vegetation Index
// These can be viewed as RGB composites, false colour composites, or as a single band NDVI.
// How it works:
// Creates a 'greenest pixel' composite image from the specified date and location.
// This approach is quite good for filtering clouds, but gives strange results over water.
// The difference image is created by subtracting the pre-event image from the post-event image.
// ########################## User Inputs: #########################################
// ###################################################################################
// 1. Define area of interest:
// In the bottom map window, using the drawing buttons to the left, draw a polygon or rectangle around your area of interest.
// This will create a variable called 'geometry' that will appear above the code editor window under the 'Imports'.
// 2. Define date ranges for producing the pre- and post-event greenest-pixel, cloud free composite images.
// This could be done either for 1 month before and after the landslide event - if the events occurred in summer,
// OR if the events occured in another season, use images from the months of peak green vegetation (and minimal shadows)
// from the summer before and after the landslides occured (1 year apart). e.g. PRE = June 2019, and POST = June 2020.
// PRE-EVENT IMAGE
// Dates over which to create a composite.
var pre_start = ee.Date('2019-07-01');
var pre_end = ee.Date('2019-07-31');
// POST-EVENT IMAGE
// Dates over which to create a median composite.
var post_start = ee.Date('2019-08-01');
var post_end = ee.Date('2019-09-01');
// 3. Set the name of the google drive-folder where you want the image to be exported to.
var my_google_drive_folder = "earthengine";
// 4. Click Run - then inspect the results. A grey box on the Map label (top right in Map Window) means the image is loading. This can take a few minutes.
// You can also toggle on or off map layers if you click on the Map button.
// Check results are reasonable - look at the pre and post RGB images to see if there are clouds.
// 5. In the Tasks panel (top right), you can export the images to your google drive folder.
// #####################################################################################
// #####################################################################################
// Visualisation parameters:
// dNDVI
var dNDVI_params = {bands:"NDVI", min:-0.6, max:0.1};
// RGB true colour
var viz_rgb = {bands: ['B4', 'B3', 'B2'], min: 0, max: 3000};
// ######################### Functions #########################
// Add NDVI
function addS2NDVI(image) {
var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
return image.addBands(ndvi);
}
// // lake mask
// function maskLakes(image) {
// var lake_mask = ee.Image.constant(1).clip(lakes).mask().not();
// return image.updateMask(lake_mask);
// }
// ######################### Import Image Collection #########################
// Sentinel-2 surface reflectance data for the composite.
var S2 = ee.ImageCollection('COPERNICUS/S2_SR');
var region = geometry
Map.centerObject(geometry);
// ######################### Make Pre Image #########################
var pre_filtered = S2.filterDate(pre_start, pre_end)
.filterBounds(geometry)
.map(addS2NDVI);
var pre_image = ee.Image(pre_filtered.qualityMosaic('NDVI')).select(['B2', 'B3', 'B4', 'B8', 'NDVI']);
// pre_image = maskLakes(pre_image);
// ######################### Make Post Image #########################
var post_filtered = S2.filterDate(post_start, post_end)
.filterBounds(geometry)
.map(addS2NDVI);
var post_image = ee.Image(post_filtered.qualityMosaic('NDVI')).select(['B2', 'B3', 'B4', 'B8', 'NDVI']);
// post_image = maskLakes(post_image);
// ######################### Make Difference Image #########################
var diff = post_image.subtract(pre_image);
// ######################### Display the results #########################
Map.addLayer(pre_image, viz_rgb, 'pre-event RGB composite');
Map.addLayer(post_image, viz_rgb, 'post-event RGB composite');
Map.addLayer(diff, dNDVI_params, 'difference NDVI');
// ################## EXPORT IMAGES ##########################
Export.image.toDrive({
image : pre_image,
description : 'greenest_S2_pre',
scale : 10,
folder : my_google_drive_folder,
region : geometry
});
Export.image.toDrive({
image : post_image,
description : 'greenest_S2_post',
scale : 10,
folder : my_google_drive_folder,
region : geometry
});
Export.image.toDrive({
image : diff,
description : 'greenest_S2_diff',
scale : 10,
folder : my_google_drive_folder,
region : geometry
});