NDVI - Preparation of Base Data
The code is basically the same for any region, the only difference is the defined Area of Interest.
Therefore, we will keep the variable names universal and set a choice of region at the very beginning.
Like that, we can simply choose which region we want to look at and the code will still work.
Preparing the base data
To start our analysis, we will want to create a base dataset that contains a NDVI-band for every Image of our ImageCollection.
This ImageCollection can then be used to filter for any specific time or region we are interested in.
As .normalizedDifference only works for images, we will need to apply the NDVI to every single Image of our ImageCollection.
Luckily, there is a way to have this done automatically by mapping a function over an ImageCollection.
//Step 1: Access the AOI geometry
//Here, we can choose which region to look at.
//Please, remove the comments of the desired region and feel free to add any region you would like to analyse.
//By default, the code will run for the Administrative Unit of the region Homs, as processing times are shorter than for the whole country of Lebanon or Syria.
//Variant 1: Syria
//var extent_aoi = ee.FeatureCollection("FAO/GAUL/2015/level0")
// .filter(ee.Filter.eq('ADM0_NAME', 'Syrian Arab Republic')); //filter for entry that equals the UN country name 'Syrian Arab Rebuplic'
//print(extent_aoi, 'Extent AOI');
//Map.addLayer(extent_syria, {},'Extent AOI');
//Map.centerObject(extent_aoi, 8);
//Variant 2: Lebanon
//var extent_aoi = ee.FeatureCollection("FAO/GAUL/2015/level0")
// .filter(ee.Filter.eq('ADM0_NAME', 'Lebanon')); //filter for entry that equals the UN country name 'Lebanon'
//print(extent_aoi, 'Extent AOI');
//Map.addLayer(extent_aoi, {},'Extent AOI');
//Map.centerObject(extent_aoi, 8);
//Variant 3: Homs
var extent_aoi = ee.FeatureCollection("FAO/GAUL/2015/level1")
.filter(ee.Filter.eq('ADM1_NAME', 'Homs')); //filter for entry that equals the ADM1_NAME 'Homs'
print(extent_aoi, 'Extent AOI');
Map.addLayer(extent_aoi, {},'Extent AOI');
Map.centerObject(extent_aoi, 8);
//Step 2: Access Landsat 8 data from the day it launched until today. Filter for the desired region and less than 10% cloud cover.
var ls8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterBounds(extent_aoi)
.filterDate('2013-02-11', '2021-06-18')
.filter(ee.Filter.lt('CLOUD_COVER', 10));
print(ls8, 'Image Collection 2013-2021 <10% clouds'); //Use the console to look at the amount of Images accumulated
var visParams = {'min': 400,'max': [4000,3000,3000], 'bands':'B5,B4,B3'}; //bands 5, 4 and 3 are to be visualized; False Color Composite
Map.addLayer(ls8, visParams ,'Image Collection 2013-2021 <10% clouds', false); // Visualize the ImageCollection in the Map panel and assign a name to it; Hide the layer by default, as it takes long to render
//Step 3: Write a function that allows us to map the NDVI to every Image of our of our ImageCollection. Use .rename to name the added NDVI-band 'NDVI'.
var addNDVI = function(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
return image.addBands(ndvi);
};
//Apply the function to the ImageCollection
var ls8_ndvi = ls8.map(addNDVI);
print(ls8_ndvi, 'Image Collection with NDVI')
Filtering the base data
Check both the Console and random pixels with the Inspector: Every Image of our Collection should now have a 13th band 'NDVI' added, with values ranging from -1 to 1.
This is our base dataset. We can apply filters to it, just as we do to create our initial ImageCollection.
Let us for example filter it for the NDVI data of 2017.
var ndvi_2017 = ls8_ndvi
.filterDate('2017-01-01', '2017-12-31')
.select('NDVI')
print(ndvi_2017, 'NDVI 2017')
//Use the console to look at the amount of Images with NDVI-bands for your Area of Interest, 2017.
//Define specific visualization parameters for the NDVI and visualize it.
var ndviParams = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
Map.addLayer(ndvi_2017, ndviParams, 'NDVI 2017', false);
Map.addLayer(ndvi_2017.median().clip(extent_aoi), ndviParams, 'NDVI 2017 Variant 2', false);
Use the Layers-panel in the top right corner of the Map panel to toggle between both layer variants. What are the differences?
//At this point, you could also further subset the AOI: just choose whatever geometry you have access to and filter the ImageCollection the way we already learned! You can for example use either GAUL Level 1 / Level 2 data, your own geometries uploaded to your assets or draw a geometry using the Geometry-Tools in the map panel.