Introduction

  • Layout Scripts, Docs, Assets, Code editor, Inspector, Console, Tasks, Search, Map
  • Declaring variables var
  • Line endings ;
  • Commenting CTRL+/ or CTRL+SHIFT+/
  • Run CTRL+ENTER

Data types

There are many data types in earth engine. Numbers, lists, strings, images, image collections, features, feature collections, geometries, etc. Here we’ll start by looking at the ee.Image() data type.

ee.Image

If you know the path to a single image, you can insert it into ee.Image(). From there, print the image metadata, and add it to the map.

/* ADD IMAGE */
  var imgS2 = ee.Image("COPERNICUS/S2_SR_HARMONIZED/20230910T191949_20230910T191944_T10UEE");
  print("Image details: ", imgS2);
  Map.addLayer(imgS2);

ee.ImageCollection

Now, we load the entire Sentinel-2 Harmonized Surface Reflectance collection. We can print the details of the first image and add a mosaic to the map using .mosaic().

/* ADD IMAGE COLLECTION */
  var col = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED");
  print("First image detail:",col.first());
  Map.addLayer(col.mosaic());

ee.FeatureCollection

Feature Collections are similar to Image Collections. They are a collection of ee.Feature() with similar attributes. There are many colections built in to Earth Engine.

/* ADD FEATURE COLLECTION */
  var countries = ee.FeatureCollection('FAO/GAUL_SIMPLIFIED_500m/2015/level0');
  print("Total size of country collection:",countries.size());
  print("First country details:",countries.first());
  Map.addLayer(countries);

Filter Feature Collections

Filter the countries dataset to only show Canada.

/* FILTER FEATURE COLLECTION */
  var canada = countries.filterMetadata("ADM0_NAME", "equals", "Canada");
  Map.addLayer(canada);

Digitize point and buffer

Create a point named pg over Prince George, BC using the user interface. Create a 20 km buffer around that point.

/* DIGITIZE GEOMETRY AND BUFFER */
  var aoi = pg.buffer(20*1000);
  Map.addLayer(aoi);

Filter Image Collections

Filter the Sentinel-2 collection using the aoi you created, filter the date range, and filter the cloud coverage. Print how many images remain, and add them to the map.

/* FILTER IMAGE COLLECTION */
  var col_filter = col
    .filterBounds(aoi)
    .filterDate("2023-07-01","2023-09-01")
    .filterMetadata("CLOUDY_PIXEL_PERCENTAGE","less_than",20);
  print("Total filtered images over PG: ", col_filter.size());
  Map.addLayer(col_filter);

Function – Clip and Scale images

A function() can be mapped over every ee.Image() of an ee.ImageCollection(). Here we clip() every image using our aoi and we scale the image by 0.0001.

/* FUNCTIONS - CLIP and SCALE */
  var col_filter_clip = col_filter
    .map(function(img){return img.multiply(0.0001).clip(aoi).copyProperties(img)});
  Map.addLayer(col_filter_clip, {}, "Clip");

Mosaic

There are many ways to reduce a collection into a mosaic. Here we show an ordered mosaic vs. a statistical mosaic.

/* ORDERED MOSAIC */
  var col_filter_clip_mosaic = col_filter_clip.mosaic();
  Map.addLayer(col_filter_clip_mosaic, {}, "Mosaic");
/* STATISTICAL MOSAIC */
  var col_filter_clip_median = col_filter_clip.median();
  Map.addLayer(col_filter_clip_median, {}, "Median");

Style Rasters

The second argument of Map.addLayer() is the visualization parameters. Here we style the median mosaic in three different ways.

/* IMAGE STYLING  */
  Map.addLayer(col_filter_clip_median, {bands: ["B4","B3","B2"], min: 0, max: 0.2}, "RGB");
  Map.addLayer(col_filter_clip_median, {bands: ["B8","B4","B3"], min: 0, max: 0.3}, "NIR");
  Map.addLayer(col_filter_clip_median, {bands: ["B11","B8","B4"], min: 0, max: 0.4}, "SWIR");

Function – NDVI

We apply an NDVI function to the statistical mosaic.

/* FUNCTIONS - NDVI */
  var ndviFunction = function(img){
    var ndvi = img.normalizedDifference(["B8","B4"]).rename("ndvi");
    return img.addBands(ndvi);
    };

  var col_filter_clip_median_ndvi = ndviFunction(col_filter_clip_median);
  print(col_filter_clip_median_ndvi);
  
  var col_filter_clip_median_ndvi_sel = col_filter_clip_median_ndvi.select("ndvi");
  print(col_filter_clip_median_ndvi_sel);
  Map.addLayer(col_filter_clip_median_ndvi_sel, {}, "NDVI");

Raster to Vector

We threshold the NDVI layer at 0.4. So >0.4 is assumed to be productive vegetation, and < 0.4 is not. Note the difference of how we style vectors in Earth Engine.

/* RASTER TO VECTOR */
  var ndvi_th = col_filter_clip_median_ndvi_sel.gt(0.4);
  Map.addLayer(ndvi_th, {palette: ["red","green"], min:0, max: 1}, "NDVI Threshold");
  
  var ndvi_th_vec = ndvi_th
    .reduceToVectors({
      geometry: aoi,
      scale: 100,
      geometryType: 'polygon',
      eightConnected: false});

  Map.addLayer(ndvi_th_vec.style({fillColor: "green", color: "black", width: 1}), {}, "NDVI Vector");

Export to Google Drive

Here we export 4 different datasets from Earth Engine to Google Drive.

/* EXPORT IMAGES AND VECTORS */

 Export.image.toDrive({
    image: col_filter_clip_median, 
    description: "PG_S2_RAW", 
    folder: "Lab1", 
    region: aoi, 
    scale: 20, 
    crs: "EPSG:3005"});

Export.image.toDrive({
    image: col_filter_clip_median.visualize({bands: ["B11","B8","B4"], min: 0, max: 0.4}), 
    description: "PG_S2_SWIR", 
    folder: "Lab1", 
    region: aoi, 
    scale: 20, 
    crs: "EPSG:3005"});
    
  Export.image.toDrive({
    image: col_filter_clip_median_ndvi_sel, 
    description: "PG_S2_NDVI", 
    folder: "Lab1", 
    region: aoi, 
    scale: 20, 
    crs: "EPSG:3005"});
  
  Export.table.toDrive({
    collection: ndvi_th_vec, 
    description: "PG_S2_NDVI_thresh", 
    folder: "Lab1", 
    fileFormat: "SHP"});

Questions

Send your answers to bevington@unbc.ca by email before the end of the lab.

  • What is the difference between band B8 and B8A in imgS2? Mention the Resolution, Wavelength, and Description.
  • What happens when you try to print the entire Sentinel-2 collection?
  • What is the difference between FAO/GAUL_SIMPLIFIED_500m/2015/level0 and FAO/GAUL_SIMPLIFIED_500m/2015/level1?
  • What is the description of the Sentinel-2 SR Harmonized CLOUDY_PIXEL_PERCENTAGE attribute in Earth Engine?
  • Open the three rasters in QGIS. What is the data type of each one?
Categories: GEOG 457Labs