Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. function addMask(img){
  2.  
  3. // grabbing the attached image
  4. var fmask_img = img.get('fmask');
  5.  
  6. // Trying to access a band in the original image works
  7. //var b1 = img.select(["B1"]);
  8.  
  9. // Trying to access a band in the image doesn't work
  10. //var fmask = fmask_img.select(["fmask"]);
  11.  
  12. //trying to add a band from the attached image to the orignal image doesn't work
  13. //var newImg = img.addBands(fmask_img, ["fmask"])
  14.  
  15. //trying to all bands from the attached image to the orignal image also doesn't work
  16. //var newImg = img.addBands(fmask_img, ["fmask"])
  17.  
  18. //even trying to the origianl image to is self doesn't work
  19. //var newImg = img.addBands(img)
  20.  
  21. return img // returning the unchanged image instead of newImg
  22. }
  23.  
  24. // Load a primary collection: Landsat imagery.
  25. var primary = ee.ImageCollection('LC8_L1T')
  26. .filterDate('2014-04-01', '2014-06-01')
  27. .filterBounds(ee.Geometry.Point(-122.092, 37.42));
  28.  
  29. // Load a secondary collection: MODIS imagery.
  30. var secondary = ee.ImageCollection('LANDSAT/LC8_L1T_TOA_FMASK')
  31. .filterDate('2014-04-01', '2014-06-01')
  32. .filterBounds(ee.Geometry.Point(-122.092, 37.42));
  33.  
  34. // Create a filter to define a match based on the scene ID.
  35. var sceneFilter = ee.Filter.equals({
  36. leftField: "LANDSAT_SCENE_ID",
  37. rightField: "LANDSAT_SCENE_ID"
  38. })
  39.  
  40. // Define the join.
  41. var saveFirstJoin = ee.Join.saveFirst({
  42. matchKey: 'fmask',
  43. ordering: 'system:time_start',
  44. ascending: true
  45. });
  46.  
  47.  
  48. // Apply the join.
  49. // This returns an image collection
  50. var landsatFmask = saveFirstJoin.apply(primary, secondary, sceneFilter);
  51.  
  52. // Mapping a simple function where input = output returns an image collection
  53. var fmask = landsatFmask.map(addMask);
  54.  
  55. // applying a reducer on the orignal collection works
  56. var primary_median = primary.median()
  57.  
  58. //applying a reducer on the orignalnew collection works doesn't work
  59. //var fmask_median = fmask.median()
  60. //var joined_median = landsatFmask.median()
  61.  
  62.  
  63. // Display the result.
  64. // the orginal collection
  65. print('raw:', primary)
  66. // looks the same as the orginal, only difference is that images inside the collection have an additional property "fmask" with the joined image inside
  67. print('Join.saveAll:', landsatFmask);
  68. // looks the same as the joined collection, but now the band list of the images collection is empty ??
  69. print('fmask:', fmask);
  70.  
  71. function addMask(img){
  72.  
  73. // cast the input image
  74. var i = ee.Image(img);
  75. // grabbing the attached image and cast it as well
  76. var fmask_img = ee.Image(img.get('fmask'));
  77.  
  78. //now i can add the band
  79. var newImg = i.addBands(fmask_img, ["fmask"])
  80.  
  81. return newImg
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement