I am new to GEE and javascript. Copy-pasting here and there, I managed to put together this code that extracts NDVI from cloud-free Sentinel-2 images on GEE. I created a fusion table from a KML with the polygon I need as bounding box. When I run the code, the result preview on GEE is exactly what I want, with boundaries matching the FeatureCollection geometry from the fusion table.
but when I export, the image does not match // Display NDVI results on map
.
See screenshot: exported results cover an area that is not matching my geometry var
Export.image.toDrive({
image: NDVI.clip(geometry),
Here I exported with maxPixels: 10E10,
Here with maxPixels: 10E11,
I have a 1TB GoogleDrive subscription, so space should not be the issue.
What am I doing wrong?
Here is the code:
//Import GEE Feature Collection (Somaliland kml)
var geometry = ee.FeatureCollection('ft:14f9y--LmmeDiBOkMFWA2eef5KvjrPRFDH68IRPfQ');
// Create image collection of S-2 imagery for the perdiod 2016-2018
var S2 = ee.ImageCollection('COPERNICUS/S2')
//filter start and end date
.filterDate('2018-08-01', '2018-08-31')
//filter according to drawn boundary
.filterBounds(geometry);
// Function to mask cloud from built-in quality band
// information on cloud
var maskcloud1 = function(image) {
var QA60 = image.select(['QA60']);
return image.updateMask(QA60.lt(1));
};
// Function to calculate and add an NDVI band
var addNDVI = function(image) {
return image.addBands(image.normalizedDifference(['B8', 'B4']));
};
// Add NDVI band to image collection
var S2 = S2.map(addNDVI);
// Extract NDVI band and create NDVI median composite image
var NDVI = S2.select(['nd']);
var NDVI = NDVI.median();
// Create palettes for display of NDVI
var ndvi_pal = ['#d73027', '#f46d43', '#fdae61', '#fee08b', '#d9ef8b',
'#a6d96a'];
// Display NDVI results on map
Map.addLayer(NDVI.clip(geometry), {min:-0.5, max:0.9, palette: ndvi_pal}, 'NDVI');
// Export image
Export.image.toDrive({
image: NDVI.clip(geometry),
description: 'NDVI_S2_Aug2018_2',
scale: 10,
maxPixels: 10E11,
fileFormat: 'GeoTIFF',
});
Based on @Rodrigo E. Principe suggestion, I have changed the code to:
// Export image
Export.image.toDrive({
image: NDVI,
region: geometry.geometry().getInfo(),
The result is good, as it respect the minimum bounding geometry enclosing my input feature (as per fusiontable), but it is not clipped to the polygon boundaries. I guess I will have to clip the result with a GIS software outside GEE.