Geographic Information Systems Asked on August 12, 2021
I am using Google Earth Engine to collect a training dateset for supervised classification, and the code is shown as follow:
// 0. Global Variables
var site = ee.Geometry.Polygon([[-80.79, 24.98], [-80.08, 26.43], [-80.71, 26.16],[-81.07, 26.20]], null, false);
var globOptions = {
versionID: '_SR',
outFolder: 'SR',
startDate: '2014-01-01',
endDate: '2018-12-31',
bandSelect: ['blue', 'green', 'red', 'nir', 'swir1', 'swir2'],
bands8: ['B2', 'B3', 'B4', 'B5', 'B6', 'B7'],
bands7: ['B1', 'B2', 'B3', 'B4', 'B5', 'B7'],
maskAltitude: 100,
maskDepth: -100,
maskDistance: 50000,
maskApplySRTM: false,
parallelScale: 8,
trainingValidationRatio: 0.0001,
nTrees: 10,
outScale: 30,
conPixels: 100
};
// 1. Functions
var landsatFunctions = {
//maskClouds: function(image) {
//var cs = cloud_shadows(image);
//var c = clouds(image);
//image = image.updateMask(cs);
//return image.updateMask(c);
//},
applyNDWI: function(image) {
// apply NDWI to an image
var ndwi = image.normalizedDifference(['green','nir']);
return ndwi.select([0], ['ndwi']);
},
applyMNDWI: function(image) {
// apply MNDWI to an image
var mndwi = image.normalizedDifference(['green','swir1']);
return image.select([0], ['mndwi']);
},
applyAWEI: function(image) {
// apply AWEI to an image
var awei = image.expression("b('blue')+2.5*b('green')-1.5*(b('nir')+b('swir1'))-0.25*b('swir2')");
return awei.select([0], ['awei']);
},
applyNDVI: function(image) {
// apply NDVI to an image
var ndvi = image.normalizedDifference(['nir','red']);
return ndvi.select([0], ['ndvi']);
},
};
var getQABits = function(image, start, end, newName) {
// Compute the bits we need to extract.
var pattern = 0;
for (var i = start; i <= end; i++) {
pattern += Math.pow(2, i);
}
// Return a single band image of the extracted QA bits, giving the band
// a new name.
return image.select([0], [newName])
.bitwiseAnd(pattern)
.rightShift(start);
};
// A function to mask out cloudy shadow pixels.
var cloud_shadows = function(image) {
// Select the QA band.
var QA = image.select(['pixel_qa']);
// Get the internal_cloud_algorithm_flag bit.
return getQABits(QA, 3,3, 'Cloud_shadows').eq(0);
// Return an image masking out cloudy areas.
};
// A function to mask out cloud pixels.
var clouds = function(image) {
// Select the QA band.
var QA = image.select(['pixel_qa']);
// Get the internal_cloud_algorithm_flag bit.
return getQABits(QA, 5,5, 'Cloud').eq(0);
// Return an image masking out cloudy areas.
};
var maskClouds = function(image) {
var cs = cloud_shadows(image);
var c = clouds(image);
image = image.updateMask(cs);
return image.updateMask(c);
};
// images
var L4collection = ee.ImageCollection('LANDSAT/LT04/C01/T1_SR')
.filterDate(globOptions.startDate, globOptions.endDate)
.map(maskClouds)
.select(globOptions.bands7, globOptions.bandSelect);
var L5collection = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
.filterDate(globOptions.startDate, globOptions.endDate)
.map(maskClouds)
.select(globOptions.bands7, globOptions.bandSelect);
var L7collection = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
.filterDate(globOptions.startDate,globOptions.endDate)
.map(maskClouds)
.select(globOptions.bands7, globOptions.bandSelect);
var L8collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterDate(globOptions.startDate, globOptions.endDate)
.map(maskClouds)
.select(globOptions.bands8, globOptions.bandSelect);
var collectionFull = ee.ImageCollection(L4collection
.merge(L5collection)
.merge(L7collection)
.merge(L8collection))
//.filterBounds(site)
//.filter(ee.Filter.intersects('.geo', globCoast.geometry(), null, null, 1000))
//.filterMetadata('WRS_ROW', 'less_than', 120)
;
var non_mosaic = collectionFull .filterBounds(site);
//Will be used for frequency calculaton
var clouds_free = non_mosaic.map(maskClouds);
//display the mosaicked origional image and cloud free image
var mosaic_free = non_mosaic.map(maskClouds).median();
var visParams = {bands: ['red','green','blue'],min: [0,0,0],max: [2000, 2000, 2000]};
Map.addLayer(non_mosaic, visParams, 'Merged Landsat Collection', false);
//Map.addLayer(mosaic_free, visParams, 'Cloud free');
// Data processing
var covariates = {
awei_min: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.min()
.setOutputs(['min'])),
awei_max: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.max()
.setOutputs(['max'])),
awei_stdDev: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.stdDev()
.setOutputs(['stdDev'])),
awei_median: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.median()
.setOutputs(['median'])),
awei_p10: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.percentile([10])
.setOutputs(['p10'])),
awei_p25: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.percentile([25])
.setOutputs(['p25'])),
awei_p50: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.percentile([50])
.setOutputs(['p50'])),
awei_p75: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.percentile([75])
.setOutputs(['p75'])),
awei_p90: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.percentile([90])
.setOutputs(['p90'])),
awei_intMn0010: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.intervalMean(0, 10)
.setOutputs(['intMn0010'])),
awei_intMn1025: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.intervalMean(10, 25)
.setOutputs(['intMn1025'])),
awei_intMn2550: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.intervalMean(25, 50)
.setOutputs(['intMn2550'])),
awei_intMn5075: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.intervalMean(50, 75)
.setOutputs(['intMn5075'])),
awei_intMn7590: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.intervalMean(75, 90)
.setOutputs(['intMn7590'])),
awei_intMn90100: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.intervalMean(90, 100)
.setOutputs(['intMn90100'])),
awei_intMn1090: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.intervalMean(10, 90)
.setOutputs(['intMn1090'])),
awei_intMn2575: non_mosaic.map(landsatFunctions.applyAWEI)
.reduce(ee.Reducer.intervalMean(25, 75)
.setOutputs(['intMn2575'])),
ndwi_min: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.min()
.setOutputs(['min'])),
ndwi_max: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.max()
.setOutputs(['max'])),
ndwi_stdDev: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.stdDev()
.setOutputs(['stdDev'])),
ndwi_median: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.median()
.setOutputs(['median'])),
ndwi_p10: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.percentile([10])
.setOutputs(['p10'])),
ndwi_p25: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.percentile([25])
.setOutputs(['p25'])),
ndwi_p50: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.percentile([50])
.setOutputs(['p50'])),
ndwi_p75: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.percentile([75])
.setOutputs(['p75'])),
ndwi_p90: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.percentile([90])
.setOutputs(['p90'])),
ndwi_intMn0010: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.intervalMean(0, 10)
.setOutputs(['intMn0010'])),
ndwi_intMn1025: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.intervalMean(10, 25)
.setOutputs(['intMn1025'])),
ndwi_intMn2550: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.intervalMean(25, 50)
.setOutputs(['intMn2550'])),
ndwi_intMn5075: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.intervalMean(50, 75)
.setOutputs(['intMn5075'])),
ndwi_intMn7590: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.intervalMean(75, 90)
.setOutputs(['intMn7590'])),
ndwi_intMn90100: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.intervalMean(90, 100)
.setOutputs(['intMn90100'])),
ndwi_intMn1090: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.intervalMean(10, 90)
.setOutputs(['intMn1090'])),
ndwi_intMn2575: non_mosaic.map(landsatFunctions.applyNDWI)
.reduce(ee.Reducer.intervalMean(25, 75)
.setOutputs(['intMn2575'])),
mndwi_min: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.min()
.setOutputs(['min'])),
mndwi_max: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.max()
.setOutputs(['max'])),
mndwi_stdDev: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.stdDev()
.setOutputs(['stdDev'])),
mndwi_median: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.median()
.setOutputs(['median'])),
mndwi_p10: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.percentile([10])
.setOutputs(['p10'])),
mndwi_p25: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.percentile([25])
.setOutputs(['p25'])),
mndwi_p50: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.percentile([50])
.setOutputs(['p50'])),
mndwi_p75: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.percentile([75])
.setOutputs(['p75'])),
mndwi_p90: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.percentile([90])
.setOutputs(['p90'])),
mndwi_intMn0010: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.intervalMean(0, 10)
.setOutputs(['intMn0010'])),
mndwi_intMn1025: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.intervalMean(10, 25)
.setOutputs(['intMn1025'])),
mndwi_intMn2550: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.intervalMean(25, 50)
.setOutputs(['intMn2550'])),
mndwi_intMn5075: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.intervalMean(50, 75)
.setOutputs(['intMn5075'])),
mndwi_intMn7590: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.intervalMean(75, 90)
.setOutputs(['intMn7590'])),
mndwi_intMn90100: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.intervalMean(90, 100)
.setOutputs(['intMn90100'])),
mndwi_intMn1090: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.intervalMean(10, 90)
.setOutputs(['intMn1090'])),
mndwi_intMn2575: non_mosaic.map(landsatFunctions.applyMNDWI)
.reduce(ee.Reducer.intervalMean(25, 75)
.setOutputs(['intMn2575'])),
ndvi: non_mosaic.map(landsatFunctions.applyNDVI)
.reduce(ee.Reducer.intervalMean(10, 90)
.setOutputs(['intMn1090'])),
nirBand: non_mosaic.select(['nir'])
.reduce(ee.Reducer.intervalMean(10, 90)
.setOutputs(['intMn1090'])),
swir1Band: non_mosaic.select(['swir1'])
.reduce(ee.Reducer.intervalMean(10, 90)
.setOutputs(['intMn1090'])),
etopo: ee.Image('NOAA/NGDC/ETOPO1')
.select(['bedrock'], ['etopo'])
.resample('bicubic'),
swOccurrence: ee.Image('JRC/GSW1_0/GlobalSurfaceWater')
.select(['occurrence'], ['surfaceWater'])
.unmask()
};
var roi = /* color: #0b4a8b */ee.Geometry.Point([-81.19522282910155, 25.61952670589741]);
// Extract the data
var roi_awei_min = covariates.awei_min
.select("awei_min")
.reduceRegion(ee.Reducer.first(),roi,10)
.get("awei_min")
// Convert to Number for further use
var awei_min = ee.Number(roi_awei_min)
// Show data
print('awei_min: ', awei_min)
var roi_awei_max = covariates.awei_max
.select("awei_max")
.reduceRegion(ee.Reducer.first(),roi,10)
.get("awei_max")
var awei_max = ee.Number(roi_awei_max)
print('awei_max: ', awei_max)
var roi_awei_stdDev = covariates.awei_stdDev
.select("awei_stdDev")
.reduceRegion(ee.Reducer.first(),roi,10)
.get("awei_stdDev")
var awei_stdDev = ee.Number(roi_awei_stdDev)
print('awei_stdDev: ', awei_stdDev)
In this code, I have specified some types of values (i.e. awei_min
, awei_max
, etc.) and the way how to calculate them. I have also specified a point (aka var roi
). If you run this code, you will see the results of awei_min
, awei_max
, and awei_stdDev
in the console tab. They are the values of awei_min
, awei_max
, and awei_stdDev
at the point of roi.
However, I have no idea how to export these values as a csv table.
Also, this code is only okay for the single points. That is, if I want to collect a training datasets for 50 points, I need to edit the roi and repeatedly run the code the for 50 times. If I specify multiple (equal or greater than 2) points for roi, the code will not work.
What I want is, export a single csv table, whose columns are the different attribute values (awei_min
, awei_max
, etc.) and whose rows are different points.
For me, it’s bearable to repeatedly run the code and get the table for every single point (but still preferred to collect the data for multiple points at the same time). The emergent issue is, how to export the multiple values of a single point as a single rowed table?
I think your best bet here is to use ee.Image.reduceRegions
var roi = ee.FeatureCollection('....')
// Extract the data
var roi_awei_min = covariates.awei_min.select("awei_min")
.reduceRegions({collection: roi,
reducer: ee.Reducer.first(),
scale: 10})
// To answer the first question, export
Export.table.toDrive({collection: roi_awei_min,
folder: 'SOMEFOLDER',
fileNamePrefix:'roi_awei_min',
fileFormat: 'CSV'})
Answered by Rodrigo E. Principe on August 12, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP