-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelDensityMap_timeSeries.js
279 lines (240 loc) · 9.2 KB
/
ModelDensityMap_timeSeries.js
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
////////////////////////////////////////////////////////////////////////////////////////////
// Define a dictionary of layers with asset paths, and
// Map visualization parameters
////////////////////////////////////////////////////////////////////////////////////////////
var layers = {
'OVEN': [
{ year: 1985, asset: 'projects/rnationalmodel/assets/OVEN_mosaic_1985' },
{ year: 1990, asset: 'projects/rnationalmodel/assets/OVEN_mosaic_1990' },
{ year: 1995, asset: 'projects/rnationalmodel/assets/OVEN_mosaic_1995' },
{ year: 2000, asset: 'projects/rnationalmodel/assets/OVEN_mosaic_2000' },
{ year: 2005, asset: 'projects/rnationalmodel/assets/OVEN_mosaic_2005' },
{ year: 2010, asset: 'projects/rnationalmodel/assets/OVEN_mosaic_2010' },
{ year: 2015, asset: 'projects/rnationalmodel/assets/OVEN_mosaic_2015' },
{ year: 2020, asset: 'projects/rnationalmodel/assets/OVEN_mosaic_2020' }
],
'AMCR': [
{ year: 1985, asset: 'projects/rnationalmodel/assets/pred-AMCR-CAN-Mean' }
// Add other species and their layers as needed
]
};
////////////////////////////////////////////////////////////////////////////////////////////
// Map center point and zoom level
Map.setCenter(-100, 58, 3.5); // Default zoom to North America
// Change the projection to Albers Equal Area for North America (EPSG:5070)
var projection = ee.Projection('EPSG:5070');
////////////////////////////////////////////////////////////////////////////////////////////
// Panel organisation
////////////////////////////////////////////////////////////////////////////////////////////
var panel = ui.Panel({
style: { width: '400px' }
});
ui.root.add(panel);
panel.add(ui.Label({
value: 'BAM bird density model v.5 trend tool',
style: {
fontSize: '18px',
color: 'blue',
fontWeight: 'bold'
}
}));
// Variables to track active species and images globally
var activeSpecies = null;
var activeImages = null;
////////////////////////////////////////////////////////////////////////////////////////////
// Section 1 - Choose a species
// Create a list of items for the dropdown menu, displaying species codes
////////////////////////////////////////////////////////////////////////////////////////////
var title1 = ui.Label('Step 1: Select a species');
panel.add(title1);
// Dropdown menu for selecting layers
var layerSelector = ui.Select({
items: Object.keys(layers),
placeholder: 'Select a species',
onChange: function (species) {
Map.layers().reset();
activeSpecies = species;
activeImages = layers[species].map(function (layer) {
return ee.Image(layer.asset).select([0]);
});
// Add layers for the selected species to the map
layers[species].forEach(function (layer) {
var image = ee.Image(layer.asset).select([0]);
Map.addLayer(
image,
{ min: 0.0, max: 0.35, palette: ['blue', 'green', 'yellow', 'red'] },
species + ' - ' + layer.year
);
});
}
});
panel.add(layerSelector);
////////////////////////////////////////////////////////////////////////////////////////////
// Label to display clicked pixel values
var valueLabel = ui.Label('Step 2: Click on the map to generate a time series plot.');
panel.add(valueLabel);
Map.onClick(function (coords) {
if (!activeSpecies || !activeImages) {
valueLabel.setValue('Please select a species first.');
return;
}
var point = ee.Geometry.Point([coords.lon, coords.lat]);
// Check the bands in the first image for debugging
activeImages[0].bandNames().evaluate(function (bands) {
print('Bands in the first image:', bands);
});
// Extract raw pixel values from all images for debugging
var rawValues = activeImages.map(function (image) {
return image.sample({
region: point,
scale: 100,
numPixels: 1
}).first();
});
ee.FeatureCollection(rawValues).evaluate(function (result) {
print('Raw Pixel Values at Location:', result);
});
// Extract pixel values using reduceRegion
var timeSeries = activeImages.map(function (image) {
return image.reduceRegion({
reducer: ee.Reducer.first(),
geometry: point,
scale: 100,
crs: projection
}).get('b1');
});
// Extract years for the selected species
var years = layers[activeSpecies].map(function (layer) {
return layer.year;
});
// Evaluate and process the time series data
ee.List(timeSeries).evaluate(function (values) {
print('Raw Time Series Values:', values);
// Filter out null values and prepare chart data
var data = [];
years.forEach(function (year, index) {
var value = values[index] !== null ? values[index] : 0;
data.push([year.toString(), value]);
});
print('Filtered Data:', data);
print('Chart Data Format:', data);
data = ee.List(data).getInfo();
data.unshift(["Year", "Population"]);
// if (data.length === 0) {
// valueLabel.setValue('No valid data at this location.');
// return;
// }
// Create the time series chart
var chart = ui.Chart(data)
.setChartType('LineChart')
.setOptions({
title: 'Population trend model',
hAxis: {
title: 'Year',
format: '####',
slantedText: true,
slantedTextAngle: 45,
textStyle: { fontSize: 10 }
},
vAxis: { title: 'Population (per km²)' },
width: 500,
height: 300,
lineWidth: 2,
pointSize: 4
});
// Add the chart to the panel
panel.widgets().set(4, chart);
});
});
////////////////////////////////////////////////////////////////////////////////////////////
// Section 2 -
// Add the stock photo to the panel using a ui.Thumbnail
////////////////////////////////////////////////////////////////////////////////////////////
// var imageUrl = 'https://photo-api.abmi.ca/photo-api/get-profile-image?sname=Turdus%20migratorius';
// var imageLink = ui.Label({
// value: 'Click here to view the bird image',
// style: {
// fontSize: '14px',
// color: 'blue',
// textAlign: 'center',
// textDecoration: 'underline'
// }
// });
// imageLink.setUrl(imageUrl); // Direct the label to the external image URL
// panel.add(imageLink);
//////////////////////////////////////////////////////////////
var title3 = ui.Label('');
panel.add(title3);
var title4 = ui.Label('*Hint: type the location name above and fly there');
panel.add(title4);
var title5 = ui.Label('');
panel.add(title5);
//////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
// Section 3 -
// Download link 1,2,3
////////////////////////////////////////////////////////////////////////////////////////////
var title6 = ui.Label('Step 3: Download options');
panel.add(title6);
// Download link 1
var imageUrl1 = 'https://drive.google.com/drive/u/1/folders/1aJUZr4fACdD02H8AYejR2XG6zuA6E492';
var imageLink1 = ui.Label({
value: '- through Google Drive:',
style: {
fontSize: '14px',
color: 'blue',
textAlign: 'center',
textDecoration: 'underline'
}
});
imageLink1.setUrl(imageUrl1); // Direct the label to the external image URL
panel.add(imageLink1);
// Download link 2
var imageUrl2 = 'https://github.com/borealbirds/BAMexploreR';
var imageLink2 = ui.Label({
value: '- through R package (BAMexploreR):',
style: {
fontSize: '14px',
color: 'blue',
textAlign: 'center',
textDecoration: 'underline'
}
});
imageLink2.setUrl(imageUrl2); // Direct the label to the external image URL
panel.add(imageLink2);
// Download link 3
var assetNameLabel = ui.Label('Asset name: Not selected');
panel.add(assetNameLabel);
var spacing = ui.Label('');
panel.add(spacing);
// Add a copyright label at the bottom of the panel
var copyrightLabel = ui.Label({
value: 'Copyright by Boreal Avian Modelling Project (BAM)',
style: {
fontSize: '12px',
textAlign: 'right',
color: 'gray',
margin: '10px auto'
}
});
panel.add(copyrightLabel);
////////////////////////////////////////////////////////////////////////////////////////////
// Map manupulation:
// Optionally add the first layer by default (if needed)
////////////////////////////////////////////////////////////////////////////////////////////
var defaultSpeciesKey = Object.keys(layers)[0]; // Get the first species key (e.g., 'OVEN')
var defaultLayer = layers[defaultSpeciesKey][0]; // Get the first layer for the default species (1985 layer in this case)
// Add the default layer to the map
var defaultImage = ee.Image(defaultLayer.asset).select([0]); // Load the default raster layer
Map.addLayer(
defaultImage,
{
min: 0.0, // Adjust the minimum value for visualization
max: 0.35, // Adjust the maximum value for visualization
palette: ['blue', 'green', 'yellow', 'red'], // Set a default palette
crs: projection.crs(),
crsTransform: projection.transform()
},
defaultSpeciesKey + ' - ' + defaultLayer.year
);
////////////////////////////////////////////////////////////////////////////////////////////