Back to Course

## Urban Spaces

0% Complete
0/14 Steps

#### Quizzes

In Progress
Lesson 1, Topic 4
In Progress

# Classifying urban areas using Sentinel 2 data

##### Jonathan Landwehr
Lesson Progress
0% Complete

Introduction to Sentinel 2 Dataset

In the third and final use case, you will classify your dataset using the random forest algorithm on sentinel two data. As in the two use cases before you’ll at first get a brief introduction to the currently used dataset.

Now you are ready to start the analysis using the code editor of google earth engine. In this last hands on use case, you’ll focus on a supervised classification example for classifying urban and non-urban areas with the use of sentinel 2 data. Before you’ll move on with the example, you’ll firstly calculate different indices like the Normalized Difference Vegetation Index (NDVI) or the Normalized Difference Bare Index (NDBI) in order to visualize different parameters like for example the vegetation cover.

You can find the code used in this video below or by following this GEE Link:

As soon as you have pre-processed your sentinel dataset, you are ready to do the analysis. To do so you will learn how you can create and manage your data so that it can be used to train and validate the final classification task.

### Code used in this example:

```// Functions to prepare the data

//Function to mask clouds using the Sentinel-2 QA band
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.
}

// Function to calculate spectral indices
var getND = function(image){
// the normalized difference vegetation index
var ndvi = image.normalizedDifference(['B8', 'B4']);
// the normalized difference bare index
var ndbi = image.normalizedDifference(['B12', 'B8']).rename('ndbi');
// the normalize difference water index
// var ndwi = image.normalizedDifference(['B3', 'B8']);
return original;
};

// Get Sentinel 2 data and filter it by time, location and cloud cover
var dataset = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate('2020-05-01', '2020-09-30')
.filterBounds(roi)
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
.map(getND); // apply function to add spectral indices

var visualization = {  min: 0.0, max: 0.3, bands: ['B4', 'B3', 'B2'],};

var s2_maximum = dataset.max()

// Visualise vegetation using NDVI treshold
var vegetationThreshold = 0.65
var ndvi_th = input.select('nd').gt(vegetationThreshold)
var ndvi_viz = {palette:"#088222"};

// Load reference data points. The numeric property 'class' stores known labels.
// The randomColumn() method will add a column of uniform random
// numbers in a column named 'random' by default.
var withRandom = ref.randomColumn('random');
var split = 0.7;  // split the reference data: 70% training, 30% testing.
var trainingPartition = withRandom.filter(ee.Filter.lt('random', split));
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));

// Sample the input imagery to get a FeatureCollection of training data.
var training = input.sampleRegions({
// Get the sample from the polygons FeatureCollection.
collection:trainingPartition,
properties: ['class'],
tileScale: 16,
scale: 10
});

// Make a Random Forest classifier and train it.
var classifier = ee.Classifier.smileRandomForest(300)
.train({
features: training,
classProperty: 'class',
inputProperties: ['B2', 'B3', 'B4', "B8",  "ndbi", 'nd', "nd_1"]
});

// Classify the input imagery.
var classified = input.classify(classifier);
// Display the input and the classification.
//Map.centerObject(roi, 10);
//Map.addLayer(classified, {palette: ["white", "black"], min: 0, max: 1}, 'classification');

// Sample the input with validation data.
var testing = input.sampleRegions({
collection:testingPartition,
properties: ['class'],
tileScale: 16,
scale: 10
});

// Classify the validation data.
var validated = testing.classify(classifier);

// Get a confusion matrix representing expected accuracy.
var testAccuracy = validated.errorMatrix('class', 'classification');
//print('Validation error matrix: ', testAccuracy);
//print('Validation overall accuracy: ', testAccuracy.accuracy());

```

Click the ‘Submit’ Button to rate this topic.