// Load Sentinel-1 C-band SAR Ground Range collection (log scale, VV,descending);
var collectionVV = ee.ImageCollection('COPERNICUS/S1_GRD')
.filter(ee.Filter.eq('instrumentMode', 'IW'))
.filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VV'))
.filter(ee.Filter.eq('orbitProperties_pass', 'DESCENDING'))
.filterMetadata('resolution_meters', 'equals' , 10)
.filterBounds(roi)
.select('VV');
print(collectionVV, 'Collection VV');

In [ ]:

// Load Sentinel-1 C-band SAR Ground Range collection ( log scale, VH, descending)
var collectionVH = ee.ImageCollection('COPERNICUS/S1_GRD')
.filter(ee.Filter.eq('instrumentMode', 'IW'))
.filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VH'))
.filter(ee.Filter.eq('orbitProperties_pass', 'DESCENDING'))
.filterMetadata('resolution_meters', 'equals' , 10)
.filterBounds(roi)
.select('VH');
print(collectionVH, 'Collection VH')

In [ ]:

//Filter by date
var SARVV = collectionVV.filterDate('2019-12-01', '2019-12-10').mosaic();
var SARVH = collectionVH.filterDate('2019-12-01', '2019-12-10').mosaic();

In [ ]:

/ Add the SAR images to "layers" in order to display them
Map.centerObject(roi, 7);
Map.addLayer(SARVV, {min:-15,max:0}, 'SAR VV', 0);
Map.addLayer(SARVH, {min:-25,max:0}, 'SAR VH', 0);
// Function to cloud mask from the pixel QA band of Landsat 8 SR data.
function maskL8sr(image) {
// Bits 3 and 5 are cloud shadows and clouds, respectively.
var cloudShadowBitMask = 1 << 3;
var cloudsBitMask = 1 << 5;
// Get the pixel QA band.
var qa = image.select('pixel_qa');
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
.and(qa.bitwiseAnd(cloudsBitMask).eq(0));
// Return the masked image, scaled to reflectance, without the QA bands.
return image.updateMask(mask).divide(10000)
.select("B[0-9]*")
.copyProperties(image, ["system:time_start"]);
}

In [ ]:

// Extract the images from the Landsat8 collection
var collectionl8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterDate('2019-12-01', '2020-01-30')
.filterBounds(roi)
.map(maskL8sr);

In [ ]:

//Calculate NDVI and create an image that contains all Landsat 8 bands and NDVI
var comp = collectionl8.mean();
var ndvi = comp.normalizedDifference(['B5', 'B4']).rename('NDVI');
var composite = ee.Image.cat(comp,ndvi);

In [ ]:

// Add images to layers in order to display them
Map.centerObject(roi, 7);
Map.addLayer(composite, {bands: ['B4', 'B3', 'B2'], min: 0, max: 0.2}, 'Optical');

In [ ]:

//Apply filter to reduce speckle
var SMOOTHING_RADIUS = 50;
var SARVV_filtered = SARVV.focal_mean(SMOOTHING_RADIUS, 'circle', 'meters');
var SARVH_filtered = SARVH.focal_mean(SMOOTHING_RADIUS, 'circle', 'meters');

In [ ]:

//Display the SAR filtered images
Map.addLayer(SARVV_filtered, {min:-15,max:0}, 'SAR VV Filtered',0);
Map.addLayer(SARVH_filtered, {min:-25,max:0}, 'SAR VH Filtered',0);

In [ ]:

//Merge Feature Collections
var newfc =
open_water.merge(bare_fields).merge(vegetation1).merge(vegetation2)
.merge(vegetation3).merge(settlement).merge(forest);

In [ ]:

//Define the SAR bands to train your data
var final = ee.Image.cat(SARVV_filtered,SARVH_filtered);
var bands = ['VH','VV'];
var training = final.select(bands).sampleRegions({
collection: newfc,
properties: ['landcover'],
scale: 30 });

In [ ]:

//Train the classifier
var classifier = ee.Classifier.randomForest().train({
features: training,
classProperty: 'landcover',
inputProperties: bands
});

In [ ]:

//Run the Classifier
var classified = final.select(bands).classify(classifier);

In [ ]:

//Display the Classification
Map.addLayer(classified,
{min: 1, max: 7, palette: ['1667fa', 'c9270d', 'cf7b68', 'ee9a1c', '146d0e', '04bd23',
]},
'SAR Classification');

In [ ]:

/ Create a confusion matrix representing resubstitution accuracy.
print('RF-SAR error matrix: ', classifier.confusionMatrix());
print('RF-SAR accuracy: ', classifier.confusionMatrix().accuracy());

In [ ]:

//Define the Landsat bands to train your data
var bandsl8 = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11', 'NDVI' ];
//var bandsl8 = ['NDVI' ];
var trainingl8 = composite.select(bandsl8).sampleRegions({
collection: newfc,
properties: ['landcover'],
scale: 30
});

In [ ]:

//Train the classifier
var classifierl8 = ee.Classifier.randomForest().train({
features: trainingl8,
classProperty: 'landcover',
inputProperties: bandsl8
});
//Run the Classifier
var classifiedl8 = composite.select(bandsl8).classify(classifierl8);

In [ ]:

//Display the Classification
Map.addLayer(classifiedl8,
{min: 1, max: 7, palette: ['1667fa', 'c9270d', 'cf7b68', 'ee9a1c', '146d0e', '04bd23', ]},
'Optical Classification');

