Skip to content

Instantly share code, notes, and snippets.

@jdbcode
Created May 25, 2020 23:02
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/0695390dbc9fcdb03b4da93aba24c7f4 to your computer and use it in GitHub Desktop.
Save jdbcode/0695390dbc9fcdb03b4da93aba24c7f4 to your computer and use it in GitHub Desktop.
Calculate zonal statistics by longitude bin and chart the result in Earth Engine
// Load VIIRS NDVI - to be summarized by longitude bins.
var viirsNdvi = ee.ImageCollection("NOAA/VIIRS/001/VNP13A1")
.select('NDVI').first();
// Load lat/lon image - select the longitude band.
var latLon = ee.Image.pixelLonLat().select('longitude');
// Make a list of longitude bin start values.
var step = 5; // longitude step
var lonStarts = ee.List.sequence(-90, 90-step, step);
// Map of the longitude list - get the mean NDVI value for pixels in given
// longitude bin. Returns a list of features.
var lonStatList = lonStarts.map(function(lon) {
var startLon = ee.Number(lon);
var endLon = startLon.add(step);
var targetPixels = latLon.gte(startLon).and(latLon.lt(endLon));
var stat = viirsNdvi.updateMask(targetPixels).reduceRegion({
reducer: ee.Reducer.mean(),
geometry: ee.Geometry.Rectangle(-179.99, -89.99, 179.99, 89.99),
scale: 10000,
bestEffort: true});
return ee.Feature(viirsNdvi.geometry(), stat).set('lon', lon);
});
// Convert feature list to feature collection.
var lonStatsCol = ee.FeatureCollection(ee.List(lonStatList));
// Chart the results.
var chart =
ui.Chart.feature.byFeature(lonStatsCol, 'lon', 'NDVI')
.setOptions({
title: 'Average NDVI by Longitude',
hAxis: {title: 'Longitude'},
vAxis: {title: 'NDVI (x1e4)'}
});
print(chart);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment