Difference between revisions of "Supervised Classifications using Google Earth Engine"

From CUOSGwiki
Jump to navigationJump to search
(Created page with "=Introduction= =Getting Started= ==Getting Images== ===Displaying Images=== ===Creating a Cloud-Free Mosaic=== ==Preparing Images== ===Adding Bands=== =Getting Training Data...")
 
Line 2: Line 2:
   
 
=Getting Started=
 
=Getting Started=
  +
We will now begin preparing the image(s) that we will be classifying.
  +
 
==Getting Images==
 
==Getting Images==
  +
Google Earth Engine has a large selection of remote sensing data to choose from. In this example, I will be using the '
===Displaying Images===
 
  +
USGS Landsat 8 Collection 1 Tier 1 Raw Scenes' collection. This is a collection of the highest quality images available through Landsat 8.
  +
  +
Define a variable 'L8' so that you can easily reference this entire collection:
  +
<syntaxhighlight lang="javascript">
  +
var L8 = ee.ImageCollection('LANDSAT/LC08/C01/T1');
  +
</syntaxhighlight>
  +
 
===Creating a Cloud-Free Mosaic===
 
===Creating a Cloud-Free Mosaic===
  +
Now that we have a variable named L8 that is linked to the Landsat 8 collection that we want, we can begin to filter images and choose what we want. For this example, I will create a cloud-free mosaic of Fort McMurray in the month after the major fire that swept through in May of 2016. I have created two functions for this, one that creates a cloud mask and the other that creates a simple composite from a collection of images.
  +
  +
#By utilizing the 'BQA' band present in Landsat 8 imagery, we can create a cloud mask to remove any cloud pixels that may alter the result. I created a function that will select any pixel that has cloud cover with even moderate confidence. The function will return a mask that removes all of these pixels from an image. Note that I have commented on this one fairly heavily due to it be slightly confusing at first.
  +
##I should add again, if you haven't already explored all the bands of your chosen imagery, do. There are plenty of interesting things that can be used.
  +
<syntaxhighlight lang="javascript">
  +
// Cloud mask for L8 imagery
  +
var L8CloudMask = function(L8) {
  +
var qa = L8.select('BQA'); //selects the BQA (quality) band
  +
var CloudOrShadow = qa.bitwiseAnd(1<<4) //chooses pixels where cloud is present
  +
.and(qa.bitwiseAnd(1<<6)) //and pixels where cloud confidence ic greater than 34%
  +
.or(qa.bitwiseAnd(1<<8)) //or pixels where cloud shadow confidence is greater than 34%
  +
.or(qa.bitwiseAnd(1<<12)); //or pixels where cirrus confidence is greater than 34%
  +
var masked = L8.mask(); //creates a mask
  +
return L8.updateMask(CloudOrShadow.not()).updateMask(masked); //updates mask to remove any pixels as described above in 'CloudOrShadow'
  +
};</syntaxhighlight>
  +
  +
  +
#Now onto the composite. This is a much simpler function, it simply intakes a collection of images and uses the built in 'simpleComposite' function to combine them into the best possible image.
  +
<syntaxhighlight lang="javascript">
  +
// Function to create composite
  +
var createComp = function(collection) {
  +
var comp = ee.Algorithms.Landsat.simpleComposite({
  +
collection: collection,
  +
asFloat: true
  +
});
  +
return comp;
  +
};</syntaxhighlight>
  +
  +
#With the two main functions created, we can now use a few lines to create our final image.
  +
<syntaxhighlight lang="javascript">
  +
// Select all images within June 2016 that touch the extent and have the least removed from the cloud mask
  +
var after = ee.ImageCollection('LANDSAT/LC08/C01/T1')
  +
.filterDate('2016-06-01', '2016-06-30')
  +
.filterBounds(extent)
  +
.map(L8CloudMask);
  +
  +
// Create composite of images chosen above
  +
var compAfter = createComp(after);
  +
// Clip to extent
  +
var clipAfter = compAfter.clip(extent);</syntaxhighlight>
  +
  +
  +
  +
 
==Preparing Images==
 
==Preparing Images==
 
===Adding Bands===
 
===Adding Bands===
 
===Displaying Images===
   
 
=Getting Training Data=
 
=Getting Training Data=

Revision as of 11:21, 16 November 2020

Introduction

Getting Started

We will now begin preparing the image(s) that we will be classifying.

Getting Images

Google Earth Engine has a large selection of remote sensing data to choose from. In this example, I will be using the ' USGS Landsat 8 Collection 1 Tier 1 Raw Scenes' collection. This is a collection of the highest quality images available through Landsat 8.

Define a variable 'L8' so that you can easily reference this entire collection:

var L8 = ee.ImageCollection('LANDSAT/LC08/C01/T1');

Creating a Cloud-Free Mosaic

Now that we have a variable named L8 that is linked to the Landsat 8 collection that we want, we can begin to filter images and choose what we want. For this example, I will create a cloud-free mosaic of Fort McMurray in the month after the major fire that swept through in May of 2016. I have created two functions for this, one that creates a cloud mask and the other that creates a simple composite from a collection of images.

  1. By utilizing the 'BQA' band present in Landsat 8 imagery, we can create a cloud mask to remove any cloud pixels that may alter the result. I created a function that will select any pixel that has cloud cover with even moderate confidence. The function will return a mask that removes all of these pixels from an image. Note that I have commented on this one fairly heavily due to it be slightly confusing at first.
    1. I should add again, if you haven't already explored all the bands of your chosen imagery, do. There are plenty of interesting things that can be used.
// Cloud mask for L8 imagery
var L8CloudMask = function(L8) {    
  var qa = L8.select('BQA');  //selects the BQA (quality) band
  var CloudOrShadow = qa.bitwiseAnd(1<<4) //chooses pixels where cloud is present
            .and(qa.bitwiseAnd(1<<6)) //and pixels where cloud confidence ic greater than 34%
            .or(qa.bitwiseAnd(1<<8))  //or pixels where cloud shadow confidence is greater than 34%
            .or(qa.bitwiseAnd(1<<12));  //or pixels where cirrus confidence is greater than 34%
  var masked = L8.mask(); //creates a mask
  return L8.updateMask(CloudOrShadow.not()).updateMask(masked); //updates mask to remove any pixels as described above in 'CloudOrShadow'
};


  1. Now onto the composite. This is a much simpler function, it simply intakes a collection of images and uses the built in 'simpleComposite' function to combine them into the best possible image.
// Function to create composite
var createComp = function(collection) {
  var comp = ee.Algorithms.Landsat.simpleComposite({ 
    collection: collection,
    asFloat: true
  });
  return comp;
};
  1. With the two main functions created, we can now use a few lines to create our final image.
// Select all images within June 2016 that touch the extent and have the least removed from the cloud mask
var after = ee.ImageCollection('LANDSAT/LC08/C01/T1') 
        .filterDate('2016-06-01', '2016-06-30') 
        .filterBounds(extent)
        .map(L8CloudMask);

// Create composite of images chosen above
var compAfter = createComp(after);
// Clip to extent
var clipAfter = compAfter.clip(extent);



Preparing Images

Adding Bands

Displaying Images

Getting Training Data

Create Landcover Polygons

Gather Data from Polygons

Create Training and Validation Sets

Assess Useability of Data

histograms etc

Classification

Test on Validation Data Set

error matrix

Run on Chosen Image

===Calculating Area of Each Class

Creating a Chart of Landcover