-
Notifications
You must be signed in to change notification settings - Fork 1
/
Field Analysis
152 lines (132 loc) · 6.15 KB
/
Field Analysis
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
var JRC = ee.Image("JRC/GSW1_1/GlobalSurfaceWater");
var DEM = ee.Image("USGS/SRTMGL1_003");
var COPERNICUS = ee.ImageCollection("COPERNICUS/S2");
var layer_list = {};
var map = ui.Map();
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(2015);
if(now_month.lt(5)) {
years_max.subtract(1);
}
var years = ee.List.sequence(0, years_max.subtract(1));
print(years);
var ndvi_pal = ['#FF0000', '#FBFF00', '#357100'];
var dem_pal = ['#33E6FF', '#734F00'];
var jrc_pal = ['ffffff', 'ffbbbb', '0000ff'];
var filtro = ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 5);
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'));
}
function analyze(geometry) {
var s2Collection = COPERNICUS.filterBounds(geometry).filter(filtro);
var s2CloudMasked = s2Collection.map(maskS2Clouds);
var unfNDVI = s2CloudMasked.map(addNDVI).select(['NDVI']);
/* NDVI P90 HISTORICO TOTAL*/
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');
layer_list.NDVI_p90 = NDVI_max.getDownloadURL({name: 'NDVI_p90', crs: 'EPSG:32721', scale: 10, region: geometry});
/* NDVI P90 POR CAMPAÑA*/
var campaigns = ee.ImageCollection(years.map(function (y){
var date_ini = ee.Date.fromYMD(2015, 5, 15).advance(y, 'year');
var date_end = ee.Date.fromYMD(2016, 5, 15).advance(y, 'year');
var NDVI_p90 = unfNDVI.filterDate(date_ini, date_end).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 = (2015 + i) + "-" + (2016 + i);
map.addLayer(img, {min: 0.4, max: 0.8, palette: ndvi_pal}, 'NDVI_p90_' + campaign);
layer_list['NDVI_p90_' + campaign] = img.getDownloadURL({name: 'NDVI_p90_' + campaign, crs: 'EPSG:32721', scale: 10, region: geometry});
//Export.image.toDrive({image: img, folder: 'earth_engine', description: 'NDVI_p90_' + campaign, scale: 10, region: geometry});
}
/* NDVI P10 HISTORICO TOTAL*/
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');
layer_list.NDVI_p10 = NDVI_min.getDownloadURL({name: 'NDVI_p10', crs: 'EPSG:32721', scale: 10, region: geometry});
//Export.image.toDrive({image: NDVI_min, folder: 'earth_engine', description: 'NDVI_p10', scale: 10, region: geometry});
/* DEM */
var elevation = DEM.select('elevation').clip(geometry);
var p90_dem = ee.Number(elevation.reduceRegion({reducer: ee.Reducer.percentile([90]), geometry: geometry, maxPixels: 1e9, scale: 30}).get('elevation'));
var p10_dem = ee.Number(elevation.reduceRegion({reducer: ee.Reducer.percentile([10]), geometry: geometry, maxPixels: 1e9, scale: 30}).get('elevation'));
map.addLayer(elevation, {min: p10_dem.getInfo(), max: p90_dem.getInfo(), palette: dem_pal}, 'DEM');
layer_list.DEM = elevation.getDownloadURL({name: 'DEM', crs: 'EPSG:32721', scale: 30, region: geometry});
//Export.image.toDrive({image: elevation, folder: 'earth_engine', description: 'DEM', scale: 30,region: geometry});
/* JRC */
var occurrence = JRC.select('occurrence').clip(geometry);
var p90_jrc = ee.Number(occurrence.reduceRegion({reducer: ee.Reducer.percentile([90]), geometry: geometry, maxPixels: 1e9, scale: 30}).get('occurrence'));
var p10_jrc = ee.Number(occurrence.reduceRegion({reducer: ee.Reducer.percentile([10]), geometry: geometry, maxPixels: 1e9, scale: 30}).get('occurrence'));
map.addLayer(occurrence, {min: p10_jrc.getInfo(), max: p90_jrc.getInfo(), palette: jrc_pal}, 'JRC');
layer_list.JRC = occurrence.getDownloadURL({name: 'JRC', crs: 'EPSG:32721', scale: 30, region: geometry});
//Export.image.toDrive({image: occurrence, folder: 'earth_engine', description: 'JRC', scale: 30,region: geometry});
select.style().set({shown: true});
select.items().reset(Object.keys(layer_list));
}
/* PANEL */
var urlLabel = ui.Label('Descargar', {shown: false});
var select = ui.Select({
placeholder: 'Seleccione una imagen',
onChange: function(layer_name) {
urlLabel.setUrl(layer_list[layer_name]);
urlLabel.style().set({shown: true});
},
style: {shown: false}
});
map.setCenter(-59,-37, 6);
map.setOptions("HYBRID");
map.drawingTools().setShown(true);
map.drawingTools().setDrawModes(['polygon','rectangle']);
var label = ui.Label('Análisis de tierras');
var input = ui.Textbox({
placeholder: 'Coordenadas',
onChange: function(str){ //-59,022503 -37,744309
try {
var arr = str.split(" ");
var x = parseFloat(arr[0].replace(',','.'));
var y = parseFloat(arr[1].replace(',','.'));
map.setCenter(x,y,18);
} catch (err) {
print('Invalid coordinates');
}
}
});
var btnClear = ui.Button({
label: 'Limpiar',
onClick: function(){
map.clear();
map.drawingTools().clear();
map.setOptions("HYBRID");
urlLabel.style().set({shown: false});
layer_list = {};
input.setValue('');
}
});
var btnAnalyze = ui.Button({
label: 'Analizar',
onClick: function(){
map.drawingTools().setSelected(null);
if (map.drawingTools().layers().length() > 0) {
map.drawingTools().layers().get(0).setShown(false);
analyze(map.drawingTools().layers().get(0).toGeometry());
} else {
print('Dibuje un poligono para analizar');
}
}
});
var panel = ui.Panel({
widgets: [label,btnAnalyze,btnClear,select,urlLabel,input],
layout: ui.Panel.Layout.flow('vertical'),
style: {position: 'bottom-left'}
});
/* UI */
ui.root.clear();
ui.root.add(panel);
ui.root.add(map);