Calculating NDVI per region, month & year with Google Earth Engine?
I want to calculate the mean NDVI per region (admin level 3, also called woreda), month and year. So my end result would look something like this:
regions year month NDVI
---------------------------------
region_1 2010 1 0.5
region_1 2010 2 -0.6
region_1 2010 3 0.7
region_1 2010 4 -0.3
region_1 2010 5 0.4
region_1 2010 6 -0.5
region_1 2010 7 0.5
region_1 2010 8 -0.7
region_1 2010 9 0.8
region_1 2010 10 -0.55
region_1 2010 11 -0.3
region_1 2010 12 -0.2
region_2 2010 1 0.5
region_2 2010 2 -0.6
region_2 2010 3 0.7
region_2 2010 4 -0.3
region_2 2010 5 0.4
region_2 2010 6 -0.5
region_2 2010 7 0.5
region_2 2010 8 -0.7
region_2 2010 9 0.8
region_2 2010 10 -0.55
region_2 2010 11 -0.3
region_2 2010 12 -0.2
... ... ... ...
My code basically does this for a predetermined region in the var modisNDVI. However I want my code to be able to do this for 2010 untill 2015, for each month for each region.
How can I do this without writing more for loops (the iterating through the years and months)?
Should I be using reduceRegion or .map() in order to skip (all) the for loops?
I've made an attempt to use reduceRegions but failed to apply this to an imageCollection.
// import data
var region = ee.FeatureCollection("ft:1zRUOJL1LYCPJj-mjP6ZRx8sxYKNH8EwDw3EPP66K"),
modisNDVI = ee.ImageCollection("MODIS/MCD43A4_006_NDVI");
// Get NDVI
var modisNDVI = ee.ImageCollection(modisNDVI.filterDate('2015-01-01', '2015-06-01'));
var woredaNames = region.aggregate_array("HRpcode")
// do something so I can get monthly data for each year (2010-2015) for earch woreda (690)
// I don't want to write another for loop for the year and month what is a more optimized way?
// Processing all the 690 takes long, for this example I've used 10 woreda's
for (var woreda=0; woreda < 10 ;woreda++)
// Focus on one region:
var focusRegion = region.filter(ee.Filter.eq('system:index', String(woreda)));
// Clip modis image on focused region:
var focus_NDVI_clip = modisNDVI.mean().clip(focusRegion);
// aggregate mean over geometry from focused region:
var mean_dict = focus_NDVI_clip.reduceRegion(
reducer: ee.Reducer.mean(),
geometry: focusRegion.geometry(),
scale: 500,
);
// Append index to mean_dictionary and print it (eventually this should turn into a list):
var woreda_code = ee.List(woredaNames).get(woreda);
mean_dict = mean_dict.set('Woreda_code', ee.String(woreda_code));
print(mean_dict);
javascript for-loop optimization google-earth-engine
add a comment |
I want to calculate the mean NDVI per region (admin level 3, also called woreda), month and year. So my end result would look something like this:
regions year month NDVI
---------------------------------
region_1 2010 1 0.5
region_1 2010 2 -0.6
region_1 2010 3 0.7
region_1 2010 4 -0.3
region_1 2010 5 0.4
region_1 2010 6 -0.5
region_1 2010 7 0.5
region_1 2010 8 -0.7
region_1 2010 9 0.8
region_1 2010 10 -0.55
region_1 2010 11 -0.3
region_1 2010 12 -0.2
region_2 2010 1 0.5
region_2 2010 2 -0.6
region_2 2010 3 0.7
region_2 2010 4 -0.3
region_2 2010 5 0.4
region_2 2010 6 -0.5
region_2 2010 7 0.5
region_2 2010 8 -0.7
region_2 2010 9 0.8
region_2 2010 10 -0.55
region_2 2010 11 -0.3
region_2 2010 12 -0.2
... ... ... ...
My code basically does this for a predetermined region in the var modisNDVI. However I want my code to be able to do this for 2010 untill 2015, for each month for each region.
How can I do this without writing more for loops (the iterating through the years and months)?
Should I be using reduceRegion or .map() in order to skip (all) the for loops?
I've made an attempt to use reduceRegions but failed to apply this to an imageCollection.
// import data
var region = ee.FeatureCollection("ft:1zRUOJL1LYCPJj-mjP6ZRx8sxYKNH8EwDw3EPP66K"),
modisNDVI = ee.ImageCollection("MODIS/MCD43A4_006_NDVI");
// Get NDVI
var modisNDVI = ee.ImageCollection(modisNDVI.filterDate('2015-01-01', '2015-06-01'));
var woredaNames = region.aggregate_array("HRpcode")
// do something so I can get monthly data for each year (2010-2015) for earch woreda (690)
// I don't want to write another for loop for the year and month what is a more optimized way?
// Processing all the 690 takes long, for this example I've used 10 woreda's
for (var woreda=0; woreda < 10 ;woreda++)
// Focus on one region:
var focusRegion = region.filter(ee.Filter.eq('system:index', String(woreda)));
// Clip modis image on focused region:
var focus_NDVI_clip = modisNDVI.mean().clip(focusRegion);
// aggregate mean over geometry from focused region:
var mean_dict = focus_NDVI_clip.reduceRegion(
reducer: ee.Reducer.mean(),
geometry: focusRegion.geometry(),
scale: 500,
);
// Append index to mean_dictionary and print it (eventually this should turn into a list):
var woreda_code = ee.List(woredaNames).get(woreda);
mean_dict = mean_dict.set('Woreda_code', ee.String(woreda_code));
print(mean_dict);
javascript for-loop optimization google-earth-engine
Cross-posted as gis.stackexchange.com/q/301447/115
– PolyGeo
Dec 12 '18 at 7:38
add a comment |
I want to calculate the mean NDVI per region (admin level 3, also called woreda), month and year. So my end result would look something like this:
regions year month NDVI
---------------------------------
region_1 2010 1 0.5
region_1 2010 2 -0.6
region_1 2010 3 0.7
region_1 2010 4 -0.3
region_1 2010 5 0.4
region_1 2010 6 -0.5
region_1 2010 7 0.5
region_1 2010 8 -0.7
region_1 2010 9 0.8
region_1 2010 10 -0.55
region_1 2010 11 -0.3
region_1 2010 12 -0.2
region_2 2010 1 0.5
region_2 2010 2 -0.6
region_2 2010 3 0.7
region_2 2010 4 -0.3
region_2 2010 5 0.4
region_2 2010 6 -0.5
region_2 2010 7 0.5
region_2 2010 8 -0.7
region_2 2010 9 0.8
region_2 2010 10 -0.55
region_2 2010 11 -0.3
region_2 2010 12 -0.2
... ... ... ...
My code basically does this for a predetermined region in the var modisNDVI. However I want my code to be able to do this for 2010 untill 2015, for each month for each region.
How can I do this without writing more for loops (the iterating through the years and months)?
Should I be using reduceRegion or .map() in order to skip (all) the for loops?
I've made an attempt to use reduceRegions but failed to apply this to an imageCollection.
// import data
var region = ee.FeatureCollection("ft:1zRUOJL1LYCPJj-mjP6ZRx8sxYKNH8EwDw3EPP66K"),
modisNDVI = ee.ImageCollection("MODIS/MCD43A4_006_NDVI");
// Get NDVI
var modisNDVI = ee.ImageCollection(modisNDVI.filterDate('2015-01-01', '2015-06-01'));
var woredaNames = region.aggregate_array("HRpcode")
// do something so I can get monthly data for each year (2010-2015) for earch woreda (690)
// I don't want to write another for loop for the year and month what is a more optimized way?
// Processing all the 690 takes long, for this example I've used 10 woreda's
for (var woreda=0; woreda < 10 ;woreda++)
// Focus on one region:
var focusRegion = region.filter(ee.Filter.eq('system:index', String(woreda)));
// Clip modis image on focused region:
var focus_NDVI_clip = modisNDVI.mean().clip(focusRegion);
// aggregate mean over geometry from focused region:
var mean_dict = focus_NDVI_clip.reduceRegion(
reducer: ee.Reducer.mean(),
geometry: focusRegion.geometry(),
scale: 500,
);
// Append index to mean_dictionary and print it (eventually this should turn into a list):
var woreda_code = ee.List(woredaNames).get(woreda);
mean_dict = mean_dict.set('Woreda_code', ee.String(woreda_code));
print(mean_dict);
javascript for-loop optimization google-earth-engine
I want to calculate the mean NDVI per region (admin level 3, also called woreda), month and year. So my end result would look something like this:
regions year month NDVI
---------------------------------
region_1 2010 1 0.5
region_1 2010 2 -0.6
region_1 2010 3 0.7
region_1 2010 4 -0.3
region_1 2010 5 0.4
region_1 2010 6 -0.5
region_1 2010 7 0.5
region_1 2010 8 -0.7
region_1 2010 9 0.8
region_1 2010 10 -0.55
region_1 2010 11 -0.3
region_1 2010 12 -0.2
region_2 2010 1 0.5
region_2 2010 2 -0.6
region_2 2010 3 0.7
region_2 2010 4 -0.3
region_2 2010 5 0.4
region_2 2010 6 -0.5
region_2 2010 7 0.5
region_2 2010 8 -0.7
region_2 2010 9 0.8
region_2 2010 10 -0.55
region_2 2010 11 -0.3
region_2 2010 12 -0.2
... ... ... ...
My code basically does this for a predetermined region in the var modisNDVI. However I want my code to be able to do this for 2010 untill 2015, for each month for each region.
How can I do this without writing more for loops (the iterating through the years and months)?
Should I be using reduceRegion or .map() in order to skip (all) the for loops?
I've made an attempt to use reduceRegions but failed to apply this to an imageCollection.
// import data
var region = ee.FeatureCollection("ft:1zRUOJL1LYCPJj-mjP6ZRx8sxYKNH8EwDw3EPP66K"),
modisNDVI = ee.ImageCollection("MODIS/MCD43A4_006_NDVI");
// Get NDVI
var modisNDVI = ee.ImageCollection(modisNDVI.filterDate('2015-01-01', '2015-06-01'));
var woredaNames = region.aggregate_array("HRpcode")
// do something so I can get monthly data for each year (2010-2015) for earch woreda (690)
// I don't want to write another for loop for the year and month what is a more optimized way?
// Processing all the 690 takes long, for this example I've used 10 woreda's
for (var woreda=0; woreda < 10 ;woreda++)
// Focus on one region:
var focusRegion = region.filter(ee.Filter.eq('system:index', String(woreda)));
// Clip modis image on focused region:
var focus_NDVI_clip = modisNDVI.mean().clip(focusRegion);
// aggregate mean over geometry from focused region:
var mean_dict = focus_NDVI_clip.reduceRegion(
reducer: ee.Reducer.mean(),
geometry: focusRegion.geometry(),
scale: 500,
);
// Append index to mean_dictionary and print it (eventually this should turn into a list):
var woreda_code = ee.List(woredaNames).get(woreda);
mean_dict = mean_dict.set('Woreda_code', ee.String(woreda_code));
print(mean_dict);
javascript for-loop optimization google-earth-engine
javascript for-loop optimization google-earth-engine
asked Nov 13 '18 at 12:20
VindicareVindicare
184
184
Cross-posted as gis.stackexchange.com/q/301447/115
– PolyGeo
Dec 12 '18 at 7:38
add a comment |
Cross-posted as gis.stackexchange.com/q/301447/115
– PolyGeo
Dec 12 '18 at 7:38
Cross-posted as gis.stackexchange.com/q/301447/115
– PolyGeo
Dec 12 '18 at 7:38
Cross-posted as gis.stackexchange.com/q/301447/115
– PolyGeo
Dec 12 '18 at 7:38
add a comment |
1 Answer
1
active
oldest
votes
First of all, you should avoid using for loops on Earth Engine at all cost, it just bogs the system down and is not good for everyone (see the Looping section on this page). You can use nested mapping to loop over the feature collection and then all of the time periods to extract the information you need:
// import data
var region = ee.FeatureCollection("ft:1zRUOJL1LYCPJj-mjP6ZRx8sxYKNH8EwDw3EPP66K"),
modisNDVI = ee.ImageCollection("MODIS/MCD43A4_006_NDVI");
var startDate = ee.Date('2010-01-01'); // set analysis start time
var endDate = ee.Date('2010-12-31'); // set analysis end time
// calculate the number of months to process
var nMonths = ee.Number(endDate.difference(startDate,'month')).round();
var result = region.map(function(feature)
// map over each month
var timeDict = ee.List.sequence(0,nMonths).map(function (n)
// calculate the offset from startDate
var ini = startDate.advance(n,'month');
// advance just one month
var end = ini.advance(1,'month');
// filter and reduce
var data = modisNDVI.filterDate(ini,end).mean().reduceRegion(
reducer: ee.Reducer.mean(),
geometry: feature.geometry(),
scale: 1000
);
// return zonal mean with a time key
return data.combine(ee.Dictionary('time':ini));
);
// return feature with a timeseries property and results
return feature.set('timeseries',timeDict);
);
// print to see if it is doing what we expect...
print(result.select(["HRpcode",'timeseries']));
// Export the data to a table for further analysis
Export.table.toDrive(
collection:result,
description:"tester",
fileFormat:"CSV",
selectors:["HRpcode","timeseries"]
)
Link to code: https://code.earthengine.google.com/abf5eeb5c203310c11bf45c6714ae731
The results formatting may be a little funky in this implementation with the result being a feature collection with dictionaries as properties and not an array or table...but, hopefully this either gives you what you need or gives you a means to get what you need.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280885%2fcalculating-ndvi-per-region-month-year-with-google-earth-engine%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First of all, you should avoid using for loops on Earth Engine at all cost, it just bogs the system down and is not good for everyone (see the Looping section on this page). You can use nested mapping to loop over the feature collection and then all of the time periods to extract the information you need:
// import data
var region = ee.FeatureCollection("ft:1zRUOJL1LYCPJj-mjP6ZRx8sxYKNH8EwDw3EPP66K"),
modisNDVI = ee.ImageCollection("MODIS/MCD43A4_006_NDVI");
var startDate = ee.Date('2010-01-01'); // set analysis start time
var endDate = ee.Date('2010-12-31'); // set analysis end time
// calculate the number of months to process
var nMonths = ee.Number(endDate.difference(startDate,'month')).round();
var result = region.map(function(feature)
// map over each month
var timeDict = ee.List.sequence(0,nMonths).map(function (n)
// calculate the offset from startDate
var ini = startDate.advance(n,'month');
// advance just one month
var end = ini.advance(1,'month');
// filter and reduce
var data = modisNDVI.filterDate(ini,end).mean().reduceRegion(
reducer: ee.Reducer.mean(),
geometry: feature.geometry(),
scale: 1000
);
// return zonal mean with a time key
return data.combine(ee.Dictionary('time':ini));
);
// return feature with a timeseries property and results
return feature.set('timeseries',timeDict);
);
// print to see if it is doing what we expect...
print(result.select(["HRpcode",'timeseries']));
// Export the data to a table for further analysis
Export.table.toDrive(
collection:result,
description:"tester",
fileFormat:"CSV",
selectors:["HRpcode","timeseries"]
)
Link to code: https://code.earthengine.google.com/abf5eeb5c203310c11bf45c6714ae731
The results formatting may be a little funky in this implementation with the result being a feature collection with dictionaries as properties and not an array or table...but, hopefully this either gives you what you need or gives you a means to get what you need.
add a comment |
First of all, you should avoid using for loops on Earth Engine at all cost, it just bogs the system down and is not good for everyone (see the Looping section on this page). You can use nested mapping to loop over the feature collection and then all of the time periods to extract the information you need:
// import data
var region = ee.FeatureCollection("ft:1zRUOJL1LYCPJj-mjP6ZRx8sxYKNH8EwDw3EPP66K"),
modisNDVI = ee.ImageCollection("MODIS/MCD43A4_006_NDVI");
var startDate = ee.Date('2010-01-01'); // set analysis start time
var endDate = ee.Date('2010-12-31'); // set analysis end time
// calculate the number of months to process
var nMonths = ee.Number(endDate.difference(startDate,'month')).round();
var result = region.map(function(feature)
// map over each month
var timeDict = ee.List.sequence(0,nMonths).map(function (n)
// calculate the offset from startDate
var ini = startDate.advance(n,'month');
// advance just one month
var end = ini.advance(1,'month');
// filter and reduce
var data = modisNDVI.filterDate(ini,end).mean().reduceRegion(
reducer: ee.Reducer.mean(),
geometry: feature.geometry(),
scale: 1000
);
// return zonal mean with a time key
return data.combine(ee.Dictionary('time':ini));
);
// return feature with a timeseries property and results
return feature.set('timeseries',timeDict);
);
// print to see if it is doing what we expect...
print(result.select(["HRpcode",'timeseries']));
// Export the data to a table for further analysis
Export.table.toDrive(
collection:result,
description:"tester",
fileFormat:"CSV",
selectors:["HRpcode","timeseries"]
)
Link to code: https://code.earthengine.google.com/abf5eeb5c203310c11bf45c6714ae731
The results formatting may be a little funky in this implementation with the result being a feature collection with dictionaries as properties and not an array or table...but, hopefully this either gives you what you need or gives you a means to get what you need.
add a comment |
First of all, you should avoid using for loops on Earth Engine at all cost, it just bogs the system down and is not good for everyone (see the Looping section on this page). You can use nested mapping to loop over the feature collection and then all of the time periods to extract the information you need:
// import data
var region = ee.FeatureCollection("ft:1zRUOJL1LYCPJj-mjP6ZRx8sxYKNH8EwDw3EPP66K"),
modisNDVI = ee.ImageCollection("MODIS/MCD43A4_006_NDVI");
var startDate = ee.Date('2010-01-01'); // set analysis start time
var endDate = ee.Date('2010-12-31'); // set analysis end time
// calculate the number of months to process
var nMonths = ee.Number(endDate.difference(startDate,'month')).round();
var result = region.map(function(feature)
// map over each month
var timeDict = ee.List.sequence(0,nMonths).map(function (n)
// calculate the offset from startDate
var ini = startDate.advance(n,'month');
// advance just one month
var end = ini.advance(1,'month');
// filter and reduce
var data = modisNDVI.filterDate(ini,end).mean().reduceRegion(
reducer: ee.Reducer.mean(),
geometry: feature.geometry(),
scale: 1000
);
// return zonal mean with a time key
return data.combine(ee.Dictionary('time':ini));
);
// return feature with a timeseries property and results
return feature.set('timeseries',timeDict);
);
// print to see if it is doing what we expect...
print(result.select(["HRpcode",'timeseries']));
// Export the data to a table for further analysis
Export.table.toDrive(
collection:result,
description:"tester",
fileFormat:"CSV",
selectors:["HRpcode","timeseries"]
)
Link to code: https://code.earthengine.google.com/abf5eeb5c203310c11bf45c6714ae731
The results formatting may be a little funky in this implementation with the result being a feature collection with dictionaries as properties and not an array or table...but, hopefully this either gives you what you need or gives you a means to get what you need.
First of all, you should avoid using for loops on Earth Engine at all cost, it just bogs the system down and is not good for everyone (see the Looping section on this page). You can use nested mapping to loop over the feature collection and then all of the time periods to extract the information you need:
// import data
var region = ee.FeatureCollection("ft:1zRUOJL1LYCPJj-mjP6ZRx8sxYKNH8EwDw3EPP66K"),
modisNDVI = ee.ImageCollection("MODIS/MCD43A4_006_NDVI");
var startDate = ee.Date('2010-01-01'); // set analysis start time
var endDate = ee.Date('2010-12-31'); // set analysis end time
// calculate the number of months to process
var nMonths = ee.Number(endDate.difference(startDate,'month')).round();
var result = region.map(function(feature)
// map over each month
var timeDict = ee.List.sequence(0,nMonths).map(function (n)
// calculate the offset from startDate
var ini = startDate.advance(n,'month');
// advance just one month
var end = ini.advance(1,'month');
// filter and reduce
var data = modisNDVI.filterDate(ini,end).mean().reduceRegion(
reducer: ee.Reducer.mean(),
geometry: feature.geometry(),
scale: 1000
);
// return zonal mean with a time key
return data.combine(ee.Dictionary('time':ini));
);
// return feature with a timeseries property and results
return feature.set('timeseries',timeDict);
);
// print to see if it is doing what we expect...
print(result.select(["HRpcode",'timeseries']));
// Export the data to a table for further analysis
Export.table.toDrive(
collection:result,
description:"tester",
fileFormat:"CSV",
selectors:["HRpcode","timeseries"]
)
Link to code: https://code.earthengine.google.com/abf5eeb5c203310c11bf45c6714ae731
The results formatting may be a little funky in this implementation with the result being a feature collection with dictionaries as properties and not an array or table...but, hopefully this either gives you what you need or gives you a means to get what you need.
edited Dec 5 '18 at 7:27
answered Dec 5 '18 at 7:20
Kel MarkertKel Markert
1163
1163
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280885%2fcalculating-ndvi-per-region-month-year-with-google-earth-engine%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Cross-posted as gis.stackexchange.com/q/301447/115
– PolyGeo
Dec 12 '18 at 7:38