Springe direkt zu Inhalt

Raster and Vector Code: Clip and Reduce

Clip

Now that we have visualized our data, we can clearly see that our image collection comprises all the Sentinel-2 tiles that lie within the extents of our geometries boundaries. This makes it rather hard to focus on the actual research area and does not look very good on a finished product.

To remove the unnecessary parts of the ImageCollection, we can clip its extents to the extent of our defining geometry.

Attention: .clip() only works on Images, not on ImageCollections! To generate an Image of your ImageCollection that does not modify your data, you can use .mosaic() to create a single layerstack. Also, according to the Google developers, clipping should not be done unnecessarily in the GEE, as it will actually increase the computation time of your subsequent analysis. If you want or need to clip your raster to your geometry, it is best advised to do so at the end of your analysis.

//Turn the ImageCollection to a single layerstack
var s2a_image = s2a_cloudfree.mosaic()
print(s2a_image, 'Layerstack Image 2020 Lebanon, < 10% Cloud Cover')

//Clip the layerstack to the extent of the geometry
var s2a_clip = s2a_image.clip(extent_lebanon)
print(s2a_clip, 'Layerstack Image 2020 Lebanon clipped, < 10% Cloud Cover')

//Add the result to the map
Map.addLayer(s2a_clip, visParams ,'Sentinel2 clipped');

Reduce

Another way to easily transform your ImageCollections to a single image is to aggregate your data using so called Reducers. In the Docs tab, browse to ee.Reducer to find a comprehensive list of all Reducers available as well as a documentation on how to correctly implement them into your script. The functionality of the Reducers can range from simple statistics like min/max, mean, median or standard deviation to advanced aggregations of the input data like histograms, linear regressions or lists.

For this first lesson, we will be using ee.Reducer.mean(), ee.Reducer.median() and ee.Reducer.minMax().

var s2_mean = s2a_cloudfree.reduce(ee.Reducer.mean())
              .clip(extent_lebanon);        //The result can be clipped, as it is an Image!
print(s2_mean, 'IC 2020 reduce mean');


var s2_median = s2a_cloudfree.reduce(ee.Reducer.median())
              .clip(extent_lebanon);
print(s2_median, 'IC 2020 reduce median');


var s2_minMax = s2a_cloudfree.reduce(ee.Reducer.minMax())
              .clip(extent_lebanon);
print(s2_minMax, 'IC 2020 reduce minMax');

//Have a look at the Console; The band names have changed! To visualize the data, we will have to adjust visParams!
//Special care is to be taken for reduce.minMax: Here, we got double the bands, since we are aggregating both min AND max values!
//Try to find better fitting min and max values by using the Inspector on the map!

var visParams_mean = {'min': 400,'max': [4000,3000,3000],   'bands':'B8_mean,B4_mean,B3_mean'};
var visParams_median = {'min': 400,'max': [4500,3000,3000],   'bands':'B8_median,B4_median,B3_median'};
var visParams_min = {'min': 400,'max': [4000,3000,3000],   'bands':'B8_min,B4_min,B3_min'};
var visParams_max = {'min': 400,'max': [8000,9000,9000],   'bands':'B8_max,B4_max,B3_max'};

Map.addLayer(s2_mean, visParams_mean, 'IC 2020 reduce mean');
Map.addLayer(s2_median, visParams_median, 'IC 2020 reduce median');
Map.addLayer(s2_minMax, visParams_min, 'IC 2020 reduce min');
Map.addLayer(s2_minMax, visParams_max, 'IC 2020 reduce max');