-
Notifications
You must be signed in to change notification settings - Fork 69
/
geet.js
4302 lines (3660 loc) · 160 KB
/
geet.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 Toolbox (GEET)
* Description: Lib to write small EE apps or big/complex apps with a lot less code.
* Version: 0.7.6
* Eduardo Ribeiro Lacerda <eduardolacerdageo@id.uff.br>
*/
// Error Handling function
function error(funcName, msg) {
print("------------------ GEET --------------------");
print("GEET Error in function: " + funcName.toString());
print(msg.toString());
print("----------------------------------------------");
}
/*
svm:
Function to apply SVM classification to a image.
Params:
(ee.Image) image - The input image to classify.
(ee.List) trainingData - Training data (samples).
(string) fieldName - The name of the column that contains the class names.
optional (string) kernelType - the kernel type of the classifier. Default is 'RBF'.
optional (number) resolution - the spatial resolution of the input image. Default is 30 (landsat).
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
var imgClass = geet.svm(image, samplesfc, landcover);
*/
var svm = function (image, trainingData, fieldName, kernelType, resolution) {
// Error Handling
if (image === undefined) error('svm', 'You need to specify an input image.');
if (trainingData === undefined) error('svm', 'You need to specify the training data.');
if (fieldName === undefined) error('svm', 'You need to specify the field name.');
// Default params
kernelType = typeof kernelType !== 'undefined' ? kernelType : 'RBF';
resolution = typeof resolution !== 'undefined' ? resolution : 30;
var training = image.sampleRegions({
collection: trainingData,
properties: [fieldName],
scale: resolution
});
var classifier = ee.Classifier.libsvm({
kernelType: kernelType,
cost: 10
});
var trained = classifier.train(training, fieldName);
var classified = image.classify(trained);
return classified;
};
/*
cart:
Function to apply CART classification to a image.
Params:
(ee.Image) image - The input image to classify.
(ee.List) trainingData - Training data (samples).
(string) fieldName - The name of the column that contains the class names.
optional (number) resolution - the spatial resolution of the input image. Default is 30 (landsat).
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
var imgClass = geet.cart(image, samplesfc, landcover);
*/
var cart = function (image, trainingData, fieldName, resolution) {
// Error Handling
if (image === undefined) error('cart', 'You need to specify an input image.');
if (trainingData === undefined) error('cart', 'You need to specify the training data.');
if (fieldName === undefined) error('cart', 'You need to specify the field name.');
// Default params
resolution = typeof resolution !== 'undefined' ? resolution : 30;
var training = image.sampleRegions({
collection: trainingData,
properties: [fieldName],
scale: resolution
});
var classifier = ee.Classifier.smileCart().train({
features: training,
classProperty: fieldName
});
var classified = image.classify(classifier);
return classified;
};
/*
rf:
Function to apply Random Forest classification to an image.
Params:
(ee.Image) image - The input image to classify.
(array of strings) bands - The input band names that will be choosed to train the model.
(FeatureCollection) trainingData - All the training data (samples).
(string) fieldName - The name of the column that contains the class names.
optional (number) numOfTrees - The number of trees that the model will create. Default is 10.
optional (number) resolution - the spatial resolution of the input image. Default is 30 (landsat).
optional (number) cv_split - The cross validation split percentage .
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
var imgClass = geet.rf(image, samplesfc, landcover, 10);
*/
var rf = function (image, bands, trainingData, fieldName, numOfTrees, resolution, cv_split) {
// Error Handling
if (image === undefined) error('rf', 'You need to specify an input image.');
if (bands === undefined) error('rf', 'You need to specify the image bands serve as the model input');
if (trainingData === undefined) error('rf', 'You need to specify the training data.');
if (fieldName === undefined) error('rf', 'You need to specify the field name.');
// Default params
numOfTrees = typeof numOfTrees !== 'undefined' ? numOfTrees : 10;
resolution = typeof resolution !== 'undefined' ? resolution : 30;
cv_split = typeof cv_split !== 'undefined' ? cv_split : 0.8;
var input_features = image.sampleRegions({
collection: trainingData,
properties: [fieldName],
scale: resolution
});
// Split data in (train - test) datasets
var withRandom = input_features.randomColumn();
var split = cv_split;
var trainingPartition = withRandom.filter(ee.Filter.lt('random', split));
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));
var classifier = ee.Classifier.smileRandomForest(numOfTrees).train({
features: trainingPartition,
classProperty: fieldName,
inputProperties: bands
});
// Model/Classify with training dataset
var classified = image.classify(classifier);
// Validation
var validation = testingPartition.classify(classifier);
var testAccuracy = validation.errorMatrix('cobertura', 'classification');
print('Validation error matrix: ', testAccuracy);
print('Validation overall accuracy: ', testAccuracy.accuracy());
print('kappa: ', testAccuracy.kappa())
var classifier_final = ee.Classifier.smileRandomForest(numOfTrees).train({
features: input_features,
classProperty: fieldName,
inputProperties: bands
});
var classified_final = image.classify(classifier_final);
return classified_final;
};
/*
naive_bayes:
Function to apply the Fast Naive Bayes classification to a image.
Params:
(ee.Image) image - The input image to classify.
(ee.List) trainingData - Training data (samples).
(string) fieldName - The name of the column that contains the class names.
optional (number) resolution - the spatial resolution of the input image. Default is 30 (landsat)..
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
var imgClass = geet.naive_bayes(image, samplesfc, landcover);
*/
var naive_bayes = function (image, trainingData, fieldName, resolution) {
// Error Handling
if (image === undefined) error('naive_bayes', 'You need to specify an input image.');
if (trainingData === undefined) error('naive_bayes', 'You need to specify the training data.');
if (fieldName === undefined) error('naive_bayes', 'You need to specify the field name.');
// Default params
resolution = typeof resolution !== 'undefined' ? resolution : 30;
var training = image.sampleRegions({
collection: trainingData,
properties: [fieldName],
scale: resolution
});
var classifier = ee.Classifier.smileNaiveBayes().train({
features: training,
classProperty: fieldName
});
var classified = image.classify(classifier);
return classified;
};
/*
max_ent:
Function to apply the Maximum Entropy classification to a image.
Params:
(ee.Image) image - The input image to classify.
(ee.List) trainingData - Training data (samples).
(string) fieldName - The name of the column that contains the class names.
optional (number) resolution - the spatial resolution of the input image. Default is 30 (landsat).
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
var imgClass = geet.max_ent(image, samplesfc, landcover);
*/
var max_ent = function (image, trainingData, fieldName, resolution) {
// Error Handling
if (image === undefined) error('max_ent', 'You need to specify an input image.');
if (trainingData === undefined) error('max_ent', 'You need to specify the training data.');
if (fieldName === undefined) error('max_ent', 'You need to specify the field name.');
// Default params
resolution = typeof resolution !== 'undefined' ? resolution : 30;
var training = image.sampleRegions({
collection: trainingData,
properties: [fieldName],
scale: resolution
});
var classifier = ee.Classifier.amnhMaxent().train({
features: training,
classProperty: fieldName
});
var classified = image.classify(classifier);
return classified;
};
/*
kmeans:
Function to apply kmeans classification to an image.
Params:
(ee.Image) image - The input image to classify.
(list) roi - A polygon containing the study area.
optional (number) numClusters - the number of clusters that will be used. Default is 15.
optional (number) resolution - the scale number. The scale is related to the spatial resolution of the image. Landsat is 30, sou the default is 30 also.
optional (number) numPixels - the number of pixels that the classifier will take samples from the roi. Default is set to 5000.
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
var imgClass = geet.kmeans(image, roi);
or
var geet = require('users/eduardolacerdageo/geet:geet');
var imgClass = geet.kmeans(image, roi, 20, 10, 6000);
*/
var kmeans = function (image, roi, numClusters, resolution, numPixels) {
// Error Handling
if (image === undefined) error('kmeans', 'You need to specify an input image.');
if (roi === undefined) error('kmeans', 'You need to define and pass a roi as argument to collect the samples for the classfication process.');
// Default params
numClusters = typeof numClusters !== 'undefined' ? numClusters : 15;
scale = typeof scale !== 'undefined' ? scale : 30;
numPixels = typeof numPixels !== 'undefined' ? numPixels : 5000;
// Make the training dataset.
var training = image.sample({
region: roi,
scale: resolution,
numPixels: numPixels
});
// Instantiate the clusterer and train it.
var clusterer = ee.Clusterer.wekaKMeans(numClusters).train(training);
// Cluster the input using the trained clusterer.
var result = image.cluster(clusterer);
Map.addLayer(ee.Image().paint(roi, 0, 2), {}, 'roi_kmeans');
Map.addLayer(result.randomVisualizer(), {}, 'clusters');
return result;
}
/*
ndvi_change_detection:
Function to detect changes between two input images using the NDVI index
and a threshold paramter.
The function adds the two masked indices and return the sum of the two.
Its a good choice to call the plotClass function to visualize the result.
Ex: geet.plotClass(ndviChange, 3, 'change_detection');
Params:
(string) sensor = The name of the sensor that will be used. 'L5' or 'L8.
(ee.Image) img1 = The first input image.
(ee.Image) img2 = The second input image.
(ee.Number) threshold = The number of the threshold. All the values at the
image that is gte (grater of equal) to this number
will be selected.
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
var ndviChange = geet.ndvi_change_detection(image_2014, image_2015, 'L8', 0.5);
*/
var ndvi_change_detection = function (img1, img2, sensor, threshold) {
// Error Handling
if (img1 === undefined) error('ndvi_change_detection', 'You need to specify an input image.');
if (img2 === undefined) error('ndvi_change_detection', 'You need to specify an input image.');
if (sensor === undefined) error('ndvi_change_detection', 'You need to specify the sensor name.');
if (threshold === undefined) error('ndvi_change_detection', 'You need to specify the threshold number.');
if (sensor === 'L8') {
var i_ndvi_1 = img1.normalizedDifference(['B5', 'B4']).rename('NDVI');
var i_ndvi_2 = img2.normalizedDifference(['B5', 'B4']).rename('NDVI');
} else if (sensor === 'L5' || sensor === 'L7') {
var i_ndvi_1 = img1.normalizedDifference(['B4', 'B3']).rename('NDVI');
var i_ndvi_2 = img2.normalizedDifference(['B4', 'B3']).rename('NDVI');
} else if (sensor === 'S2') {
var i_ndvi_1 = img1.normalizedDifference(['B8', 'B4']).rename('NDVI');
var i_ndvi_2 = img2.normalizedDifference(['B8', 'B4']).rename('NDVI');
} else {
print('Error: Wrong sensor. Choose between L5, L7, L8 or S2');
return;
}
var i_ndvi_1_mask = i_ndvi_1.select('NDVI').gte(threshold);
var i_ndvi_2_mask = i_ndvi_2.select('NDVI').gte(threshold);
var imgSoma = i_ndvi_1_mask.add(i_ndvi_2_mask);
Map.addLayer(imgSoma, { min: 0, max: 2, palette: [COLOR.SHADOW, COLOR.URBAN, COLOR.PASTURE] }, 'ndvi_cd');
return imgSoma;
}
/*
ndwi_change_detection:
Function to detect changes between two input images using the NDWI index
and a threshold paramter.
The function adds the two masked indices and return the sum of the two.
Its a good choice to call the plotClass function to visualize the result.
Ex: geet.plotClass(ndwiChange, 3, 'change_detection');
Params:
(string) sensor = The name of the sensor that will be used. 'L5' or 'L8.
(ee.Image) img1 = The first input image.
(ee.Image) img2 = The second input image.
(ee.Number) threshold = The number of the threshold. All the values at the
image that is gte (grater of equal) to this number
will be selected.
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
var ndwiChange = geet.ndwi_change_detection( image_2014, image_2015, 'L8', 0.5);
*/
var ndwi_change_detection = function (img1, img2, sensor, threshold) {
// Error Handling
if (img1 === undefined) error('ndwi_change_detection', 'You need to specify an input image.');
if (img2 === undefined) error('ndwi_change_detection', 'You need to specify an input image.');
if (sensor === undefined) error('ndwi_change_detection', 'You need to specify the sensor name.');
if (threshold === undefined) error('ndwi_change_detection', 'You need to specify the threshold number.');
if (sensor === 'L8') {
var i_ndwi_1 = img1.normalizedDifference(['B4', 'B6']).rename('NDWI');
var i_ndwi_2 = img2.normalizedDifference(['B4', 'B6']).rename('NDWI');
} else if (sensor === 'L5' || sensor === 'L7') {
var i_ndwi_1 = img1.normalizedDifference(['B3', 'B5']).rename('NDWI');
var i_ndwi_2 = img2.normalizedDifference(['B3', 'B5']).rename('NDWI');
} else if (sensor === 'S2') {
var i_ndwi_1 = img1.normalizedDifference(['B4', 'B11']).rename('NDWI');
var i_ndwi_2 = img2.normalizedDifference(['B4', 'B11']).rename('NDWI');
} else {
print('Error: Wrong sensor. Choose between L5, L7, L8 or S2');
return;
}
var i_ndwi_1_mask = i_ndwi_1.select('NDWI').gte(threshold);
var i_ndwi_2_mask = i_ndwi_2.select('NDWI').gte(threshold);
var imgSoma = i_ndwi_1_mask.add(i_ndwi_2_mask);
Map.addLayer(imgSoma, { min: 0, max: 2, palette: [COLOR.SHADOW, COLOR.URBAN, COLOR.PASTURE] }, 'ndwi_cd');
return imgSoma;
}
/*
ndbi_change_detection:
Function to detect changes between two input images using the NDBI index
and a threshold paramter.
The function adds the two masked indices and return the sum of the two.
Its a good choice to call the plotClass function to visualize the result.
Ex: geet.plotClass(ndbiChange, 3, 'change_detection');
Params:
(string) sensor = The name of the sensor that will be used. 'L5' or 'L8.
(ee.Image) img1 = The first input image.
(ee.Image) img2 = The second input image.
(ee.Number) threshold = The number of the threshold. All the values at the
image that is gte (grater of equal) to this number
will be selected.
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
var ndbiChange = geet.ndbi_change_detection(image_2014, image_2015, 'L8', 0.5);
*/
var ndbi_change_detection = function (img1, img2, sensor, threshold) {
// Error Handling
if (img1 === undefined) error('ndbi_change_detection', 'You need to specify an input image.');
if (img2 === undefined) error('ndbi_change_detection', 'You need to specify an input image.');
if (sensor === undefined) error('ndbi_change_detection', 'You need to specify the sensor name.');
if (threshold === undefined) error('ndbi_change_detection', 'You need to specify the threshold number.');
if (sensor === 'L8') {
var i_ndbi_1 = img1.normalizedDifference(['B6', 'B5']).rename('NDBI');
var i_ndbi_2 = img2.normalizedDifference(['B6', 'B5']).rename('NDBI');
} else if (sensor === 'L5' || sensor === 'L7') {
var i_ndbi_1 = img1.normalizedDifference(['B5', 'B4']).rename('NDBI');
var i_ndbi_2 = img2.normalizedDifference(['B5', 'B4']).rename('NDBI');
} else if (sensor === 'S2') {
var i_ndbi_1 = img1.normalizedDifference(['B11', 'B8']).rename('NDBI');
var i_ndbi_2 = img2.normalizedDifference(['B11', 'B8']).rename('NDBI');
} else {
print('Error: Wrong sensor. Choose between L5, L7, L8 or S2');
return;
}
var i_ndbi_1_mask = i_ndbi_1.select('NDBI').gte(threshold);
var i_ndbi_2_mask = i_ndbi_2.select('NDBI').gte(threshold);
var imgSoma = i_ndbi_1_mask.add(i_ndbi_2_mask);
Map.addLayer(imgSoma, { min: 0, max: 2, palette: [COLOR.SHADOW, COLOR.URBAN, COLOR.PASTURE] }, 'ndbi_cd');
return imgSoma;
};
/*
Texture:
Function generate a texture filter on the image.
Params:
(ee.Image) image = The input image.
(ee.Number) radius = the radius number that defines the effect level of the filter.
Bigger numbers generalize more the result.
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
var texture = geet.texture(image_from_rio, 1);
*/
var texture = function (image, radius) {
// Error Handling
if (image === undefined) error('texture', 'You need to specify an input image.');
if (radius === undefined) error('texture', 'You need to specify the radius number.');
var texture = image.reduceNeighborhood({
reducer: ee.Reducer.stdDev(),
kernel: ee.Kernel.circle(radius),
});
return texture;
};
/*
Majority:
Function to filter the final classification image and clear the salt n' pepper effect.
Params:
(ee.Image) image = The input image.
(ee.Number) radius = the radius number that defines the effect level of the filter.
Bigger numbers generalize more the result.
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
var majority = geet.majority(image_from_rio, 1);
*/
var majority = function (image, radius) {
// Error Handling
if (image === undefined) error('majority', 'You need to specify an input image.');
if (radius === undefined) error('majority', 'You need to specify the radius number.');
var majority = image.reduceNeighborhood({
reducer: ee.Reducer.mode(),
kernel: ee.Kernel.circle(radius),
});
return majority;
};
// COLOR OBJECT
var COLOR = {
WATER: '0066ff',
FOREST: '009933',
PASTURE: '99cc00',
URBAN: 'ff0000',
SHADOW: '000000',
NULL: '808080'
};
/*
color:
Function to return a valid color value from the object COLOR.
Params:
(string) color - the name of the desired color.
Valid options are water, forest, pasture, urban, shadow or null
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
geet.color('water');
*/
var color = function (_color) {
// Error Handling
if (_color === undefined) error('color', 'You need to specify the color name.');
var color = _color.toLowerCase();
switch (color) {
case 'water':
return COLOR.WATER;
case 'forest':
return COLOR.FOREST;
case 'pasture':
return COLOR.PASTURE;
case 'urban':
return COLOR.URBAN;
case 'shadow':
return COLOR.SHADOW;
case 'null':
return COLOR.NULL;
default:
return 'Error: Valid options are water, forest, pasture, urban, shadow or null! Remember to pass the argument as a string.';
}
};
/*
plot_rgb:
Function to plot a RGB image.
Params:
(ee.Image) image - the image to display.
optional (string) title - the layer title.
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
geet.plot_rgb(image, 'rgb_image');
*/
var plot_rgb = function (image, title) {
// Error Handling
if (image === undefined) error('plot_rgb', 'You need to specify an input image.');
// Default params
title = typeof title !== 'undefined' ? title : 'image_RGB';
var vizParams = {
'bands': 'B4,B3,B2',
'min': 5000,
'max': 30000,
'gamma': 1.6
};
Map.addLayer(image, vizParams, title);
};
/*
plot_ndvi:
Function to plot a NDVI image index.
Params:
(ee.Image) image - the image to display.
(string) title - the layer title.
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
geet.plot_ndvi(ndvi, 'ndvi_image');
*/
var plot_ndvi = function (image, title) {
// Error Handling
if (image === undefined) error('plot_ndvi', 'You need to specify an input image.');
Map.addLayer(image, { min: -1, max: 1, palette: ['FF0000', '00FF00'] }, title);
};
/*
plot_ndwi:
Function to plot a NDWI image index.
Params:
(ee.Image) image - the image to display.
(string) title - the layer title.
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
geet.plot_ndwi(ndwi, 'ndwi_image');
*/
var plot_ndwi = function (image, title) {
// Error Handling
if (image === undefined) error('plot_ndwi', 'You need to specify an input image.');
Map.addLayer(image, { min: -1, max: 1, palette: ['00FFFF', '0000FF'] }, title);
};
/*
plot_class:
Function to plot the final classification map.
Params:
(ee.Image) image - the image to process
(number) numClasses - the number of classes that your classification map has. It variates from 2 to 5 max classes only.
optional (string) title - the layer title.
Usage:
var geet = require('users/eduardolacerdageo/geet:geet');
geet.plot_class(classified, 4, 'class_final');
*/
var plot_class = function (image, numClasses, title) {
// Error Handling
if (image === undefined) error('plot_class', 'You need to specify an input image.');
if (numClasses === undefined) error('plot_class', 'You need to specify the number of classes to plot.');
// Default params
title = typeof title !== 'undefined' ? title : 'class_final';
switch (numClasses) {
case 2:
Map.addLayer(image, { min: 0, max: numClasses - 1, palette: [COLOR.SHADOW, COLOR.NULO] }, title);
break;
case 3:
Map.addLayer(image, { min: 0, max: numClasses - 1, palette: [COLOR.URBAN, COLOR.FOREST, COLOR.WATER] }, title);
break;
case 4:
Map.addLayer(image, { min: 0, max: numClasses - 1, palette: [COLOR.URBAN, COLOR.FOREST, COLOR.PASTURE, COLOR.WATER] }, title);
break;
case 5:
Map.addLayer(image, { min: 0, max: numClasses - 1, palette: [COLOR.URBAN, COLOR.FOREST, COLOR.PASTURE, COLOR.WATER, COLOR.SHADOW] }, title);
break;
default:
print("Error: Wrong number of classes. plotClass supports a number of classes from 2 to 5 only.");
break;
}
};
/*
landsat_indices:
Function to take an input image and generate indexes using the landsat (5, 7 and 8) dataset like:
NDVI, NDWI, NDBI...
More indices and features will be added in the future!
Supported indices:
NDVI, NDWI, NDBI, NRVI, EVI, SAVI and GOSAVI
Params:
(ee.Image) image - the image to process.
(string) sensor - the sensor that you are working on Landsat 5 ('L5'), 7 ('L7') or 8 ('L8').
optional (string or string array) index - you can specify the index that you want
if you dont specify any index the function will create all possible indices.
Usage:
var geet = require('users/eduardolacerdageo/default:Function/indexGen');
var result = geet.landsat_indices(image, 'L5'); // Will create all possible indices.
or specifying the index to generate:
var geet = require('users/eduardolacerdageo/geet:geet');
var result = geet.landsat_indices(image, 'L5', 'savi'); // This will create only SAVI.
*/
var landsat_indices = function (image, sensor, index) {
// Error Handling
if (image === undefined) error('landsat_indices', 'You need to specify an input image.');
if (sensor === undefined) error('landsat_indices', 'You need to specify the sensor name.');
if (index != null) {
switch (index.toLowerCase()) {
case 'ndvi':
if (sensor == 'L5' || sensor == 'L7') {
var i_ndvi = image.expression(
'((NIR - RED) / (NIR + RED))', {
'NIR': image.select('B4'),
'RED': image.select('B3')
}).rename('NDVI');
var newImage = image.addBands(i_ndvi);
return newImage;
} else if (sensor == 'L8') {
var i_ndvi = image.expression(
'((NIR - RED) / (NIR + RED))', {
'NIR': image.select('B5'),
'RED': image.select('B4')
}).rename('NDVI');
var newImage = image.addBands(i_ndvi);
return newImage;
} else {
print('Error: Wrong sensor!');
}
break;
case 'ndwi':
if (sensor == 'L5' || sensor == 'L7') {
var i_ndwi = image.expression(
'((NIR - SWIR1) / (NIR + SWIR1))', {
'SWIR1': image.select('B5'),
'RED': image.select('B3')
}).rename('NDWI');
var newImage = image.addBands(i_ndwi);
return newImage;
} else if (sensor == 'L8') {
var i_ndwi = image.expression(
'((NIR - SWIR1) / (NIR + SWIR1))', {
'SWIR1': image.select('B6'),
'RED': image.select('B4')
}).rename('NDWI');
var newImage = image.addBands(i_ndwi);
return newImage;
} else {
print('Error: Wrong sensor!');
}
break;
case 'ndbi':
if (sensor == 'L5' || sensor == 'L7') {
var i_ndbi = image.expression(
'((SWIR1 - NIR) / (SWIR1 + NIR))', {
'SWIR1': image.select('B5'),
'NIR': image.select('B3')
}).rename('NDBI');
var newImage = image.addBands(i_ndbi);
return newImage;
} else if (sensor == 'L8') {
var i_ndbi = image.expression(
'((SWIR1 - NIR) / (SWIR1 + NIR))', {
'SWIR1': image.select('B6'),
'NIR': image.select('B5')
}).rename('NDBI');
var newImage = image.addBands(i_ndbi);
return newImage;
} else {
print('Error: Wrong sensor!');
}
break;
case 'nrvi':
if (sensor == 'L5' || sensor == 'L7') {
var i_nrvi = image.expression(
'(RED/NIR - 1) / (RED/NIR + 1)', {
'NIR': image.select('B4'),
'RED': image.select('B3')
}).rename('NRVI');
var newImage = image.addBands(i_nrvi);
return newImage;
} else if (sensor == 'L8') {
var i_nrvi = image.expression(
'(RED/NIR - 1) / (RED/NIR + 1)', {
'NIR': image.select('B5'),
'RED': image.select('B4')
}).rename('NRVI');
var newImage = image.addBands(i_nrvi);
return newImage;
} else {
print('Error: Wrong sensor!');
}
break;
case 'ndmi':
if (sensor == 'L5' || sensor == 'L7') {
var i_ndmi = image.expression(
'((NIR - SWIR) / (NIR + SWIR))', {
'NIR': image.select('B4'),
'SWIR': image.select('B5')
}).rename('NDMI');
var newImage = image.addBands(i_ndmi);
return newImage;
} else if (sensor == 'L8') {
var i_ndmi = image.expression(
'((NIR - SWIR) / (NIR + SWIR))', {
'NIR': image.select('B5'),
'SWIR': image.select('B6')
}).rename('NDMI');
var newImage = image.addBands(i_ndmi);
return newImage;
} else {
print('Error: Wrong sensor!');
}
break;
case 'gli':
if (sensor == 'L5' || sensor == 'L7') {
var i_gli = image.expression(
'(2 * GREEN - RED - BLUE) / (2 * GREEN + RED + BLUE)', {
'BLUE': image.select('B1'),
'GREEN': image.select('B2'),
'RED': image.select('B3')
}).rename('GLI');
var newImage = image.addBands(i_gli);
return newImage;
} else if (sensor == 'L8') {
var i_gli = image.expression(
'(2 * GREEN - RED - BLUE) / (2 * GREEN + RED + BLUE)', {
'BLUE': image.select('B2'),
'GREEN': image.select('B3'),
'RED': image.select('B4')
}).rename('GLI');
var newImage = image.addBands(i_ndbi);
return newImage;
} else {
print('Error: Wrong sensor!');
}
break;
case 'evi':
if (sensor == 'L5' || sensor == 'L7') {
var i_evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B4'),
'RED': image.select('B3'),
'BLUE': image.select('B1')
}).rename('EVI');
var newImage = image.addBands(i_evi);
return newImage;
} else if (sensor == 'L8') {
var i_evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B5'),
'RED': image.select('B4'),
'BLUE': image.select('B2')
}).rename('EVI');
var newImage = image.addBands(i_evi);
return newImage;
} else if (sensor == 'S2') {
var i_evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B8'),
'RED': image.select('B4'),
'BLUE': image.select('B2')
}).rename('EVI');
var newImage = image.addBands(i_evi);
return newImage;
} else {
print('Error: Wrong sensor!');
}
break;
case 'savi':
if (sensor == 'L5' || sensor == 'L7') {
var i_savi = image.expression(
'(1 + L) * ((NIR - RED) / (NIR + RED + L))', {
'NIR': image.select('B4'),
'RED': image.select('B3'),
'L': 0.5
}).rename('SAVI');
var newImage = image.addBands(i_savi);
return newImage;
} else if (sensor == 'L8') {
var i_savi = image.expression(
'(1 + L) * ((NIR - RED) / (NIR + RED + L))', {
'NIR': image.select('B5'),
'RED': image.select('B4'),
'L': 0.5
}).rename('SAVI');
var newImage = image.addBands(i_savi);
return newImage;
} else if (sensor == 'S2') {
var i_savi = image.expression(
'(1 + L) * ((NIR - RED) / (NIR + RED + L))', {
'NIR': image.select('B8'),
'RED': image.select('B4'),
'L': 0.5
}).rename('SAVI');
var newImage = image.addBands(i_savi);
return newImage;
} else {
print('Error: Wrong sensor!');
}
break;
case 'gosavi':
if (sensor == 'L5' || sensor == 'L7') {
var i_gosavi = image.expression(
'(NIR - GREEN) / (NIR + GREEN + Y)', {
'NIR': image.select('B4'),
'GREEN': image.select('B2'),
'Y': 0.16
}).rename('GOSAVI');
var newImage = image.addBands(i_gosavi);
return newImage;
} else if (sensor == 'L8') {
var i_gosavi = image.expression(
'(NIR - GREEN) / (NIR + GREEN + Y)', {
'NIR': image.select('B5'),
'GREEN': image.select('B3'),
'Y': 0.16
}).rename('GOSAVI');
var newImage = image.addBands(i_gosavi);
return newImage;
} else if (sensor == 'S2') {
var i_gosavi = image.expression(
'(NIR - GREEN) / (NIR + GREEN + Y)', {
'NIR': image.select('B8'),
'GREEN': image.select('B3'),
'Y': 0.16
}).rename('GOSAVI');
var newImage = image.addBands(i_gosavi);
return newImage;
} else {
print('Error: Wrong sensor!');
}
break;
}
} else { // END OF SWITCH
// Gen ALL indices
if (sensor == 'L5') {
var i_ndvi = image.expression(
'((NIR - RED) / (NIR + RED))', {
'NIR': image.select('B4'),
'RED': image.select('B3')
}).rename('NDVI');
var i_ndwi = image.expression(
'((NIR - SWIR1) / (NIR + SWIR1))', {
'SWIR1': image.select('B5'),
'NIR': image.select('B3')
}).rename('NDWI');
var i_ndbi = image.expression(
'((SWIR1 - NIR) / (SWIR1 + NIR))', {
'SWIR1': image.select('B5'),
'NIR': image.select('B3')
}).rename('NDBI');
var i_gli = image.expression(
'(2 * GREEN - RED - BLUE) / (2 * GREEN + RED + BLUE)', {
'BLUE': image.select('B1'),
'GREEN': image.select('B2'),
'RED': image.select('B3')
}).rename('GLI');
var i_nrvi = image.expression(
'(RED/NIR - 1) / (RED/NIR + 1)', {
'NIR': image.select('B4'),
'RED': image.select('B3')
}).rename('NRVI');
var i_ndmi = image.expression(
'((NIR - SWIR) / (NIR + SWIR))', {
'NIR': image.select('B4'),
'SWIR': image.select('B5')
}).rename('NDMI');
var i_evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B4'),
'RED': image.select('B3'),
'BLUE': image.select('B1')
}).rename('EVI');
var i_savi = image.expression(
'(1 + L) * ((NIR - RED) / (NIR + RED + L))', {
'NIR': image.select('B4'),
'RED': image.select('B3'),
'L': 0.5
}).rename('SAVI');
var i_gosavi = image.expression(
'(NIR - GREEN) / (NIR + GREEN + Y)', {
'NIR': image.select('B4'),
'GREEN': image.select('B2'),
'Y': 0.16
}).rename('GOSAVI');
var Brightness = image.expression(
'(BLUE * 0.2043) + (GREEN * 0.4158) + (RED * 0.5524) + (NIR * 0.5741) + (SWIR1 * 0.3124) + (SWIR2 * 0.2303)', {
'SWIR2': image.select('B7'),
'SWIR1': image.select('B5'),
'NIR': image.select('B4'),
'RED': image.select('B3'),
'GREEN': image.select('B2'),
'BLUE': image.select('B1')
}).rename('Brightness').toFloat();
var Greenness = image.expression(
'(BLUE * -0.1603) + (GREEN * -0.2819) + (RED * -0.4934) + (NIR * 0.7940) + (SWIR1 * -0.0002) + (SWIR2 * -0.1446)', {
'SWIR2': image.select('B7'),
'SWIR1': image.select('B5'),
'NIR': image.select('B4'),
'RED': image.select('B3'),
'GREEN': image.select('B2'),
'BLUE': image.select('B1')
}).rename('Greenness').toFloat();
var Wetness = image.expression(
'(BLUE * 0.0315) + (GREEN * 0.2021) + (RED * 0.3102) + (NIR * 0.1594) + (SWIR1 * -0.6806) + (SWIR2 * -0.6109)', {
'SWIR2': image.select('B7'),
'SWIR1': image.select('B5'),
'NIR': image.select('B4'),
'RED': image.select('B3'),
'GREEN': image.select('B2'),
'BLUE': image.select('B1')
}).rename('Wetness').toFloat();
var newImage = image.addBands([i_ndvi, i_ndwi, i_ndbi, i_nrvi, i_evi, i_savi, i_ndmi, i_gosavi, Brightness, Greenness, Wetness]);
return newImage;
} else if (sensor == 'L7') {
var i_ndvi = image.expression(
'((NIR - RED) / (NIR + RED))', {
'NIR': image.select('B4'),
'RED': image.select('B3')
}).rename('NDVI');
var i_ndwi = image.expression(
'((NIR - SWIR1) / (NIR + SWIR1))', {
'SWIR1': image.select('B5'),
'NIR': image.select('B3')
}).rename('NDWI');