-
Notifications
You must be signed in to change notification settings - Fork 3
/
ChesapeakeBayAgSentinel2.txt
163 lines (132 loc) · 6.22 KB
/
ChesapeakeBayAgSentinel2.txt
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//2017 Spring GSFC Chesapeake Bay Agriculture
//NASA DEVELOP | Goddard Space Flight Center
//Script Primary Author: Alison Thieme, [email protected]
//Team Members: John Fitz, Sean McCartney, Perry Oddo, Alison Thieme, Sunita Yadav (Team Leader)
//Most Recent Update: April 10, 2018
//
//This script is designed to extract Sentinel-2 NDVI averages from field boundaries and export the values
//as a table. The date of the image is found in the header of the table and the NDVI averages as as the cells
//in the column corresponding to the date. Here CLUID is used as the unique identifier for each field.
//
//Complete top section in order to adjust script for fields and year as needed.
//___________________________________________________________________________________________________________
//Run python script using ArcMap to buffer field polygons 15m inward and separate into 4 shapefiles based on county
//Upload shapefiles as Fusion Table or as a table Asset in Google Earth Engine.
//Shapefiles must be in WGS 1984 as projection
//Import these Fusion Table containing buffered field data for one year
//replace XXXXX with Fusion Table ID or import table from Assets and name var region
//var region = ee.FeatureCollection('ft:XXXXXXXXX', 'geometry'); //buffered field boundaries
//To add a different fusion table, change the ID found after ft: to the one matching your fusion table
//ID found under File>About This Table
//Specify year when Winter Cover Crop planting took place
//Sentinel-2 imagery is available from 2015 onward.
var year = ee.Number(2017);
//When steps above are completed click Run.
//When completed, under Console tab, select location to output data and hit Run.
//This should appear as Winter (December 15-January 31) and Spring (March 1-April 15) tables.
//___________________________________________________________________________________________________________
//******************************DROP HTML DESCRIPTION FIELD***************************************
region = region.map(
function(feature) {
return feature.select(['CLUID']);
});
print('Field Boundaries', region.first());
//******************************CREATE DATE RANGES BASED ON YEAR INPUT ABOVE**********************
var start_date = ee.Date.fromYMD(year, 1, 1)
var advanceyear = start_date.advance(1, 'year');
var nextyear = advanceyear.get('year');
var start_Winter = ee.Date.fromYMD(year, 12, 15);
var end_Winter = ee.Date.fromYMD(nextyear, 1, 31);
var start_Spring = ee.Date.fromYMD(nextyear, 3, 1);
var end_Spring = ee.Date.fromYMD(nextyear, 4, 15);
//******************************IMPORT SENTINEL-2 SURFACE REFLECTANCE COLLECTION******************
// Load a Sentinel-2 ImageCollection for a single path-row for Winter
var Wcollection = ee.ImageCollection("COPERNICUS/S2")
.filterBounds(region)
.filterDate(start_Winter, end_Winter);
print('Collection: ', Wcollection);
// Load a Sentinel-2 ImageCollection for a single path-row for Spring
var Scollection = ee.ImageCollection("COPERNICUS/S2")
.filterBounds(region)
.filterDate(start_Spring, end_Spring);
print('Collection: ', Scollection);
//Map.addLayer(Wcollection.filterBounds(region),
// {'bands': ['B4', 'B3', 'B2'], 'min': 1000, 'max': 9500}, 'Sentinel-2');
//******************************ADD DATE**********************************************************
var addDate = function(image) {
var date = ee.Date(image.get('system:time_start'));
var dateString = date.format('YYYY-MM-dd');
return image.set('date', dateString);
};
var W_dated = Wcollection.map(addDate);
var S_dated = Scollection.map(addDate);
//******************************CLOUD MASKING*****************************************************
// function to mask using 'cfmask' layer
var maskCloud = function(image){
var cfmask = image.select('B10');
return image.updateMask(cfmask.gte(200));
};
var W_cloudmasked = Wcollection.map(maskCloud);
var S_cloudmasked = Scollection.map(maskCloud);
//******************************FUNCTION TO GET NDVI FROM IMAGE***********************************
var S2_getNDVI = function(image) {
return image.addBands(image.normalizedDifference(['B8', 'B4']));
};
var NDVI_W = W_cloudmasked.map(S2_getNDVI);
var NDVI_S = S_cloudmasked.map(S2_getNDVI);
Map.setCenter(-76.575367, 39.393800, 8);
Map.addLayer(NDVI_S, {bands: ['nd'], palette: '000000, 00FF00', min: 0, max: 0.7}, 'NDVI');
//******************************RENAME NDVI TO DATE***********************************************
NDVI_W = NDVI_W.map(function(i) {
return ee.Image(i)
.select(['nd'],[ee.Date(ee.Image(i).get('system:time_start')).format('YYYY-MM-dd')])});
NDVI_S = NDVI_S.map(function(i) {
return ee.Image(i)
.select(['nd'],[ee.Date(ee.Image(i).get('system:time_start')).format('YYYY-MM-dd')])});
//******************************EXTRACT MEAN NDVI FOR ALL FIELDS*********************************
var stack = function (i1, i2) {
return ee.Image(i2).addBands(ee.Image(i1));
};
var W_STACK = NDVI_W.iterate(stack, ee.Image());
var S_STACK = NDVI_S.iterate(stack, ee.Image());
var ww = function(feature) {
return feature.set(ee.Image(W_STACK)
.slice(1)
.reduceRegion({
reducer: 'mean',
geometry: feature.geometry(),
scale: 30,
maxPixels: 1e9
}).map(function(key, value) {
return ee.Algorithms.If(value, value, 0)
}))
}
var Wresults = region.map(ww);
print('WResults', Wresults.limit(10));
var ss = function(feature) {
return feature.set(ee.Image(S_STACK)
.slice(1)
.reduceRegion({
reducer: 'mean',
geometry: feature.geometry(),
scale: 30,
maxPixels: 1e9
}).map(function(key, value) {
return ee.Algorithms.If(value, value, 0)
}))
}
var Sresults = region.map(ss);
print('SResults', Sresults.limit(10));
//******************************EXPORT AS CSV******************************************************
Export.table.toDrive({
collection: Wresults,
description: 'Winter_NDVI',
fileNamePrefix: 'Winter_NDVI_S2',
fileFormat: 'CSV'
});
Export.table.toDrive({
collection: Sresults,
description: 'Spring_NDVI',
fileNamePrefix: 'Spring_NDVI_S2',
fileFormat: 'CSV'
});