Académique Documents
Professionnel Documents
Culture Documents
JAVASCRIPT
1. GENERALITES 2
5.1. PREREQUIS 4
5.2. SE CONNECTER 4
6. RESSOURCES 19
PAGE 1
1. GENERALITES
Google Earth Engine est une plate-forme cloud pour l'analyse des données géospatiales à
l'échelle mondiale. Les principaux composants de Earth Engine sont:
Base de données: Une archive d'images satellites et autres bases de données geospatiales
disponibles en libre accès.
Puissance de Calcul: une infrastructure de calcul comprenont plus de 20.000 serveurs Google
optimisée pour le traitement parallèle des données géospatiales.
API: API pour JavaScript et Python (hébergées sur GitHub) pour effectuer des requêtes aux
serveurs Earth Engine. Ces documents se concentrent sur JavaScript. (Guide de démarrage
pour l'API Javascript). Pour Python, consultez le guide d'installation Python et les exemples
Python dans le référentiel Earth Engine GitHub.
Éditeur de code: Un environnement de développement intégré (IDE) en ligne pour le
prototypage rapide et la visualisation d'analyses spatiales complexes à l'aide de l'API
Javascript.
Google Earth Engine comprend :
Earth Explorer : https://explorer.earthengine.google.com/
Earth Code Editor : https://code.earthengine.google.com/
PAGE 2
2. PRENDRE SES MARQUES DANS GEE
Google Earth Engine permet aux utilisateurs d'exécuter des algorithmes sur des images satellites
géoréférencées et des données vecteurs stockées sur l'infrastructure de Google.
L'API JavaScript de Google Earth Engine fournit une bibliothèque de fonctions permettant la
visualisation et l’analyse de données issues de satellites. En effet, le catalogue de données
publiques de Earth Engine donne accès à une grande quantité d'images et de données
vectorielles.
Il est possible d’ajouter ses propres données et les intégrer à l’analyse.
Il faut donc bien comprendre que tout, absolument tout se fait en ligne ! Néanmoins, les résultats
des traitements peuvent être sauvegardés dans Google Drive, puis téléchargés par la suite.
PAGE 3
Utilisateur navigateur Earth Engine
5.1. PREREQUIS
- Notions sur les images satellites
- Notions sur le traitement d’images satellites
- Notions de programmation orientée objets
- Avoir un compte Gmail
5.2. SE CONNECTER
Pour utiliser Google Earth Engine, il faut :
- se connecter à l’adresse : https://code.earthengine.google.com
- Créer un compte
PAGE 4
3. GOOGLE EARTH ENGINE CODE EDITOR
Dans cette section nous allons pratiquer pour mieux apprendre. Dans un premier temps
familiarisons nous avec l’interface de GEE Code editor :
PAGE 5
4. INTRODUCTION A GEE JAVASCRIPT API
a. Premier script (Hello World)
// Create a dictionary called Stephen dictionary, that contains information about Stephen
var stephenDictionary = {
PAGE 6
// Create a 'name' property, that contains Stephen's name
name: "Stephen",
// Create a 'height' property, that contains Stephen's height
height: 2.0
}
// Create a function that has two parameters (aka, 'inputs', aka, 'parameters')
var myFunction = function(input1, input2) {
// displays the first input
print("input1 was", input)
// returns the second input
return input2
}
// This function always returns the number five. It has no parameters / arguments / inputs
var noInputs = function() {
return 5
}
// Use our function with number and the value 6. We 'invoke' (aka, 'call', aka, 'run')
// a function by putting parentheses after its name. Any aruments that we are passing
// to the function go in the parentheses.
myFunction(number, 6)
PAGE 7
return
}
// Should print: 3
print(addTwoNumbers(1, 2))
Le type imageCollections represente une collection d'images d’un capteur. Les collections
d'images peuvent être filtrées, de sorte que certaines images sont exclues en fonction d'éléments
tels que le lieu, l'heure ou le taux de nuages.
Les collections d'images peuvent être traitées en blocs grâce à la fonction « map ». Elle permet
en effet de modifier chaque image de la collection en une seule ligne de code. Les collections
d'images peuvent être réduites, chaque image de la collection étant aplatie en une seule image.
Dans ce script, nous allons charger une collection d'images. Ensuite, nous allons filtrer la
collection d'images, en sélectionnant uniquement les images qui sont dans une certaine
géométrie et dans un certain laps de temps. Ensuite, nous allons cartographier la collection
d'images, en masquant les pixels qui comportent beaucoup de nuages. Enfin, nous allons réduire
la collection d'images, et prendre une mosaïque ou médiane de la collection.
Exercice:
• définir une région d'intérêt
• Utiliser .filterBounds pour sélectionner uniquement les images dans une zone donnée.
• Utiliser .filterDates pour sélectionner uniquement ces images en 2017.
• Ajouter notre collection d'images à la carte.
PAGE 8
[
// Coordinates for the botom-left
2.2, 11.9,
// Coordinates for the top-right
2.5, 12.2
]
)
/*
Every Landsat scene has a 'CLOUD_COVER' property. This represents the
percentage
of the scene that is covered in clouds. We will filter out scenes that
are covered
by 40% clouds or greater. .filter() takes as a property an ee.Filter
object.
You can read more about the filters in the Docs tab. We are going to use
the
filter ee.Filter.lt('CLOUD_COVER', 0.4). Images only pass the filter if
their
'CLOUD_COVER' property is less than 0.4.
*/
PAGE 9
filteredImages = filteredImages
.filter(ee.Filter.lt('CLOUD_COVER', 0.4))
In this example, we will map a collection for clouds, so that every image
will
have its clouds masked. First, we must create a function that will mask
our
images. To determine what is clouds and what is not, we will use a built-
in
Earth Engine function, called ee.Algorithms.Landsat.simpleCloudScore().
This
takes a Landsat TOA image and returns the same image, with a new band,
'cloud',
that represents the cloudiness of this pixel. More information about
.simpleCloudScore()
is available in the Docs tab.
Once we have an image with information about how cloudy each pixel is, we
will
create a mask where pixels of a certain cloudiness are masked. Finally,
we will
return our masked image.
*/
PAGE 10
// We want to run the image through the
ee.Algorithms.Landsat.simpleCloudScore().
// This gives the percent chance that the pixel is a cloud.
image = ee.Algorithms.Landsat.simpleCloudScore(image)
// Now we can create a mask. .lte() (think, Less Than or Equal) returns
an image
// where each pixel is 0 if it fails the test, and 1 if it passes. So in
the
// mask image, each pixel will be 1 if it is less than or equal to 0.5,
or
// 0 if it is greater than 0.5.
// We want to create the cloud mask, from the 'cloud' band, so we select
that
// band from the image.
var cloudMask = image.select('cloud')
cloudMask = image.lte(0.5)
// We now apply that mask to the image, using .updateMask().
image = image.updateMask(cloudMask)
return image
}
/*
Compositing and mosaicking
We now have a collection of images, which we have filtered and masked
for clouds. We would now like to turn that collection of images into a
single
image. This will make it easier to analyze or export our information.
There are two techniques that we will look at for turning an image
collection into a
single image. One is mosaicking, and the other is compositing.
Mosaicking
There are two ways that we can mosaic in Earth Engine. We use the
.mosaic() function on
a collection, and Earth Engine will mosaic our masked images, giving
preference to
more recent images.
PAGE 11
that we pass.
*/
// Mosaic an image
var mosaickedImage = maskedImages.mosaic()
Map.addLayer(mosaickedImage, {min: 0, max: 0.3, bands:'B4,B3,B2'},
'Mosaicked Image')
/*
Compositing
Another way that we can turn a collection into a single image is by
using
compositing. We might take the min, the median, the max or the mean value
of a pixel. There are corresponding methods to composite a collection
(ie,
.min(), .max(), .median(), .mean() ). You can read more in the Docs tab.
Right
now, let's take the median value.
*/
// Composite an image
var compositedImage = maskedImages.median()
Map.addLayer(compositedImage, {min: 0, max: 0.3, bands:'B4,B3,B2'},
'Composited Image')
/*
Clipping an Image
You can see that our coposited image is much larger that our AOI. We
would like
to clip the image to our original AOI. We can clip an image by using the
clip function.
When we use .clip(), we pass the geometry that we would like to clip an
image to.
We can only use .clip on an image, not on an image collection. If we had
tried to use
this function on our collection before compositing, it would have given
an error.
*/
var clippedImage = compositedImage.clip(aoi)
Map.addLayer(clippedImage, {min: 0, max: 0.3, bands:'B4,B3,B2'}, 'Clipped
Image')
PAGE 12
/*
We have now successfully processed a Landsat collection into a single
image.
We have:
* Created an ee.ImageCollection variable that refers to the Landsat 8
Tier 1 TOA collection.
* Filtered that collection, limiting scenes based on date, on location,
and on cloud coverage.
* Mapped over that collection, to mask cloudy pixels.
* Composited or mosaicked that collection of images into a single
image.
* Clipped that composite image to our original area of interest.
*/
/*
Optional: Chaining function calls
Up to this point, we have been assigning the returned value of each
function
to a new variable, and passing that new variable to the next function,
and so on.
It is possible, however, for us to directly pass the results of each
function
to the next, without having to declare a new variable. The result of the
functions
is exactly the same: we only do this because it is easier to read and
easier to write.
Furthermore, we can put a line break between function calls that we have
chained
together in this way. Below is our function rewritten so that all of the
function
calls are chained together.
*/
PAGE 13
Map.addLayer(chainedImage, {min:0, max:0.3, bands:'B4,B3,B2'}, 'chained
image')
/*
Normalized difference
/*
To calculate spectral indices, we are going to use the
ee.Image().normalizedDifference()
function. Using the Docs tab, we can see that normalizedDifference() is a
function
that acts on an image. It has one input: a list containing the names of
the
two bands that are to be used for the normalized difference calculation.
The
returned value is an image where each pixel is equal to:
(first − second) / (first + second)
*/
PAGE 14
/*
We now need to determine what bands we are going to use for different
spectral
indices. Using the Docs tab, we saw that the function's input is a list
of strings
representing the names of the bands we are going to use for our
calculations.
We can use the Landsat spectral indices guide
(https://landsat.usgs.gov/sites/default/files/documents/si_product_guide.pd
f)
to see which bands to use for different indices. For now, we will only
use NDVI.
*/
/*
Now, let's add our NDVI image to the map. We will give it a palette
from the colors
'brown' to the color 'green', and it will stretch the values between
these two colors.
Using the inspector, you can click around on your NDVI image to see what
values are present.
Based on what values you see, choose a min and max value for your
visualiztion.
(replace the null values below)
*/
/*
Compare your image with your neighbor.
*/
/*
Sentinel NDVI
We have created a Landsat NDVI. Now, let's create a Sentinel NDVI. The
Sentinel 2
NDVI bands are Bands 4 and 8 (I think...)
*/
PAGE 15
var sentinelNdviBands = ["B4", "B8"]
var sentinelNdviImage =
sentinelImage.normalizedDifference(sentinelNdviBands)
Map.addLayer(sentinelNdviImage, {min: null, max: null, palette:
'brown,green'}, 'Landsat NDVI')
/*
=====
Exercises
=====
*/
/*
Getting NDMI
Let's try getting a different kind of spectral index. In Landsat 8, the
NDMI bands
are Band 5 and Band 6. The code below almost works, but it's missing a
little bit.
Replace the null values to make the code work.
*/
var landsatNdmiBands = [null, null]
var landsatNdmiImage = landsatNdmiImage.normalizedDifference(null)
Map.addLayer(landsatNdmiImage, {min: null, max: null, palette:
'white,blue'}, 'Landsat NDMI')
/*
getNdvi function
It would be quite convenient if we could create a function that would
calculate
an image's NDVI and return it. Let's create a function called getNdvi
that takes
a Landsat 8 image as an input and returns that image's NDVI. Replace the
null values.
*/
var getNdvi = function (landsatImage) {
var landsatNdviBands = null
var landsatNdviImage = null
return landsatNdviImage
}
/*
======
Extra
======
PAGE 16
(The following is advanced. We won't go over it in the workshop, but you
may find
it useful.)
Custom Expressions
We saw how to generate a normalized difference index. That calculation
is fairly simple. It is possible to use custom defined indices instead.
We will
use the example of Enhanced Vegetation Index.
*/
// To generate our EVI, we use ee.Image().expression. This takes two
arguments:
// a string defining the arithmetic function to be used to calculate the
index,
// and an optional dictionary, that creates variables that we can use in
our expression.
// You can read more in the Docs tag.
PAGE 17
CLASSIFICATION NON SUPERVISE
PAGE 18
5.4. UPLOADER SES PROPRES DONNEES
Le upload de données s’effectue via l’ongte Assets. L’utilisateur peut importer des images
TIFF/GeoTIFF ou des shapes files et les organiser en ligne.
6. Ressources
a. Documentation officielle
PAGE 19
https://developers.google.com/earth-engine/
b. Forum de développeurs
https://github.com/google/earthengine-api/tree/master/demos
https://developers.google.com/earth-engine/tutorial_api_01
PAGE 20