Having read the Google Earth Engine guide 'NDVI, Mapping a Function over a Collection, Quality Mosaicking' (https://developers.google.com/earth-engine/tutorials/tutorial_api_06) I am trying to compute a Normalized Difference Vegetation Index (NDVI) image from a LANDSAT/LC08/C01/T1_TOA image and have written the following code:
var roi = ee.Geometry.Rectangle(138.78575924068758, 35.99620792950091, 140.41997554928133, 35.27856379077837);
Map.centerObject(roi, 8);
Map.addLayer(roi, {}, 'roi');
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA');
var image = ee.Image(
l8.filterBounds(roi)
.filterDate('2015-01-01', '2015-12-31')
.sort('CLOUD_COVER')
.first()
);
print(image)
var nir = image.select('B5');
var red = image.select('B4');
var ndvi = nir.subtract(red).divide(nir.add(red)).rename('NDVI');
print(ndvi)
var ndviParams = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
print(ndviParams)
Map.addLayer(ndvi, ndviParams, 'NDVI image')
Also here is the link to the code: https://code.earthengine.google.com/b46f6ec756fe35c65d302eedbb23d5da.
In the console all of the code appears to compute correctly but for some reason the map layer is not added within the region of interest (roi) and I have no idea why. Also I have tried to use the clip tool (this code: https://code.earthengine.google.com/3c93d1c804aa21ff0c0fb082caa953f3) but the map layer is not added at all.
How can I adjust my code so that the NDVI map layer is added within the region of interest (roi)?