Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/examples/javascripts/QualityMosaic.js
2313 views
1
// Array-based quality mosaic.
2
3
// Returns a mosaic built by sorting each stack of pixels by the first band
4
// in descending order, and taking the highest quality pixel.
5
function qualityMosaic(bands) {
6
// Convert to an array, and declare names for the axes and indices along the
7
// band axis.
8
var array = bands.toArray();
9
var imageAxis = 0;
10
var bandAxis = 1;
11
var qualityIndex = 0;
12
var valuesIndex = 1;
13
14
// Slice the quality and values off the main array, and sort the values by the
15
// quality in descending order.
16
var quality = array.arraySlice(bandAxis, qualityIndex, qualityIndex + 1);
17
var values = array.arraySlice(bandAxis, valuesIndex);
18
var valuesByQuality = values.arraySort(quality.multiply(-1));
19
20
// Get an image where each pixel is the array of band values where the quality
21
// band is greatest. Note that while the array is 2-D, the first axis is
22
// length one.
23
var best = valuesByQuality.arraySlice(imageAxis, 0, 1);
24
25
// Project the best 2D array down to a single dimension, and convert it back
26
// to a regular scalar image by naming each position along the axis. Note we
27
// provide the original band names, but slice off the first band since the
28
// quality band is not part of the result. Also note to get at the band names,
29
// we have to do some kind of reduction, but it won't really calculate pixels
30
// if we only access the band names.
31
var bandNames = bands.min().bandNames().slice(1);
32
return best.arrayProject([bandAxis]).arrayFlatten([bandNames]);
33
}
34
35
// Load the l7_l1t collection for the year 2000, and make sure the first band
36
// is our quality measure, in this case the normalized difference values.
37
var l7 = ee.ImageCollection('LANDSAT/LE07/C01/T1')
38
.filterDate('2000-01-01', '2001-01-01');
39
var withNd = l7.map(function(image) {
40
return image.normalizedDifference(['B4', 'B3']).addBands(image);
41
});
42
43
// Build a mosaic using the NDVI of bands 4 and 3, essentially showing the
44
// greenest pixels from the year 2000.
45
var greenest = qualityMosaic(withNd);
46
47
// Select out the color bands to visualize. An interesting artifact of this
48
// approach is that clouds are greener than water. So all the water is white.
49
var rgb = greenest.select(['B3', 'B2', 'B1']);
50
51
Map.addLayer(rgb, {gain: [1.4, 1.4, 1.1]}, 'Greenest');
52
Map.setCenter(-90.08789, 16.38339, 11);
53
54
55