-
Notifications
You must be signed in to change notification settings - Fork 1
/
geeEMSMonitor-code.js
1281 lines (1123 loc) · 45.8 KB
/
geeEMSMonitor-code.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Google Earth Engine App for EMSAfrica
Created by Paul Renner 2022
Friedrich Schiller University Jena
Citations
Braaten, Justin (2021): Landsat Time Series Explorer
(https://github.com/jdbcode/ee-rgb-timeseries/blob/main/landsat-timeseries-explorer.js)
Multiple Contributors (2020): Image mosaic/composite creation for Landsat and Sentinel-2 in Google Earth Engine - Cloudmask
(https://open-mrv.readthedocs.io/en/latest/image_composite_Web.html)
Principe, Rodrigo E. (2019): GEEtools-code-editor Tools
(https://github.com/fitoprincipe/geetools-code-editor/blob/51fa835dfaabe73fa238fe1f3ce0c98118ded244/_tools/map)
Staridas Geography (2020): Hillshade
(https://code.earthengine.google.com/d136f0a45cee76a89ed45384dc376793 from https://www.staridasgeography.gr/)
Acknowledgment
Funded by the Federal Ministry of Education and Research (BMBF)
as part of EMSAfrica within SPACES II.
*/
// #################################
// ########## IMPORT ##########
// #################################
var s2sr = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED"),
dtm1m = ee.Image("users/prenner_eo/KNP_dtm_1m"),
dsm1m = ee.Image("users/prenner_eo/KNP_dsm_1m"),
GSWM = ee.Image("JRC/GSW1_3/GlobalSurfaceWater"),
knparea = ee.FeatureCollection("users/prenner_eo/KNP_Area");
// #################################
// ########## PARAMETER ##########
// #################################
// URL
var initRun = 'false';
var runUrl = ui.url.get('run', initRun);
ui.url.set('run', runUrl);
var initLon = 30.82863920230089;
var lonUrl = ui.url.get('lon', initLon);
ui.url.set('lon', lonUrl);
var initLat = -25.00466212563505;
var latUrl = ui.url.get('lat', initLat);
ui.url.set('lat', latUrl);
var initStart = 2019;
var startUrl = ui.url.get('start', initStart);
ui.url.set('start', startUrl);
var initEnd = 2022;
var endUrl = ui.url.get('end', initEnd);
ui.url.set('end', endUrl);
//var initIndex = 'RGB';
var initIndex = 'Plant Health (NDVI)';
var indexUrl = ui.url.get('index', initIndex);
ui.url.set('index', indexUrl);
// ROI
var COORDS = null;
var CLICKED = false;
var visParams = null;
// #################################
// ########## UI ELEMENTS ##########
// #################################
// COLORS
// Background Colors
var coloryear = '#E7FFD6'; //'#E0FFCC';
var colorindices = '#CCF2B3'; //'#FFEDC6';
var colorexternal = '#9FD17D'; //'#FFE1E1';
// Shape Colors
var AOI_COLOR = 'ffffff';
// STYLE & FONT
// Titel
var titelFont = {fontSize: '18px', fontWeight: 'bold', margin: '4px 8px 4px 8px', stretch: 'horizontal', textAlign: 'center'};
// Header
var headerFont = {fontSize: '13px', fontWeight: 'bold', margin: '4px 8px 4px 8px'};
var headerFontyear = {fontSize: '13px', fontWeight: 'bold', margin: '4px 8px 4px 8px', backgroundColor: coloryear};
var headerFontindex = {fontSize: '13px', fontWeight: 'bold', margin: '4px 8px 4px 8px', backgroundColor: colorindices};
var headerFontexternal = {fontSize: '13px', fontWeight: 'bold', margin: '4px 8px 4px 8px', backgroundColor: colorexternal};
// Text
var textFont = {fontSize: '12px', whiteSpace: 'pre'};
var textFontyellow = {fontSize: '12px', whiteSpace: 'pre', backgroundColor: '#FFFF00'};
var textFontcenter = {fontSize: '12px', whiteSpace: 'pre', textAlign: 'center'};
var textFontyear = {fontSize: '12px', backgroundColor: coloryear, whiteSpace: 'pre', margin: '0px 8px 0px 8px'};
var textFontexternal = {fontSize: '12px', backgroundColor: colorexternal, whiteSpace: 'pre', margin: '0px 8px 2px 8px'};
// Info
var infoFontyear = {fontSize: '12px', backgroundColor: coloryear, textAlign: 'justify'};
var infoFonttext = {fontSize: '12px', margin:'0px 16px 0px 16px', textAlign: 'justify'};
var infoFontindex = {fontSize: '12px', textAlign: 'justify'};
var infoFont = {fontSize: '12px', whiteSpace: 'pre', textAlign: 'justify'};
// Button
var buttonStyle = {fontSize: '18px', fontWeight: 'bold', margin: '4px 8px 4px 8px', stretch: 'horizontal', textAlign: 'center'};
// Hyperlinks
var linkstyleexternal = {fontSize: '12px', stretch: 'horizontal', margin: '0px 8px 10px 8px', color: 'darkblue', backgroundColor: colorexternal};
var githubwikilink = {fontSize: '16px', margin: '4px 8px 8px 8px', stretch: 'horizontal', color: 'darkblue', textAlign: 'center'};
var githublink = {fontSize: '12px', margin: '4px 8px 8px 8px', stretch: 'horizontal', color: 'darkblue', textAlign: 'center'};
// PANEL
var settingPanel = ui.Panel({
style: {position: 'top-left', width: '280px'}});
var galleryPanel = ui.Panel({
layout: ui.Panel.Layout.flow('horizontal', true),
style: {position: 'top-right', height: '60%'}});
var chartPanel = ui.Panel({
style: {position: 'bottom-right'}});
var headPanel = ui.Panel({
style: {backgroundColor: '#FFFFFF'}
});
var yearPanel = ui.Panel({
style: {backgroundColor: coloryear, margin:'6px'}
});
var indicesPanel = ui.Panel({
style: {backgroundColor: colorindices, margin:'6px'}
});
var externaldata = ui.Panel({style: {backgroundColor: colorexternal, margin:'6px'}});
var map = ui.Map();
var gallerychartPanel = ui.Panel(ui.SplitPanel({
firstPanel: galleryPanel,
secondPanel: chartPanel,
orientation: 'vertical'}));
var splitMapGallery = ui.Panel(ui.SplitPanel({
firstPanel: map,
secondPanel: gallerychartPanel,
orientation: 'horizontal'}));
var splitPanel = ui.SplitPanel(settingPanel,splitMapGallery);
var waitMsgImgPanel = ui.Label({
value: '⚙️' + ' Processing, please wait.',
style: {
stretch: 'horizontal',
textAlign: 'center',
backgroundColor: '#FFFFFF'
}});
var clickmapbox = ui.Label({
value: 'Click on the map at your point of interest to get started.',
style: {
stretch: 'horizontal',
position: 'top-center',
backgroundColor: '#FFFF00'
}});
// Index Library
// Indices 'IndexSelectName': [calculate index, visParams for GalleryView, chart true or false, description of index, short info]
var indices = {
'Visible Light (RGB)': [{bands: ['B4', 'B3', 'B2'], min:0, max:3000,},
false,
null,
'The true color or ‘RGB’ composite uses visible light bands red, green and blue in a combination, resulting in a naturally colored image. This composite represents a good representation of the Earth as humans would see it naturally.'],
// without Chart
'Agriculture': [{bands: ['B11', 'B8', 'B2'], min:0, max:3000,},
false,
null,
'The agriculture band combination uses SWIR-1 (B11), near-infrared (B8), and blue (B2). It’s mostly used to monitor the health of crops because of how it uses short-wave and near-infrared. Both these bands are particularly good at highlighting dense vegetation that appears as dark green.'],
'Plant Health (NDVI)': [{bands: ['NDVI'], min: -0.5, max: 1, palette: ['C4022F','FF7847','F7FFAD','8BCC68','066634']},
true,
'NDVI',
'The Normalized Difference Vegetation Index (NDVI) is used to separate states of vegetation health. As greater chlorophyll content (greater NDVI value) leads to increased reflection of near-infrared (NIR) and green wavelengths, the NDVI can monitor important parameters such as water stress in plants.',
'water stress in plants'],
'Burnt Area (NBR)': [{bands: ['NBR'], min: -0.5, max: 1, palette: ['red', 'F5ECBC', 'green']},
true,
'NBR',
'The NBR is used to identify burned areas and provide a measure of burn severity. It is calculated as a ratio between the NIR and SWIR values. ',
''
],
'Vegetation Moisture (NDMI)' :[{bands: ['NDMI'], min: -0.5, max: 0.5, palette: ['FFE3CF','FFE6EB','C9BFFF','7581FF','0F23FB']},
true,
'NDMI',
'The NDMI is sensitive to moisture levels in vegetation. It is used to monitor droughts as well as monitor fuel levels in fire-prone areas. It uses NIR and SWIR bands to create a ratio designed to mitigate illumination and atmospheric effects.',
''
],
'Bare Soil Adjusted Vegetation (MSAVI)': [{bands: ['MSAVI'], min: -1, max: 1, palette: ['174499','4AA0D9','D9EDED','44B86E','378C31']},
true,
'MSAVI',
'The MSAVI is an index designed to provide accurate data due to low vegetation or a lack of chlorophyll in the plants. It reduces the effect of the soil on the calculation of vegetation density in the field.',
''
],
// '(S2REP)': [{bands: ['S2REP'], min: 600, max: 800, palette: ['C4022F','FF7847','F7FFAD','8BCC68','066634']},
// true,
// 'S2REP'],
'Bare Soil (BSI)': [{bands: ['BSI'], min: -0.5, max: 0.5, palette: ['004A11','3D8549','F7FFAD','FF7847','FF0800']},
true,
'BSI',
'The BSI shows all vegetation in green and barren ground in red colors. Water appears black. This index helps to discriminate the status of crops (growing, not yet growing), detect recent deforestation and monitor droughts. It can also be used to detect landslides or determine the extent of erosion in non-vegetated areas.',
''
],
};
// TEXT ####################
// App Name
var appname = ui.Label(
'EMSAfrica Monitor',
titelFont, 'https://www.emsafrica.org/');
// Info Button
var introtext = ui.Label({
value: 'The EMSAfrica Monitor aims to provide easy-to-understand information for decision-making ranging from academic to governmental levels. This open-source tool offers you the possibility to analyze time series metrics derived from multi-temporal Sentinel data. You can calculate indices, investigate monthly composites, import additional data sets and learn about new remote sensing products that are relevant to your area of interest.',
style: infoFonttext
});
var infobutton = ui.Label({
value: 'About this App (GitHub Wiki)', style: githubwikilink, targetUrl: 'https://github.com/prenner-eo/EMSAfricaMonitor/wiki/EMSAfrica-Monitor'
});
var sourceinfo = ui.Label({
value: 'This GEE App was created by\nPaul Renner (FSU Jena) &\nKai Heckel (FSU Jena, Supervision)\nto contribute to the SPACES II project EMSAfrica.',
style: infoFont
});
var sourcebutton1 = ui.Label({
value: 'Source Code (GitHub)', style: githublink, targetUrl: 'https://github.com/prenner-eo/EMSAfricaMonitor/blob/main/geeEMSMonitor-code.js'
});
var sourcebutton2 = ui.Label({
value: 'Source Code (GEE Editor)', style: githublink, targetUrl: 'https://code.earthengine.google.com/4554dbc236b75b9284809e12e2ee3f09'
});
var sourcebutton = ui.Panel({
widgets: [sourcebutton1, sourcebutton2],
layout: ui.Panel.Layout.flow('horizontal', true)
});
var infoIndices = ui.Label(
indices[ui.url.get('index')][3],
infoFontindex);
// YEAR
// Date
var yearhead = ui.Label(
'Select duration of interest:',
headerFontyear);
var yearinfo =ui.Label(
'(Dataset temporal availability: 2017-today) Earliest possible start year in Southern Africa is 2019 due to the selected Earth Engine image collection and its coverage.',
infoFontyear);
var firstdatetext = ui.Label({
value: 'Start Year (YYYY)',
style: textFontyear});
var seconddatetext = ui.Label({
value: 'End Year (YYYY)',
style: textFontyear});
var firstdatepick = ui.Textbox({
placeholder: ui.url.get('start'),
value: ui.url.get('start'),
style: {maxWidth: '90px'}});
var seconddatepick = ui.Textbox({
placeholder: ui.url.get('end'),
value: ui.url.get('end'),
style: {maxWidth: '90px'}});
var months = ee.List.sequence(1, 12);
var years = ee.List.sequence(ui.url.get('start'),ui.url.get('end'));
var datepick = ui.Panel({widgets:[firstdatetext, seconddatetext, firstdatepick, seconddatepick],
layout: ui.Panel.Layout.flow('horizontal', true),
style: {backgroundColor: coloryear}
});
// Submit Button
var submitbutton = ui.Button({
label: 'Submit Years', style: buttonStyle
});
// INDICES
var indexhead = ui.Label(
'Choose an index from the list:',
headerFontindex);
// Drop Down Menu
var indexselect = ui.Select({
items: Object.keys(indices),
//placeholder: ui.url.get('index'),
value: ui.url.get('index'),
style: buttonStyle,
onChange: function(key){
infoIndices.setValue(indices[key][3]);
handleSubmit();
}
});
// EMSAfirca DATA
var emsdataheadline = ui.Label({
value: 'EMSAfrica Data to view:',
style: headerFontexternal});
// DMC
var linkdmc = ui.Label('More information in the related publication.', linkstyleexternal,'https://koedoe.co.za/index.php/koedoe/article/view/1679/2919');
var linkdmcdownload = ui.Label('Download the data here.', linkstyleexternal,'http://dx.doi.org/10.5285/deab4235f1ef4cd79b73d0cbf2655bd7');
var headdmc = ui.Label('Digital Mapping Camera (DMC) Products\n(only available for Kruger National Park, SA)\nwith 1 meter resolution:', textFontexternal);
var button2 = ui.Button({
label: 'DMC Digital Surface Model (DSM)', style: buttonStyle
});
var button3 = ui.Button({
label: 'DMC Digital Terrain Model (DTM)', style: buttonStyle
});
// EXTERNAL DATA
var externalheadline = ui.Label({
value: 'External data to add to the map:',
style: headerFontexternal});
// ESA World Cover
var headesaworldcover = ui.Label('Landcover Product 2020 10m (ESA)', textFontexternal);
var buttonesaworldcover = ui.Button({
label: 'World Cover', style: buttonStyle
});
var sourceesaworldcover = ui.Label('Source: ESA 2020' ,linkstyleexternal, 'https://esa-worldcover.org/en');
// South African National Land Cover
var headsanlc = ui.Label('South African National Land Cover 2018 (DFFE, South Africa)', textFontexternal);
var buttonsanlc = ui.Button({
label: 'South African Land Cover', style: buttonStyle
});
var sourcesanlc = ui.Label('Source: DFFE, South Africa 2018' ,linkstyleexternal, 'https://www.dffe.gov.za/projectsprogrammes/egis_landcover_datasets');
// Terra Vegetation Continuous Fields Yearly Global 250m
var headTerraVeg = ui.Label('Tree Cover 2020 250m (Terra MODIS)', textFontexternal);
var buttonTerraVeg = ui.Button({
label: 'Tree Cover', style: buttonStyle
});
var sourceTerraVeg = ui.Label('Source: NASA 2020',linkstyleexternal, 'https://lpdaac.usgs.gov/products/mod44bv006/');
// RESOLVE Ecoregions 2017
var headEcoregion = ui.Label('Ecoregions 2017 (Resolve)', textFontexternal);
var buttonEcoregion = ui.Button({
label: 'Ecoregions', style: buttonStyle
});
var sourceEcoregion = ui.Label('Source: Resolve (2017)',linkstyleexternal, 'https://ecoregions.appspot.com/');
// World Settlement Footprint 2015
var headSettlement = ui.Label('World Settlement Footprint 2015 10m (DLR)', textFontexternal);
var buttonSettlement = ui.Button({
label: 'World Settlement Footprint', style: buttonStyle
});
var sourceSettlement = ui.Label('Source: DLR (2020)',linkstyleexternal, 'https://www.dlr.de/blogs/en/all-blog-posts/world-settlement-footprint-where-do-humans-live.aspx');
// SRTM
var headSRTM = ui.Label('SRTM Digital Elevation Model 2000 30m (NASA)', textFontexternal);
var buttonSRTM = ui.Button({
label: 'SRTM DEM', style: buttonStyle
});
var sourceSRTM = ui.Label('Source: NASA (2013)',linkstyleexternal, 'https://cmr.earthdata.nasa.gov/search/concepts/C1000000240-LPDAAC_ECS.html');
// CHART & GALLERY PANEL
var infotextslider = ui.Label({
value: "If you don't see the message above the slider, please pull the slider down.",
style: textFontyellow
});
var infotextchart = ui.Label({
value: '\n\n\n\n Here the index chart will be displayed once you click the submit years button or click the map.',
style: textFont
});
var infotextgallery = ui.Label({
value: "Here the gallery view will be displayed once you click the submit years button or click the map.\n\n\n\nHow to use the EMSAfrica Monitor?\n\nClick on the map to define your point of interest.\nDefine the years you are interested in.\nChoose an index from the list.\nYou can also view EMSAfrica products and external datasets.",
style: textFont
});
var headlineindexinfo = ui.Label({
value: 'Index Information:',
style: headerFont
});
// #################################
// ########## FUNCTIONS ##########
// #################################
// MAP FUNCTIONS
function clearImgs() {
galleryPanel.clear();
}
// Create S2 Monthly Composite per year Time Series
function monthlycomposite(region,years,months) {
var col = s2sr.filterBounds(region);
// add all Indices to the composite
// NDVI Function
var addNDVI = function(image) {
var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
return image.addBands(ndvi);
};
// NBR Function
var addNBR = function(image) {
var nbr = image.normalizedDifference(['B8', 'B12']).rename('NBR');
return image.addBands(nbr);
};
// NDMI Function
var addNDMI = function(image) {
var ndmi = image.normalizedDifference(['B8', 'B11']).rename('NDMI');
return image.addBands(ndmi);
};
// MSAVI Function
var addMSAVI = function(image) {
var msavi = image.select('B8').multiply(2).add(1)
.subtract(image.select('B8').multiply(2).add(1).pow(2)
.subtract(image.select('B8').subtract(image.select('B4')).multiply(8)).sqrt()
).divide(2).rename('MSAVI');
return image.addBands(msavi);
};
// S2REP Function
var addS2REP = function(image) {
var s2rep = image.expression('705 + 35*((RED+ VNIR3)/2-VNIR)/(VNIR2-VNIR)',{
'RED': image.select('B4'),
'VNIR': image.select('B5'),
'VNIR2': image.select('B6'),
'VNIR3': image.select('B7')
}).rename('S2REP');
return image.addBands(s2rep);
};
// BSI Function
var addBSI = function(image) {
var bsi = image.expression(
'(( X + Y ) - (A + B)) /(( X + Y ) + (A + B)) ', {
'X': image.select('B11'), //swir1
'Y': image.select('B4'), //red
'A': image.select('B8'), // nir
'B': image.select('B2'), // blue
}).rename('BSI');
return image.addBands(bsi);
};
var monthlyImages = ee.ImageCollection.fromImages(
years.map(function (y) {
return months.map(function(m){
var w = col.filter(ee.Filter.calendarRange(y, y, 'year'))
.filter(ee.Filter.calendarRange(m, m, 'month'))
.filterMetadata('CLOUDY_PIXEL_PERCENTAGE',
'less_than',10)
.map(maskS2srCloudsgallery)
.map(addNDVI)
.map(addNBR)
.map(addNDMI)
.map(addMSAVI)
//.map(addS2REP)
.map(addBSI)
.mean()
//.median()
//.min()
.set('system:time_start',ee.Date.fromYMD(y,m,1));
return w.set('year', y)
.set('month', m)
.set('date', ee.Date.fromYMD(y,m,1).format('YYYY MM'))
.set('system:time_start',ee.Date.fromYMD(y,m,1));
});
}).flatten());
return monthlyImages;
}
// Source: https://open-mrv.readthedocs.io/en/latest/image_composite_Web.html
function maskS2srClouds(image) {
var qa = image.select('QA60');
// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000);
}
function maskS2srCloudsgallery(image) {
var qa = image.select('QA60');
// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask);
}
// print('Sentinel-2 Filtered collection',s2Filt);
// Create Time Series Gallery View
// modified from Braaten 2021
function displayBrowseImg(col, aoiBox, aoiCircle,visParams) {
clearImgs();
waitMsgImgPanel.style().set('shown', true);
galleryPanel.add(waitMsgImgPanel);
//print(visParams);
var dates = col.aggregate_array('date');
//print(dates);
dates.evaluate(function(dates) {
waitMsgImgPanel.style().set('shown', false);
dates.forEach(function(date) {
var img = col.filter(ee.Filter.eq('date', date)).first();
var aoiImg = ee.Image().byte()
.paint(ee.FeatureCollection(ee.Feature(aoiCircle)), 1, 2)
.visualize({palette: AOI_COLOR});
var thumbnail = ui.Thumbnail({
image: img.visualize(visParams).blend(aoiImg),
params: {
region: aoiBox,
dimensions: '200',
crs: 'EPSG:3857',
format: 'PNG'
}
});
var monthname = { '01' : ['January'], '02' : ['February'], '03' : ['March'],
'04' : ['April'], '05' : ['May'], '06' : ['June'],
'07' : ['July'], '08' : ['August'], '09' : ['September'],
'10' : ['October'], '11' : ['November'], '12' : ['December']};
date = monthname[date.slice(-2)][0] +' '+ date.slice(0,4);
var bandcountEmpty = img.bandNames().length().eq(ee.Number(0));
var imgCard = ui.Panel([
ui.Label(date,
{margin: '4px 4px -6px 8px', fontSize: '13px', fontWeight: 'bold'}),
thumbnail
], null, {margin: '4px 0px 0px 4px' , width: 'px'});
galleryPanel.add(imgCard);
bandcountEmpty.getInfo(function(ifso){
if (ifso) {
imgCard.insert(1, ui.Label(
{value: "There is no cloud-free image data available for this month.",
style: {height: '200px',width: '200px', textAlign: 'center', position: 'middle-left'}}))
.remove(thumbnail);
}
});
});
});
}
// Remove Layers from the map by name (Source: https://github.com/fitoprincipe/geetools-code-editor/blob/51fa835dfaabe73fa238fe1f3ce0c98118ded244/_tools/map)
function removeLayerByName(name, map) {
var m = map || Map;
var layers = m.layers();
// list of layers names
var names = [];
layers.forEach(function(lay) {
var lay_name = lay.getName();
names.push(lay_name);
});
// get index
var index = names.indexOf(name);
if (index > -1) {
// if name in names
var layer = layers.get(index);
m.remove(layer);
}
}
function removePoint(){
removeLayerByName('Sentinel-2 latest composite',map);
removeLayerByName('Point of Interest',map);
}
function removeAllLayer(){
removeLayerByName('Surface Water',map);
removeLayerByName('Elevation DSM 1m',map);
removeLayerByName('Hillshade DSM 1m',map);
removeLayerByName('Elevation DTM 1m',map);
removeLayerByName('Hillshade DTM 1m',map);
removeLayerByName('Landcover 2020 10m',map);
removeLayerByName('Tree Cover 2020 250m',map);
removeLayerByName('Ecoregions 2017 250m',map);
removeLayerByName('Human settlement areas 2015 10m',map);
removeLayerByName('Background (Human settlement areas 2015)',map);
removeLayerByName('SRTM Digital Elevation Model', map);
removeLayerByName('Hillshade', map);
removeLayerByName('Surface Water', map);
removeLayerByName('South African National Land Cover 2018', map);
}
// ROI Points and AOI Circle Define by Map Click Function
// modified from Braaten 2021
function renderGraphics(coords,years,months,visParams) {
// Get the clicked point and buffer it.
var point = ee.Geometry.Point(coords);
var aoiCircle = point.buffer(50);
var aoiBox = point.buffer(500);
// Clear previous point from the Map.
removePoint();
var endDate = ee.Date(Date.now());
var startDate = endDate.advance(-1, 'month');
// Filter Sentinel-2 collection
var s2Filt = s2sr.filterBounds(aoiBox)
.filterDate(startDate,endDate)
.filterMetadata('CLOUDY_PIXEL_PERCENTAGE',
'less_than',50)
.map(maskS2srClouds);
var s2composite = s2Filt.median(); // can be changed to mean, min, etc
// Add composite to map
map.addLayer(s2composite,{bands:['B4','B3','B2'],min:0.02,max:0.3,
gamma:1.5},'Sentinel-2 latest composite');
// Add new point to the Map.
map.addLayer(aoiCircle, {color: AOI_COLOR}, 'Point of Interest');
map.centerObject(aoiCircle, 14);
// Load Gallery
var monthlys2 = monthlycomposite(point,years,months);
displayBrowseImg(monthlys2, aoiBox, aoiCircle,visParams);
//Chart
if (indices[indexselect.getValue()][1]===true){
// Make a chart if index values are available.
var chart = ui.Chart.image.series({
imageCollection: monthlys2.select(indices[indexselect.getValue()][2]),
region: aoiCircle,
reducer: ee.Reducer.mean(),
scale: 30
});
// Color Gradient
var rgbColors = 'palette:["C4022F","FF7847","F7FFAD","8BCC68","066634"]';
rgbColors = rgbColors.slice(8);
//print(rgbColors)
// Design Chart
var options = {
title: indexselect.getValue()+' indicates '+ indices[indexselect.getValue()][4]+' over time',
hAxis: { title: 'Month' },
vAxis: { title: indexselect.getValue()},
// ToDo: Insert change and fitted line
series: {
0:{
visibleInLegend: false,
type: 'scatter',
}},
//legend: {position: 'none'},
trendlines: {
0:{ // add a trend line,
type: 'linear',
color: 'black',
lineWidth: 2,
opacity: 0.5,
labelInLegend: 'Trend',
visibleInLegend: true,
}},
tooltip: {trigger: 'selection'}
};
// Set values to points
chart = chart.setOptions(options);
chartPanel.clear();
chartPanel.add(chart);
chartPanel.add(headlineindexinfo);
chartPanel.add(infoIndices);
}
else{
chartPanel.clear();
chartPanel.add(ui.Label({value: 'For this index there is no chart available.',
style: {stretch: 'horizontal', textAlign: 'center'}}));
chartPanel.add(headlineindexinfo);
chartPanel.add(infoIndices);
}
}
// modified from Braaten 2021
function handleMapClick(coords) {
CLICKED = true;
COORDS = [coords.lon, coords.lat];
map.remove(clickmapbox);
ui.url.set('run', 'true');
ui.url.set('lon', COORDS[0]);
ui.url.set('lat', COORDS[1]);
visParams=indices[indexselect.getValue()][0];
years = ee.List.sequence(ui.url.get('start'),ui.url.get('end'));
//print(COORDS);
renderGraphics(COORDS,years,months,visParams);
}
function handleSubmit(){
CLICKED = true;
map.remove(clickmapbox);
ui.url.set('run', 'true');
// Set new years
ui.url.set('start', firstdatepick.getValue());
ui.url.set('end', seconddatepick.getValue());
ui.url.set('index', indexselect.getValue());
years = ee.List.sequence(ui.url.get('start'),ui.url.get('end'));
// Set new Index
// read coordinates and process
COORDS = [ui.url.get('lon'), ui.url.get('lat')];
visParams=indices[indexselect.getValue()][0];
//print(COORDS);
renderGraphics(COORDS,years,months,visParams);
}
// #################################
// ########## EXTERNAL DATA ########
// #################################
// Legend Panel
var legendPanel = ui.Panel({
style: {
position: 'bottom-left',
padding: '5px;',
maxHeight: '30%'
}
});
var legendTitle = ui.Label({
value: 'Legend',
style: {
fontSize: '14px',
fontWeight: 'bold',
margin: '0px;'
}
});
// DMC Data
// Generating Hillshade for DMC Data code snippets from
// eARTh Engine: Turn cold pixels to a colorful Terrain
// https://code.earthengine.google.com/d136f0a45cee76a89ed45384dc376793
function demshade (image){
var N = ee.Terrain.hillshade(image,0,36).multiply(0);
var NE = ee.Terrain.hillshade(image,45,44).multiply(0);
var E = ee.Terrain.hillshade(image,90,56).multiply(0);
var SE = ee.Terrain.hillshade(image,135,68).multiply(0);
var S = ee.Terrain.hillshade(image,180,80).multiply(0.1);
var SW = ee.Terrain.hillshade(image,225,68).multiply(0.2);
var W = ee.Terrain.hillshade(image,270,56).multiply(0.2);
var NW = ee.Terrain.hillshade(image,315,44).multiply(0.5);
var MULTI = N
.add(NE)
.add(E)
.add(SE)
.add(S)
.add(SW)
.add(W)
.add(NW)
.visualize({
min:0,
max:255,
palette:['#000000','#ffffff'],
})
.resample('bicubic')
.updateMask(0.5);
var SLOPE = ee.Terrain.slope(image)
.multiply(2)
.visualize({
min:100,
max:180,
palette:['#ffffff','#000000']
})
.resample('bicubic')
.updateMask(1);
var SHADED_RELIEF = ee.ImageCollection([
SLOPE,
MULTI
])
.mosaic()
.reduce(ee.Reducer.median())
.updateMask(1);
return SHADED_RELIEF;
}
var SURFACE_WATER = GSWM
.visualize({
bands:['occurrence'],
min:0,
max:100,
palette:[
'#B9E9E7'
]
})
.resample('bicubic')
.clip(knparea);
var visdmc = {
min: 0,
max: 750,
palette: ['0000ff', '00ffff', 'ffff00', 'ff0000'],
};
//LEGEND
// Creates a color bar thumbnail image for use in legend from the given color palette
function makeColorBarParams(palette) {
return {
bbox: [0, 0, 1, 0.1],
dimensions: '100x10',
format: 'png',
min: 0,
max: 1,
palette: palette,
};
}
function adddmclegend(){
// Create Legend for DMC
var dmcpalette = ['0000ff', '00ffff', 'ffff00', 'ff0000'];
var visdmc = {min: 0, max: 750, palette: dmcpalette};
// Create the colour bar for the legend
var colorBar = ui.Thumbnail({
image: ee.Image.pixelLonLat().select(0),
params: makeColorBarParams(visdmc.palette),
style: {stretch: 'horizontal', margin: '0px 8px', maxHeight: '24px'},
});
// Create a panel with three numbers for the legend
var legendLabels = ui.Panel({
widgets: [
ui.Label(visdmc.min, {margin: '4px 8px'}),
ui.Label(
((visdmc.max-visdmc.min) / 2+visdmc.min),
{margin: '4px 8px', textAlign: 'center', stretch: 'horizontal'}),
ui.Label(visdmc.max, {margin: '4px 8px'})
],
layout: ui.Panel.Layout.flow('horizontal')
});
// Legend title
var dmcLegendLabel = ui.Label({
value: 'Elevation [in Meter]',
style:{ fontSize: '12px',
margin: '0px;'
}});
var legendTitle = ui.Label({
value: 'Legend',
style: {
fontSize: '14px',
fontWeight: 'bold',
margin: '0px;'
}
});
legendPanel.clear();
// Add the legendPanel to the map
legendPanel.add(legendTitle);
legendPanel.add(dmcLegendLabel);
legendPanel.add(colorBar);
legendPanel.add(legendLabels);
}
function insertdsm(){
removeAllLayer();
removePoint();
galleryPanel.clear();
chartPanel.clear();
adddmclegend();
map.centerObject(knparea); //ToDo: if Point is not in KNP center KNP else do nothing
var dsm1mshade = demshade(dsm1m).clip(knparea);
map.addLayer(dsm1mshade, null, 'Hillshade DSM 1m');
map.addLayer(dsm1m.resample('bicubic').clip(knparea), visdmc, 'Elevation DSM 1m', null, 0.5);
map.addLayer(SURFACE_WATER, null, 'Surface Water');
}
function insertdtm(){ //ToDo: if Point is not in KNP center KNP else do nothing
removeAllLayer();
removePoint();
galleryPanel.clear();
chartPanel.clear();
adddmclegend();
map.centerObject(knparea);
var dtm1mshade = demshade(dtm1m).clip(knparea);
map.addLayer(dtm1mshade, null, 'Hillshade DTM 1m');
map.addLayer(dtm1m.resample('bicubic').clip(knparea), visdmc, 'Elevation DTM 1m', null, 0.5);
map.addLayer(SURFACE_WATER, null, 'Surface Water');
}
// Function to add external data
// ESA World Cover 10m #############
function insertesaworldcover(){
// Product to the Map
var esaworldcover = ee.ImageCollection("ESA/WorldCover/v100").first();
var vis = {bands: ['Map']};
removeAllLayer()
map.addLayer(esaworldcover, vis, 'Landcover 2020 10m');
// Add Legend to Map
var esaworldcoverColor = ee.List(esaworldcover.get('Map_class_palette'));
var esaworldcoverLabel = ee.List(esaworldcover.get('Map_class_names'));
var esaworldcoverLegend = function(Color, Label){
var C = ui.Label({
style: {backgroundColor: Color,
padding: '10px',
margin: '4px'}});
var L = ui.Label({
value: Label,
style: {
margin: '5px'
}
});
return ui.Panel({
widgets: [C, L],
layout: ui.Panel.Layout.Flow('horizontal')
});
};
var esaworldcoverLegendLabel = ui.Label({
value: 'World Cover Classes',
style: {fontSize: '12px',
margin: '0px;'
}
});
legendPanel.clear();
legendPanel.add(legendTitle);
legendPanel.add(esaworldcoverLegendLabel);
for(var a = 0; a < 11; a++){
legendPanel.add(esaworldcoverLegend(esaworldcoverColor.get(a).getInfo(), esaworldcoverLabel.get(a).getInfo()));
}
}
// South African National Land Cover
function insertSanlc(){
var sa_nlc2018 = ee.Image("projects/sat-io/open-datasets/landcover/SA_NLC_2018");
var dictsanlc = {"names": ["Contiguous (indigenous) Forest (combined very high, high, medium)", "Contiguous Low Forest & Thicket (combined classes)", "Dense Forest & Woodland (35 - 75% cc)", "Open Woodland (10 - 35% cc)", "Contiguous & Dense Planted Forest (combined classes)", "Open & Sparse Planted Forest", "Temporary Unplanted Forest", "Low Shrubland (other regions)", "Low Shrubland (Fynbos)", "Low Shrubland (Succulent Karoo)", "Low Shrubland (Nama Karoo)", "Sparsely Wooded Grassland (5 - 10% cc)", "Natural Grassland", "Natural Rivers", "Natural Estuaries & Lagoons", "Natural Ocean, Coastal", "Natural Lakes", "Natural Pans (flooded @ obsv time)", "Artificial Dams (incl. canals)", "Artificial Sewage Ponds", "Artificial Flooded Mine Pits", "Herbaceous Wetlands (currently mapped)", "Herbaceous Wetlands (previous mapped extent)", "Mangrove Wetlands", "Natural Rock Surfaces", "Dry Pans", "Eroded Lands", "Sand Dunes (terrestrial)", "Coastal Sand Dunes & Beach Sand", "Bare Riverbed Material", "Other Bare", "Cultivated Commercial Permanent Orchards", "Cultivated Commercial Permanent Vines", "Cultivated Commercial Sugarcane Pivot Irrigated", "Commercial Permanent Pineapples", "Cultivated Commercial Sugarcane Non-Pivot (all other)", "Cultivated Emerging Farmer Sugarcane Non-Pivot (all other)", "Commercial Annuals Pivot Irrigated", "Commercial Annuals Non-Pivot Irrigated", "Commercial Annuals Crops Rain-Fed / Dryland / Non-Irrigated", "Subsistence / Small-Scale Annual Crops", "Fallow Land & Old Fields (Trees)", "Fallow Land & Old Fields (Bush)", "Fallow Land & Old Fields (Grass)", "Fallow Land & Old Fields (Bare)", "Fallow Land & Old Fields (Low Shrub)", "Residential Formal (Tree)", "Residential Formal (Bush)", "Residential Formal (low veg / grass)", "Residential Formal (Bare)", "Residential Informal (Tree)", "Residential Informal (Bush)", "Residential Informal (low veg / grass)", "Residential Informal (Bare)", "Village Scattered (bare only)", "Village Dense (bare only)", "Smallholdings (Tree)", "Smallholdings (Bush)", "Smallholdings (low veg / grass)", "Smallholdings (Bare)", "Urban Recreational Fields (Tree)", "Urban Recreational Fields (Bush)", "Urban Recreational Fields (Grass)", "Urban Recreational Fields (Bare)", "Commercial", "Industrial", "Roads & Rail (Major Linear)", "Mines: Surface Infrastructure", "Mines: Extraction Sites: Open Cast & Quarries combined", "Mines: Extraction Sites: Salt Mines", "Mines: Waste (Tailings) & Resource Dumps", "Land-fills", "Fallow Land & Old Fields (wetlands)"], "colors": ["#F2F2F2", "#065106", "#005F00", "#008500", "#F74006", "#F9764D", "#F9906C", "#B8ABD1", "#8FAB39", "#AC92C5", "#AC9CDA", "#85D285", "#D2B485", "#00009F", "#041FA7", "#0639AB", "#0D50AC", "#125FAC", "#1373B4", "#1D81B6", "#1F8EB8", "#06DEDC", "#06E0D0", "#9F1FEC", "#ffffe0", "#DCDAC5", "#F9E0E0", "#F9F9C5", "#F9F9A7", "#CDD2E0", "#ffffe0", "#A62C39", "#B31F5C", "#DB0000", "#9F3978", "#FF0000", "#F64D6C", "#381A12", "#521F1C", "#85402C", "#C5735F", "#C1436C", "#C55E82", "#D27592", "#E0AAB8", "#DB90A9", "#ECDB0F", "#F6EC13", "#F9F81F", "#FFFF29", "#EC82EC", "#F691E0", "#F99FCF", "#FFC5CF", "#ECC500", "#FFD91F", "#AC7879", "#B89192", "#C49C9E", "#D2B8B8", "#BFFF00", "#33FF33", "#66FF66", "#99FF99", "#C49F0D", "#8F8506", "#F9DD03", "#FFFF00", "#B30606", "#C50606", "#D21D1A", "#F95479", "#6CE7DC"]}
// Create a panel to hold the legend widget
var legend = ui.Panel({
style: {
position: 'bottom-left',
padding: '8px 15px',
height: '50%',
}
});
// Function to generate the legend Source Code: https://code.earthengine.google.com/?scriptPath=users%2Fsat-io%2Fawesome-gee-catalog-examples%3Aregional-landuse-landcover%2FSOUTH-AFRICA-LULC
function addCategoricalLegend(panel, dictsanlc, title) {
// Create and add the legend title.
var legendTitle = ui.Label({
value: title,
style: {
fontWeight: 'bold',
fontSize: '12px',
margin: '0 0 4px 0',
padding: '0'
}
});
panel.add(legendTitle);
var loading = ui.Label('Loading legend...', {margin: '2px 0 4px 0'});
panel.add(loading);
// Creates and styles 1 row of the legend.
var makeRow = function(color, name) {
// Create the label that is actually the colored box.
var colorBox = ui.Label({
style: {
backgroundColor: color,
// Use padding to give the box height and width.
padding: '8px',
margin: '0 0 4px 0'
}
});
// Create the label filled with the description text.
var description = ui.Label({
value: name,
style: {margin: '0 0 2px 3px'}
});
return ui.Panel({
widgets: [colorBox, description],
layout: ui.Panel.Layout.Flow('horizontal')
});
};
// Get the list of palette colors and class names from the image.
var palette = dictsanlc['colors'];
var names = dictsanlc['names'];
loading.style().set('shown', false);
for (var i = 0; i < names.length; i++) {
panel.add(makeRow(palette[i], names[i]));
}
//map.add(panel);
}
// Add the legend to the map
legendPanel.clear();
addCategoricalLegend(legendPanel, dictsanlc, 'South African National Land Cover 2018');
// Add USDM Image image to the map
removeAllLayer()
map.addLayer(sa_nlc2018, {min:1, max:73, palette:dictsanlc['colors']}, 'South African National Land Cover 2018')
}
// Terra Vegetation Continuous Fields Yearly Global 250m
function insertTerraVeg(){