True color and False color display, NDVI calculation, masking and display in custom colors¶

Creation of a collection of images covering Amsterdam and acquired in december 2016 with a cloud coverage not more than 10%
From the collection two process elements are created:
pTC is a True Color combination of B04, B03 and B02 bands
pFC is a False Color combination of B08, B04 and B03 bands
The two processing elements are added as layers to the map

In [ ]:
map = Map(center=[52.2, 5.2], size=[800, 600], zoom=7)
map
In [ ]:
map.clear()
coll = inter.ImageCollection("S2")
coll = coll.filterOnGeoName("Amsterdam, Netherlands", 0.5)
coll = coll.filterOnDate(2016,12,1, 2016,12,30)
coll = coll.filterOn("cloudcover", "<=", 10).filterOn("jrc_filepath", "<>", "").limit(20)
coll.sort("cloudcover",True)
pTC = coll.process().bands("B04","B03","B02",1.2).shift(0)
layerTC = map.addLayer(pTC.toLayer())
pFC = coll.process().bands("B08","B04","B03",1.5).shift(0)
layerFC = map.addLayer(pFC.toLayer())

Sample swapping of the two layers using a python function

In [ ]:
def swapLayers(swap, interval=3):
    import time 
    ntimes = 6
    while ntimes > 0:
        ntimes = ntimes - 1
        if swap[0].visible:
            swap[0].visible = False
            swap[1].visible = True
        else:
            swap[0].visible = True
            swap[1].visible = False

        time.sleep(interval)
        
swapLayers([layerTC, layerFC])

Open another map

In [ ]:
map = Map(center=[52.2, 5.2], size=[800, 600], zoom=7)
map

Create a processing element that calculates the NDVI (output values range from 0 to 65535) and applies a color legend to all values in the range [46000,55000]. Then the color NDVI is displayed on top of the True Color composite

In [ ]:
colors = ["Red", "Yellow", "Green"]
p2 = coll.process().NDVI().maskLT(46000.0).scale(46000,55000).colorCustom(colors)
map.clear()
layerTC = map.addLayer(pTC.toLayer())
tlayer2 = map.addLayer(p2.toLayer())
In [ ]:
tlayer2.visible = False
In [ ]:
tlayer2.visible = True
In [ ]: