Skip to content
Extraits de code Groupes Projets
Valider 1ac9418d rédigé par Dries De Bièvre's avatar Dries De Bièvre
Parcourir les fichiers

s2_gee_2025 script

parent 22dda0d5
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
%% Cell type:markdown id:7aee23a0-e716-4370-b590-307f2a6351a4 tags:
# Load Sentinel-2 Surface Reflectance
The Sentinel-2 L2 data are downloaded from the Copernicus DataSpace. They were computed by running Sen2Cor.
All information concerning *Harmonized Sentinel-2 MSI: MultiSpectral Instrument, Level-2A* are available [here](https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2_SR_HARMONIZED#description)
```js
// Load Harmonized Sentinel-2 MSI: MultiSpectral Instrument, Level-2A
var s2_sr = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED")
```
```{note}
The Sentinel Level-1C and Level-2A data products have a harmonized time series. The "harmonized" designation means that the band-dependent offset added to reflectance bands in the 04.00 processing baseline has been removed. The offset affects data after January 24th 2022; removing the offset puts these data in spectral alignment with pre-04.00 baseline data. If you are using COPERNICUS/S2 or COPERNICUS/S2_SR, it is recommended that you switch to COPERNICUS/S2_HARMONIZED and COPERNICUS/S2_SR_HARMONIZED.
```
## Filter Sentinel-2 data
```js
// Define time period
var startDate = '2019-01-01'
var endDate = '2019-12-31'
```
```js
var fao_level1 = ee.FeatureCollection("FAO/GAUL/2015/level1")
var roi = fao_level1.filter("ADM1_CODE == 602")
```
```js
// Select S2 SR images in area of interest and time period
var s2_filter = s2_sr
.filterDate(startDate, endDate)
.filterBounds(roi)
// Pre-filter to get less cloudy granules.
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
```
%% Cell type:markdown id:51797ab3-9d2e-42b5-8315-c8c07dce9d27 tags:
# Cloud Mask
<p>Clouds can be removed by using the Sentinel-2 QA band
or the <a href="/earth-engine/datasets/catalog/COPERNICUS_S2_CLOUD_PROBABILITY">COPERNICUS/S2_CLOUD_PROBABILITY</a>.
## Mask clouds using the Sentinel-2 QA band
<section class="expandable">
<p class="showalways">Bitmask for QA60</p>
<ul>
<li> Bits 0-9: Unused
<ul>
</ul>
</li>
<li> Bit 10: Opaque clouds
<ul>
<li>0: No opaque clouds</li>
<li>1: Opaque clouds present</li>
</ul>
</li>
<li> Bit 11: Cirrus clouds
<ul>
<li>0: No cirrus clouds</li>
<li>1: Cirrus clouds present</li>
</ul>
</li>
</ul>
</section>
[More info](https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2_SR_HARMONIZED#bands)
```js
function maskS2cloudsQA(image) {
var qa = image.select('QA60')
// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10
var cirrusBitMask = 1 << 11
// Both flags should be set to zero, indicating clear conditions.
var cloud = qa.bitwiseAnd(cloudBitMask).eq(0)
var cirrus = qa.bitwiseAnd(cirrusBitMask).eq(0)
var mask = cloud
.and(cirrus)
var maskedImage = image.updateMask(mask).divide(10000);
return maskedImage
}
```
```js
var s2_mask_cloudsQA = s2_filter
.map(maskS2cloudsQA)
```
%% Cell type:markdown id:6f4134ce-a583-47b7-9580-555938ac0c74 tags:
# Composite
Reducers are the way to aggregate data over time, space, bands, arrays and other data structures in Earth Engine. The `ee.Reducer` class specifies how data is aggregated. The reducers in this class can specify a simple statistic to use for the aggregation (e.g. minimum, maximum, mean, median, standard deviation, etc.), or a more complex summary of the input data (e.g. histogram, linear regression, list).
Reductions may occur over :
- **time (`imageCollection.reduce()`),**
- space (`image.reduceRegion()`, `image.reduceNeighborhood()`),
- bands (`image.reduce()`),
- attribute space of a `FeatureCollection`** (`featureCollection.reduceColumns()` or `FeatureCollection` methods that start with `aggregate_`).
Consider the example of needing to take the median over a time series of images represented by an `ImageCollection`. To reduce an `ImageCollection`, use `imageCollection.reduce()`. This reduces the collection of images to an individual image. Specifically, the output is computed pixel-wise, such that each pixel in the output is composed of the median value of all the images in the collection at that location. To get other statistics, such as mean, sum, variance, an arbitrary percentile, etc., the appropriate reducer should be selected and applied.
```{note}
For basic statistics like min, max, mean, etc., `ImageCollection` has shortcut methods like `min()`, `max()`, `mean()`, etc. They function in exactly the same way as calling `reduce()`, except the resultant band names will not have the name of the reducer appended.
```
**To composite images in an `ImageCollection`, use `imageCollection.reduce()`. This will composite all the images in the collection to a single image representing, for example, the min, max, mean or standard deviation of the images.**
![reducer](Reduce_ImageCollection.png)
```js
var s2_composite_cloudsQA = s2_mask_cloudsQA
.median()
.clip(roi)
```
## Visualization
```js
var visParams = {
bands: ['B4','B3','B2'],
min: 0.0,
max: 0.3,
};
```
```js
Map.centerObject(roi, 8)
Map.addLayer(s2_composite_cloudsQA, visParams, 'S2 SR masked QA60')
```
## Exportation to Google Drive
```js
// Get projection of the original image
var projection = s2_filter.first().projection().getInfo()
// Export the image, specifying the CRS, transform, and region.
Export.image.toDrive({
image: s2_composite_cloudsQA,
description: 'S2_2025_median_composite',
folder: 'LBRAT2104',
crs: projection.crs, // The base coordinate reference system of this projection (e.g. 'EPSG:4326')
crsTransform: projection.transform, // The transform between projected coordinates and the base coordinate system
region: roi
});
```
> Composite images created by reducing an image collection are able to produce pixels in any requested projection and therefore have no fixed output projection. Instead, composites have the default projection of WGS-84 with 1-degree resolution pixels. Composites with the default projection will be computed in whatever output projection is requested. A request occurs by displaying the composite in the Code Editor or by explicitly specifying a projection/scale as in an aggregation such as `ReduceRegion` or `Export`.
%% Cell type:code id:646300ef-0748-41d7-9a19-1b506e0616c0 tags:
``` python
```
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter