Skip to content

Instantly share code, notes, and snippets.

@jdbcode
Created August 14, 2023 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdbcode/3779c09f4a77b70738532660b44fd21f to your computer and use it in GitHub Desktop.
Save jdbcode/3779c09f4a77b70738532660b44fd21f to your computer and use it in GitHub Desktop.
Estimate image chunk size

(Updated: 2023-08-14)

A method to estimate Earth Engine interactice server-to-client image data request size.

Code Editor script

var geometry = ee.Geometry.Polygon(
        [[[-123.61064453125, 45.977647428626796],
          [-123.61064453125, 45.47145566980041],
          [-120.940966796875, 45.47145566980041],
          [-120.940966796875, 45.977647428626796]]], null, false);

// Estimate the download size, assumes all bands are same type.
function getSize(img, region, scale, crs, dataType) {
  var bytes = {
    '8bit': 1 + 1,
    '16bit': 2 + 1,
    '32bit': 4 + 1,
    '64bit': 8 + 1
  };
  var bounds = region.bounds(0.01, crs);
  var nPixels = img.select(0).unmask(0).clip(bounds)
    .reduceRegion({
      reducer: ee.Reducer.count(),
      geometry: bounds,
      scale: scale,
      crs: crs,
      maxPixels: 1e13
    }).values().getNumber(0);
  var nBands = img.bandNames().size();
  var size = nPixels.multiply(nBands).multiply(bytes[dataType]);
  return size;
}


// Mock image.
var img = ee.Image([1]).toByte();

// Export settings.
var scale = 30;
var crs = 'EPSG:4326';
var dataType = '8bit';

// Estimate the size of the download.
var sizeTest = getSize(img, geometry, scale, crs, dataType);
print('Estimated size (bytes)', sizeTest);
print('Estimated size (mb)', sizeTest.divide(1e6));

// Get the Download URL.
var url = img.getDownloadURL({
  region: geometry,
  scale: scale,
  crs: crs,
  format: 'GEO_TIFF'
});

print('Download URL', url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment