-
Notifications
You must be signed in to change notification settings - Fork 1
/
NDVI p90 & p10
145 lines (116 loc) · 6.67 KB
/
NDVI p90 & p10
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
/**** Start of imports. If edited, may not auto-convert in the playground. ****/
var JRC = ee.Image("JRC/GSW1_1/GlobalSurfaceWater"),
DEM = ee.Image("USGS/SRTMGL1_003"),
geometry =
/* color: #98ff00 */
/* shown: false */
/* displayProperties: [
{
"type": "rectangle"
},
{
"type": "rectangle"
}
] */
ee.Geometry.MultiPolygon(
[[[[-59.587254804003734, -37.151761914218],
[-59.587254804003734, -37.179052727476716],
[-59.540820401537914, -37.179052727476716],
[-59.540820401537914, -37.151761914218]]],
[[[-58.32376181477621, -36.71732838086412],
[-58.32376181477621, -36.775649841042146],
[-58.226086491289884, -36.775649841042146],
[-58.226086491289884, -36.71732838086412]]]], null, false);
/***** End of imports. If edited, may not auto-convert in the playground. *****/
//var lotes = ee.FeatureCollection(geometry2);
var now_month = ee.Number(ee.Date(Date.now()).get('month'));
var now_year = ee.Number(ee.Date(Date.now()).get('year'));
var years_max = now_year.subtract(2014);
if(now_month.lt(5)) {
years_max.subtract(1);
}
var years = ee.List.sequence(0, years_max.subtract(1));
var ndvi_pal = ['#FF0000', '#FBFF00','#357100'];
var cloudBitMask = ee.Number(2).pow(10).int();
var cirrusBitMask = ee.Number(2).pow(11).int();
function MaskS2Clouds(image) {
var qa = image.select('QA60');
var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(
qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask);
}
function addNDVI(image) {
return image.addBands(image.normalizedDifference(['B8', 'B4']).rename('NDVI'));
}
var S2Collection = ee.ImageCollection('COPERNICUS/S2').filterBounds(geometry).filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 5));//.filter(ee.Filter.calendarRange(10,01,'month'));
var S2CloudMasked = S2Collection.map(MaskS2Clouds);
var unfNDVI = S2CloudMasked.map(addNDVI).select(['NDVI']);
var NDVI_max= unfNDVI.reduce(ee.Reducer.percentile([90])).clip(geometry);
Map.addLayer(NDVI_max, {min:0.4, max:0.8, palette: ndvi_pal}, 'NDVI_p90');
Export.image.toDrive({image:NDVI_max,folder:'earth_engine',description:'NDVI_p90',scale:10,region:geometry,maxPixels:1e9,crs:'EPSG:32721'});
var campaigns = ee.ImageCollection(years.map(function (y){
var date_ini = ee.Date.fromYMD(2014,5,15).advance(y, 'year');
var date_fin = ee.Date.fromYMD(2015,5,15).advance(y, 'year');
var NDVI_p90 = unfNDVI.filterDate(date_ini,date_fin).reduce(ee.Reducer.percentile([90])).clip(geometry);
return NDVI_p90;
}));
var listOfImages = campaigns.toList(campaigns.size());
for (var i=0; i < campaigns.size().getInfo(); i++){
var img = ee.Image(listOfImages.get(i));
var campaign = (2014 + i) + "-" + (2015 + i);
Map.addLayer(img, {min:0.4, max:0.8, palette: ndvi_pal}, 'NDVI_p90_'+campaign);
Export.image.toDrive({image:img,folder:'earth_engine',description:'NDVI_p90_'+campaign,scale:10,region:geometry});
}
var NDVI_min= unfNDVI.reduce(ee.Reducer.percentile([10])).clip(geometry);
Map.addLayer(NDVI_min, {min:0, max:0.3, palette: ndvi_pal}, 'NDVI_p10');
Export.image.toDrive({image:NDVI_min,folder:'earth_engine',description:'NDVI_p10',scale:10,region:geometry});
//var batch = require('users/fitoprincipe/geetools:batch');
//batch.Download.ImageCollection.toDrive(campaigns,'earth_engine',{name:'PC_p90',scale:10,region:geometry});
//CLUSTERS
/*
var training = NDVI_max.sample({region:geometry,scale:10,factor:1});
var clusterer = ee.Clusterer.wekaKMeans(5).train(training);
var result = NDVI_max.cluster(clusterer);
Map.addLayer(result.randomVisualizer(), {}, 'clusters');
*/
//DEM
var elevation = DEM.select('elevation').clip(geometry);
var p90_lote = ee.Number(elevation.reduceRegion({reducer: ee.Reducer.percentile([90]),geometry:geometry,maxPixels:1e9,scale:30,}).get('elevation'));
var p10_lote = ee.Number(elevation.reduceRegion({reducer: ee.Reducer.percentile([10]),geometry:geometry,maxPixels:1e9,scale:30,}).get('elevation'));
var dem_pal = ['#33E6FF','#734F00'];
Map.addLayer(elevation, {min:p10_lote.getInfo(), max:p90_lote.getInfo(), palette: dem_pal}, 'DEM');
Export.image.toDrive({image:elevation,folder:'earth_engine',description: 'DEM',scale:30,region:geometry,});
//JRC
var occurrence = JRC.select('occurrence').clip(geometry);
var p90_lote = ee.Number(occurrence.reduceRegion({reducer: ee.Reducer.percentile([90]),geometry:geometry,maxPixels:1e9,scale:30}).get('occurrence'));
var p10_lote = ee.Number(occurrence.reduceRegion({reducer: ee.Reducer.percentile([10]),geometry:geometry,maxPixels:1e9,scale:30}).get('occurrence'));
var dem_pal = ['ffffff', 'ffbbbb', '0000ff'];
Map.addLayer(occurrence, {min:p10_lote.getInfo(), max:p90_lote.getInfo(), palette: dem_pal}, 'JRC');
Export.image.toDrive({image:occurrence,folder:'earth_engine',description: 'JRC',scale:30,region:geometry});
/*
var PCs_campo = ee.ImageCollection(years.map(function(y) {
var nov_ini = ee.Date.fromYMD(2014,10,15).advance(y, 'year');
var nov_fin = ee.Date.fromYMD(2014,11,15).advance(y, 'year');
var feb_ini = ee.Date.fromYMD(2015,02,01).advance(y, 'year');
var feb_fin = ee.Date.fromYMD(2015,03,01).advance(y, 'year');
var PCs_lotes = ee.ImageCollection(lotes.map(function(lote){
var nov_img = unfNDVI.filterDate(nov_ini,nov_fin).reduce(ee.Reducer.percentile([90])).clip(lote.geometry());
nov_img = ee.Image(ee.Algorithms.If(nov_img.bandNames().length().gt(0),nov_img,ee.Image(1.0).rename('NDVI_p90').clip(lote.geometry())));
var nov_val = ee.Number(nov_img.reduceRegion({reducer: ee.Reducer.mean(),geometry:lote.geometry(),maxPixels:1e9,scale:30}).get('NDVI_p90'));
nov_img = nov_img.set('fecha',nov_ini).set('valor',nov_val);
var feb_img = unfNDVI.filterDate(feb_ini,feb_fin).reduce(ee.Reducer.percentile([90])).clip(lote.geometry());
feb_img = ee.Image(ee.Algorithms.If(feb_img.bandNames().length().gt(0),feb_img,ee.Image(1.0).rename('NDVI_p90').clip(lote.geometry())));
var feb_val = ee.Number(feb_img.reduceRegion({reducer: ee.Reducer.mean(),geometry:lote.geometry(),maxPixels:1e9,scale:30}).get('NDVI_p90'));
feb_img = feb_img.set('fecha',feb_ini).set('valor',feb_val);
var out_img = ee.Algorithms.If(nov_val.gt(feb_val),nov_img,feb_img);
return out_img;
}));
var mosaic = PCs_lotes.mosaic();
return mosaic;
}));
print (PCs_campo);
var out = PCs_campo.reduce(ee.Reducer.percentile([10]));
Map.addLayer(out, {min:0.4, max:0.8, palette: ndvi_pal}, 'PC_p10');
var batch = require('users/fitoprincipe/geetools:batch')
batch.Download.ImageCollection.toDrive(PCs_campo,'earth_engine',{name:'PC_p10',scale:10,region:geometry});
*/