In [ ]:

// Create a confusion matrix representing resubstitution accuracy.
print('RF-L8 error matrix: ', classifierl8.confusionMatrix());
print('RF-L8 accuracy: ', classifierl8.confusionMatrix().accuracy());

In [ ]:

//Define both optical and SAR to train your data
var opt_sar = ee.Image.cat(composite, SARVV_filtered,SARVH_filtered);
var bands_opt_sar = ['VH','VV','B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11', 'NDVI'];
var training_opt_sar = opt_sar.select(bands_opt_sar).sampleRegions({
collection: newfc,
properties: ['landcover'],
scale: 30 });

In [ ]:

//Train the classifier
var classifier_opt_sar = ee.Classifier.randomForest().train({
features: training_opt_sar,
classProperty: 'landcover',
inputProperties: bands_opt_sar
});

In [ ]:

//Run the classifier
var classifiedboth = opt_sar.select(bands_opt_sar).classify(classifier_opt_sar);

In [ ]:

//Display the Classification
Map.addLayer(classifiedboth,
{min: 1, max: 7, palette: ['1667fa', 'c9270d', 'cf7b68', 'ee9a1c', '146d0e', '04bd23',]},
'Optical/SAR Classification');

In [ ]:

// Create a confusion matrix representing resubstitution accuracy.
print('RF-Opt/SAR error matrix: ', classifier_opt_sar.confusionMatrix());
print('RF-Opt/SAR accuracy: ', classifier_opt_sar.confusionMatrix().accuracy());

In [ ]:

// Export the image, specifying scale and region.
Export.image.toDrive({
image: classifiedboth,
description: 'Optical_Radar',
scale: 100,
fileFormat: 'GeoTIFF',
});

In [ ]:

var gaul = ee.FeatureCollection(
'FAO/GAUL_SIMPLIFIED_500m/2015/level2');
var uttarpradesh = gaul.filter(ee.Filter.eq('ADM1_NAME', 'Uttar Pradesh'))
// Show the state polygon with a blue outline
var outline = ee.Image().byte().paint({
featureCollection: uttarpradesh,
color: 1,
width: 3
});

In [ ]:

Map.addLayer(outline, {palette: ['blue']}, 'AOI')

var palette = ['05450a', '086a10', '54a708', '78d203', '009900', 'c6b044',
'dcd159',]

In [ ]:

// Clip the classified image to the state boundary
var uttarpradeshLandcover = classified.clip(uttarpradesh)
Map.addLayer(uttarpradeshLandcover,{min:1, max:7, palette: palette},'Uttar pradesh')

In [ ]:

var areaImage = ee.Image.pixelArea().addBands(uttarpradeshLandcover)

In [ ]:

var areas = areaImage.reduceRegion({
reducer: ee.Reducer.sum().group({
groupField: 1,
groupName: 'class',
}),
geometry: uttarpradesh.geometry(),
scale: 500,
maxPixels: 1e10
});
print(areas)

In [ ]:

var classAreas = ee.List(areas.get('groups'))
var classAreaLists = classAreas.map(function(item) {
var areaDict = ee.Dictionary(item)
var classNumber = ee.Number(areaDict.get('class')).format()
var area = ee.Number(areaDict.get('sum')).divide(1e6).round()
return ee.List([classNumber, area])
})

In [ ]:

print(classAreaLists)

var nestedList = ee.List([['a', 'b'], ['c', 'd'], ['e', 'f']])
print(nestedList)
print(nestedList.flatten())

var result = ee.Dictionary(classAreaLists.flatten())
print(result)

In [ ]:

var calculateClassArea = function(feature) {
var areas = ee.Image.pixelArea().addBands(classified)
.reduceRegion({
reducer: ee.Reducer.sum().group({
groupField: 1,
groupName: 'class',
}),
geometry: feature.geometry(),
scale: 500,
maxPixels: 1e10
})

In [ ]:

var classAreas = ee.List(areas.get('groups'))
var classAreaLists = classAreas.map(function(item) {
var areaDict = ee.Dictionary(item)
var classNumber = ee.Number(
areaDict.get('class')).format()
var area = ee.Number(
areaDict.get('sum')).divide(1e6).round()
return ee.List([classNumber, area])
})

In [ ]:

var result = ee.Dictionary(classAreaLists.flatten())
var district = feature.get('ADM2_NAME')
return ee.Feature(
feature.geometry(),
result.set('district', district))
}

In [ ]:

var districtAreas = uttarpradesh.map(calculateClassArea);

var classes = ee.List.sequence(1, 7)
var outputFields = ee.List(['district']).cat(classes).getInfo()
Export.table.toDrive({
collection: districtAreas,
description: 'class_area_by_district',
folder: 'earthengine',
fileNamePrefix: 'class_area_by_district',
fileFormat: 'CSV',
selectors: outputFields
})

In [ ]:

// Export the image, specifying scale and region.
Export.image.toDrive({
image: classified,
description: 'Radar',
scale: 100,
region:uttarpradesh.geometry(),
fileFormat: 'GeoTIFF',
});

In [ ]:

// Export the image, specifying scale and region.
Export.image.toDrive({
image: classified,
description: 'Optical_Radar',
scale: 100,
region:uttarpradesh.geometry(),
fileFormat: 'GeoTIFF',
});

--

--