Today I am going to give you a short introduction into the Google Earth Engine and show you how to create a cloud free mosaic of Europe using Sentinel-2 data in just one minute! Yes, literally one minute!
The Google Earth Engine is a “computing platform that allows users to run geospatial analysis on Google’s infrastructure.” This means that you don’t have to download any data yourself and that you can perform high speed data analysis of all kinds of available satellite imagery on your laptop, computer or even your cell phone if you wish by using the computantional power of Google.
1. Sign up at earthengine.google.com
No worries, it’s free of charge if you are a researcher or simply want to explore the possibilities and power of the Google Earth Engine. However if you want to use it for commercial purposes, you should get in touch with Goolge first.
2. Go to code.earthengine.google.com
Google Earth Engine offers a couple of interfaces. In our case study we are going to use the code editor interface. Via this interface you can make us of a variety of already existing code chunks which will help you with your analysis and the processing of all kinds of satellite imagery. You can find a short intro into the Google Earth Engine API here.
The following illustration shows the main features of the code editor:
3. Select an existing script from the script manager
From the script manager on the left side select “Filtered Composite”. This script takes an polygon extent as an input:
var polygon = ee.Geometry.Polygon([[ [-109.05, 37.0], [-102.05, 37.0], [-102.05, 41.0], // colorado [-109.05, 41.0], [-111.05, 41.0], [-111.05, 42.0], // utah [-114.05, 42.0], [-114.05, 37.0], [-109.05, 37.0]]]);
And then selects all available Landsat 7 scenens from April 2000 until July 2000:
var collection = ee.ImageCollection('LE7_L1T') .filterDate('2000-04-01', '2000-07-01') .filterBounds(polygon);
In a third step the median of all of these images is calculated and displayed to the map:
// Select the median pixel. var median = collection.median(); // Select the red, green and blue bands. var result = median.select('B3', 'B2', 'B1'); Map.addLayer(result, {gain: '1.4, 1.4, 1.1'}); Map.setCenter(-110, 40, 5);
The result is a Landsat 7 mosaic over Colorado and Denver. You can execute the code by clicking the “Run” button. It takes only a couple of seconds to compute! Amazing!
4. Pushing the limits – Europe Sentinel-2 cloud free. How long will it take?
Let’s test how long the same procedure will take for the entire continent of Europe. And lets use Sentinel-2 instead!
Any guess how long it will take? Here are the code adaptations I have made:
Polygon over Europe:
var geometry = /* color: #d63000 */ee.Geometry.Polygon( [[[-26.60888671875, 66.01801815922043], [-18.1494140625, 32.565333160841035], [15.954649288064843, 35.39010560236367], [44.80224609375, 36.756490329505176], [43.59375, 70.85188122123132], [11.88720703125, 73.23937702441908]]]); var polygon = geometry;
This time we want Sentinel-2 data, so let’s change “LE7_L1T” to “COPERNICUS/S2” and specify the time frame to April 2016 until December 2016:
Voila! We are done! How long did it take? About a minute!! Wow, stunning! Here the result:
16 Comments
You can post comments in this post.
Hi Great work, congrats!
Can i use this code for Indian region?
thanks
Raj
RAJ KUMAR SINGH 6 years ago
Thanks! You can use it for any region. You just need to adapt the extent of the polygon variable.
Martin 6 years ago
By taking the minimum value an issue is that you get the cloud shadows.
Simon 6 years ago
Hey Simon! Thanks for the comment!
You are absolutely right! The post is meant as a proof of concept and shows the computantional potential of GEE which is stunning.
For an entirely cloud- and shadow free mosaic a more advanced algorithm is of course needed. 😉
However, I was still stunned that “min” performed that well. I was expecting many more artefacts!!
Cheers
Martin
BTW: I am a big fan of your blog and your work at “series temporelles”
Martin 6 years ago
Dear Simon,
Thank you so much for the presentation.
Please, How to assign coordinates to polygon?
Best,
Freddy Argotty 6 years ago
Dear Freddy,
which presentation are you refering to?
To answer your question;
You may either directly change the coordinates in the variable polygon (if you know them):
var geometry = /* color: #d63000 */ee.Geometry.Polygon( [[[-26.60888671875, 66.01801815922043],
[-18.1494140625, 32.565333160841035], [15.954649288064843, 35.39010560236367], [44.80224609375, 36.756490329505176],
[43.59375, 70.85188122123132], [11.88720703125, 73.23937702441908]]]);
Or if you prefer to draw a polygon, you first delete the existing geometry variable by clicking on the small trash can symbil right next to the variable and then simply draw a new polygon on the map.
If you need any assistance with that, let me know.
Martin 6 years ago
Hi Martin,
I have adapted your instructions to my area of interest and it works perfectly!
Now, what would be the best way to export all S2 images that are composing the mosaic?
I would also like to export 4 bands (B8, B4, B3 and B2) instead of 3.
Thanks
Michele 6 years ago
Do you want to export the final mosaic or all the images that are composing the mosaic?
Martin 6 years ago
i want to export pakistan sentinal -2 RGB mosaic at one time or in tiles plz help me out seamless mosaic
izwa 6 years ago
Hey!
First, you need to draw a polygon around your desired area of interest (pakistan) inside the goolge earth engine.
Secondly, you need to export your visualization. I usually export my files to google drive as follows (simply add the lines of code at the bottom of the script and adapt it where needed):
// Export the image, specifying scale and region.
Export.image.toDrive({
image: imagename, //replace with your image name variable
description: 'pakistan_mosaic',
scale: 10,
maxPixels: 1e9,
region: geometry, //polygon name that you have drawn at step 1
folder:'mosaic_pakistan',
crs: "EPSG:3857"
});
Martin 6 years ago
want to export Afghanistan Sentinal -2 RGB mosaic with cloud masked, could u please help me how to download I used this guideline, unfortunately, was not able to download.
Thanks
Abdul Basir Mahmoodzada 4 years ago
Thank you for this, it helped me. Is there a way to calculate NDVI for selected region? If so, could you take some time to write the code?
Ljubisa Jurosevic 5 years ago
Nice work!
May I ask a question about the atmospheric correction of Setinel-2, L-1C product?
Is there any code for its’ atmospheric correction in Google earth engine?
Thanks
Zhonghua He 4 years ago
Hi,
I’ve tried your script, but it came up with this message –> Layer 1: Layer error: Image.select: Pattern ‘B4’ did not match any bands
Do you have any suggestion ?
This is my complete script:
var geometry =
/* color: #0b4a8b */
/* displayProperties: [
{
“type”: “rectangle”
}
] */
ee.Geometry.Polygon(
[[[94.005859375, 8.059229627200192],
[94.005859375, -12.64033830684679],
[142.697265625, -12.64033830684679],
[142.697265625, 8.059229627200192]]], null, false);
var kotak = geometry
var collection = ee.ImageCollection(‘COPERNICUS/S2_SR’)
.filterDate(‘2016-04-01’, ‘2018-12-31’)
.filterBounds(kotak);
var min = collection.min();
// Select the red, green and blue bands.
var result = min.select(‘B4’, ‘B3’, ‘B2’);
Map.addLayer(result, {gain: ‘0.1, 0.1, 0.1’, scale:20});
Map.setCenter(118, 0, 4);
Bayu Prayudha - 4 years ago
Hi, thank you for script above. I just replace my coordinates for Guyana, South America. Now, I would like to create a cloud mask file for sentinel 2, band b2, b4, b8 and b11. At the end I need to calculate NDVI. However, I have a problem to understand the concept of cloudmasking using Sentinel 2. Could you explain?
Susy
Susy Lewis 4 years ago
Hi, thans for your introduction. I wonder how to get the second or the third minimum value instead of the minimum?
Longlong Yu 4 years ago
Post A Reply