'Selecting specific landcover data in a loop and saving it as image collection

I am looking to select specific year of NLCD land cover data for specific year for my study region. I initially didnt know how to select specific products (NLCD have data for various years) from the NLCD image collection. But later i found i could just select single image using a "/[imagename]" so that is the method i am following.

Basically i have list of year, and for each year i want to:

  1. obtain the frequency of each landcover, save this data in loop for all image
  2. obtain the second image which only contains specific landcover (such as forests) and save this as image collection in the loop [This is something i want to present as video thumbnail to show change in forest]

My questions:

  1. How do i save the frequency histogram for each image in a loop so i can download it at once?
  2. How do i save each image in a loop as image collection so i can visualize temporal change at once?

Problem i am having with my code:

The error i get is: List (Error) Image.load: Image asset 'USGS/NLCD_RELEASES/2016_REL/ee.ComputedObject({ "type": "ArgumentRef", "value": "_MAPPING_VAR_2_0" })' not found.

I feel like i am making a mistake during the looping process. because my code works when i try to do this for individual image.

Codelink: https://code.earthengine.google.com/5a4e7ea4d9aeec5730a0312ad05273f8 Asset: https://code.earthengine.google.com/?asset=users/memugal33/Puyallup

Any help in fixing this will be greatly appreciated. Thank you.

// Import the NLCD collection.
/// Takes in the images i want and extracts frequency of each


//// List of year i need from the image collection /////
var yr = ee.List(['1992','2001','2004','2006','2008','2011','2013','2016']);

///// setting up my study area //////
var std = ee.FeatureCollection(puy) // i have this as a asset

/////// Writing a function that will iterate over each year ///// 

//// Maybe there is other way of selecting each image in image collection but 
//// i couldnt figure out anyother way

var each_img = function(s){ //// s is year number

//// This will select image for specific year from Land cover data ////
var dat = ee.Image('USGS/NLCD_RELEASES/2016_REL/'+s)
var cov = dat.select('landcover'); //// only select 'landcover'
var cov2 = cov.clip(std); //// clip for my study area


////// obtain frequency historgram ///////////
var freq = cov2.reduceRegions({
  reducer:ee.Reducer.frequencyHistogram(),
  collection: std,
  scale:30
})
var freq1 = freq.select(['histogram'],null,false);

var freq2 = freq1.map(function(feature){
  var dict = ee.Dictionary(feature.toDictionary().get('histogram'))
  feature = feature.set(dict)
  return feature
})

//// Is there a way to extract freq2 to a table or list in a loop
////so that i could download all histogram at once?

//// This is the only way i know to extract each histogram in a loop but this may not work?
Export.table.toDrive(freq2, "PuyallupLandCoverData"+s);

/////////////////////////////


////// select land cover i want /////////////////
var onlyforest = cov2.updateMask(cov2.eq(41).or(cov2.eq(42)).or(cov2.eq(43)))
/// This selects three land cover type (Three types of forests from the data)


////// How do i save each of this image (onlyforest) as a image collection in a loop?
// So i could visualize change in forest in the area




return ee.Feature(freq2)


}

var my = y2.map(each_img)
print(my)

///// Show Temporal Change in Forest in the study area ///////

//// For this to work each map needs to be a part of image collection ///// 

var visParams = {
  min: 0.0,
  max: 5000.0,
  palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'
  ],
};

// Create RGB visualization images for use as animation frames.
var rgbVis = imgcollection.map(function(img) {
  return img.visualize(viz).clip(std);
});

// Define GIF visualization parameters.
var gifParams = {
  // 'region': puyallup,
  'dimensions': 600,
  'crs': 'EPSG:3857',
  'framesPerSecond': 1
};

// Print the GIF URL to the console.
print(rgbVis.getVideoThumbURL(gifParams));

// // Render the GIF animation in the console.
print(ui.Thumbnail(rgbVis, gifParams));



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source