4

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.

code result preview

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, enter image description here

Here with maxPixels: 10E11,

enter image description here

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.

enter image description here

  • Have you tried to keep image: NDVI.clip(geometry)? I took it off because I thought you didn't need to do it, but try to keep it and see if it's what you are looking for – Rodrigo E. Principe Oct 25 '18 at 16:19
3

When you apply the clip method to an Image, all you are doing is masking out pixels outside that geometry, but your NDVI image is unbounded. If you want to export only one region, you have to use the region parameter in the exporting function. An in this case it's easier to pick it up directly from your FeatureCollection

// Export image
Export.image.toDrive({
  image: NDVI,
  region: geometry.geometry().getInfo(),
  description: 'NDVI_S2_Aug2018_2',
  scale: 10,
  maxPixels: 10E11,
  fileFormat: 'GeoTIFF',
});
  • Thanks for your help, Rodrigo. With your line region: geometry.geometry().getInfo()['coordinates'], I get Invalid format for region property. Region must be GeoJSON LinearRing or Polygon specified as actual coordinates or serialized as a string. – Michele Bolognesi Oct 25 '18 at 9:48
  • Your are right @MicheleBolognesi, I have edited the answer – Rodrigo E. Principe Oct 25 '18 at 11:06
1

Also, make sure you are zoomed out from the image area. If you are zoomed in past the edges of the polygon you are trying to clip to, only the contents of your map window will be exported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.