-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSatelliteImageDate
208 lines (171 loc) · 5.43 KB
/
SatelliteImageDate
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Test mapping function
//
// area
// fileID, satID, timestamp
// The Hawaiian Islands
//var geom = ee.Geometry.Point(Map.getCenter().coordinates().get(0), Map.getCenter().coordinates().get(1))
/*
Declaration of all variables to be used
which can be initialized and cleared
*/
var HotMap = function(){
this.name = "HotMap";
//UI components
this.dateBoxStart = null;
this.dateBoxEnd = null;
this.reRunAnalysis = null;
this.addPrecipitation = null;
//Object Variables
this.startDate = null;
this.endDate = null;
this.ic = null;
this.init();
};
HotMap.prototype.init = function(){
/*
Generates UI components as global variables
*/
var thisObject = this;
// date textbox
this.dateBoxStart = ui.Textbox({
placeholder: 'Enter start date (YYYY-MM-DD)',
onChange: function(){thisObject.startDateChanged()}
});
this.dateBoxEnd = ui.Textbox({
placeholder: 'Enter end date (YYYY-MM-DD)',
onChange: function(){thisObject.endDateChanged()}
});
//Rerun for selection
this.reRunAnalysis = ui.Button({
label: 'Re-Run Analysis',
onClick: function(target){thisObject.onReRunClick()}
});
this.addPrecipitation = ui.Button({
label: 'Add Precipitation',
onClick: function(target){thisObject.addPrecipitationLayer(thisObject.startDate, thisObject.endDate)}
});
/*
End of global ui components
*/
}
HotMap.prototype.show = function(){
this.setupSidePannel();
//this.onReRunClick();
}
/*
*********************** Handlers of UI call back functions ***********************
*/
HotMap.prototype.startDateChanged = function(date){
print(date)
this.startDate = date;
}
HotMap.prototype.endDateChanged = function(date){
print(date)
this.endDate = date;
}
HotMap.prototype.onReRunClick = function(){
var geom = Map.getCenter();
// Landsat 8 collection
var ic = ee.ImageCollection('LANDSAT/LC8_L1T_TOA')
.filterBounds(geom)
//.filterDate('2016-01-01','2016-01-5')
.select(['B2','B3','B4','B5','B6','B7'],['blue','green','red','nir','swir1','swir2']);
if(this.startDate != null && this.startDate != ''){
ic = ic.filterDate(this.startDate)
}else{
ic = ic.sort('system:time-start')
}
// SINGLE IMAGE
var img = ee.Image(ic.first())
Map.addLayer(img.select(['swir1','swir2','nir']),{min:0,max:0.4},'infrared')
var hot = this.runHotMapAnalysis(img)
Map.addLayer(hot,{color:'FF0000'},'hot_vectors')
}
/*
*********************** END ***********************
*/
// Generates the side pannel
HotMap.prototype.setupSidePannel = function(){
// PANELS
// ********************************************************************************************
// main panel
var panel = ui.Panel({style: {width: '400px'}})
panel.widgets().set(0,ui.Label('Controls',{fontWeight:'bold'}))
panel.widgets().set(1,this.dateBoxStart)
panel.widgets().set(2,this.dateBoxEnd)
panel.widgets().set(3,this.reRunAnalysis)
panel.widgets().set(4, this.addPrecipitation)
ui.root.add(panel);
}
/**
Fancy pants function to do the actual SCIENCE!!!
*/
// HOTMAP detection (returns hot pixel clusters, i.e. a feature collection per image)
HotMap.prototype.runHotMapAnalysis = function(img) {
// wavebands
var n = img.select('nir')
var s1 = img.select('swir1')
var s2 = img.select('swir2')
// alpha detection
var a1 = s2.divide(s1).gt(1.4)
var a2 = s2.divide(n).gt(1.4)
var a3 = s2.gt(0.15)
var alpha = a1.and(a2).and(a3).rename('alpha')
// beta detection
var b1 = s1.divide(n).gt(2)
var b2 = s1.gt(0.5)
var beta = b1.and(b2).rename('beta')
// potential hot pixels
var potentialHot = alpha.or(beta)
var hotPixels = potentialHot.updateMask(potentialHot)
// hot pixel clusters (count alpha pixels)
var hotClusters = hotPixels.addBands(alpha).reduceToVectors({
eightConnected:true,
maxPixels:2e8,//might seem like a lot but MOST pixels are masked
reducer:ee.Reducer.count()
})
// hot pixel clusters must contain alpha pixels
var validHotClusters = hotClusters.filter(ee.Filter.gt('count', 0));
// Image properties
var fileID = img.get('system:index')
var satID = ee.String(fileID).slice(0,3)
var timestamp = img.get('system:time_start')
// Assign properties
var clusters = validHotClusters.map(function(feature) {
var clusterGeom = feature.geometry()
var clusterArea = feature.geometry().area(1) // i.e. to within 1 m2
var cluster = ee.Feature(clusterGeom,{
fileID:fileID,
satID:satID,
timestamp:timestamp,
area:clusterArea
})
return cluster
})
return clusters
}
/**
Grabs precipitation data and plots it on map
**/
// function addPrecipitationLayer(dateStart, dateEnd)
// Parameters:
// string dateStart, indicating start of data range (e.g. '2017-03-01')
// string dateEnd, indicating end of datarange (e.g. '2017-03-27')
// Return values:
// like any good function, this function returns void and just has tons of side effects
HotMap.prototype.addPrecipitationLayer = function(dateStart, dateEnd) {
var s = dateStart;
var e = dateEnd;
if(s == null){
s = '2017-03-01';
e = '2017-03-27';
}
this.ic = ee.ImageCollection('UCSB-CHG/CHIRPS/PENTAD')
//.filterDate('2017-03-01', '2017-03-27');
.filterDate(s, e);
var meanPrecip = this.ic.reduce(ee.Reducer.mean());
Map.addLayer(meanPrecip, {min:0,max:10, palette: ['FF0000', '0000FF']}, 'precipitation', true, 0.40);
}
// declare the hot object to be used
var hot = new HotMap();
hot.show();