0

I'm trying to calculate the water areas and the statistics of each band for each water areas to see if there are seasonal changes.

The exported .csv file only have the columns ("GNIS_ID","AreaSqKm","waterArea") before join. I printed the variable, Join, and it does have all the column I need. How come they didn't show in the exported CSV file? Did I miss something?

    var CollectMonth = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
      .filterBounds(HI_poly)
      .sort('CLOUD_COVER')
      .filterDate('2014-01-01', '2014-01-30')
      .map(function(image){return image.clip(USGS_WBD)})  // clip the images by the;
      print(CollectMonth);

    // Calculate Water Area
    var waterThreshold = 324;

    var WaterAreaFunction = function(image){
      //add the water band to the image
      var water = image.select(['pixel_qa']).rename('water');
      //get pixels equal the threshold
      var water01 = water.eq(waterThreshold);
      //mask those pixels from the image
      image = image.updateMask(water01).addBands(water);

      var area = ee.Image.pixelArea();
      var waterArea = water01.multiply(area).rename('waterArea');

      image = image.addBands(waterArea);

      var stats = waterArea.reduceRegion({
        reducer: ee.Reducer.sum(), 
        geometry: USGS_WBD, 
        scale: 30,
      });

      return image.set(stats);
    };

    var l8Mosaic = CollectMonth.map(WaterAreaFunction).mosaic().multiply(0.0001);
    var l8Mosaic_area = l8Mosaic.divide(1000000)

    //statistics
    var WBD_area = l8Mosaic_area.reduceRegions({
      collection: USGS_WBD,
      reducer: ee.Reducer.sum(),
      scale: 30 // resolution
    }).select(["GNIS_ID","AreaSqKm","waterArea"]);
    print("area:",WBD_area);

    var WBD_value = l8Mosaic.select(["B1","B2","B3","B4","B5","B6","B7"])
                            .rename(["B1_mean","B2_mean","B3_mean","B4_mean","B5_mean","B6_mean","B7_mean"])
    var WBD_mean = WBD_value.reduceRegions({
      collection: USGS_WBD,
      reducer: ee.Reducer.mean(),
      scale: 30 // resolution
    }).select(["GNIS_ID","B1_mean","B2_mean","B3_mean","B4_mean","B5_mean","B6_mean","B7_mean"])
    print("mean:",WBD_mean);

    var WBD_value = l8Mosaic.select(["B1","B2","B3","B4","B5","B6","B7"])
                            .rename(["B1_median","B2_median","B3_median","B4_median","B5_median","B6_median","B7_median"])
    var WBD_median = WBD_value.reduceRegions({
      collection: USGS_WBD,
      reducer: ee.Reducer.median(),
      scale: 30 // resolution
    }).select(["GNIS_ID","B1_median","B2_median","B3_median","B4_median","B5_median","B6_median","B7_median"])
    print("median:",WBD_median);

    var WBD_value = l8Mosaic.select(["B1","B2","B3","B4","B5","B6","B7"])
                            .rename(["B1_min","B2_min","B3_min","B4_min","B5_min","B6_min","B7_min"])
    var WBD_min = WBD_value.reduceRegions({
      collection: USGS_WBD,
      reducer: ee.Reducer.min(),
      scale: 30 // resolution
    }).select(["GNIS_ID","B1_min","B2_min","B3_min","B4_min","B5_min","B6_min","B7_min"])
    print("min:",WBD_min);

    var WBD_value = l8Mosaic.select(["B1","B2","B3","B4","B5","B6","B7"])
                            .rename(["B1_max","B2_max","B3_max","B4_max","B5_max","B6_max","B7_max"])
    var WBD_max = WBD_value.reduceRegions({
      collection: USGS_WBD,
      reducer: ee.Reducer.max(),
      scale: 30 // resolution
    }).select(["GNIS_ID","B1_max","B2_max","B3_max","B4_max","B5_max","B6_max","B7_max"])
    print("max:",WBD_max);

    var WBD_value = l8Mosaic.select(["B1","B2","B3","B4","B5","B6","B7"])
                            .rename(["B1_WBD10","B2_WBD10","B3_WBD10","B4_WBD10","B5_WBD10","B6_WBD10","B7_WBD10"])
    var WBD10 = WBD_value.reduceRegions({
      collection: USGS_WBD,
      reducer: ee.Reducer.percentile([10]),
      scale: 30 // resolution
    }).select(["GNIS_ID","B1_WBD10","B2_WBD10","B3_WBD10","B4_WBD10","B5_WBD10","B6_WBD10","B7_WBD10"])
    print("WBD10:",WBD10);

    var WBD_value = l8Mosaic.select(["B1","B2","B3","B4","B5","B6","B7"])
                            .rename(["B1_WBD90","B2_WBD90","B3_WBD90","B4_WBD90","B5_WBD90","B6_WBD90","B7_WBD90"])
    var WBD90 = WBD_value.reduceRegions({
      collection: USGS_WBD,
      reducer: ee.Reducer.percentile([90]),
      scale: 30 // resolution
    }).select(["GNIS_ID","B1_WBD90","B2_WBD90","B3_WBD90","B4_WBD90","B5_WBD90","B6_WBD90","B7_WBD90"])
    print("WBD90:",WBD90);

    var WBD_value = l8Mosaic.select(["B1","B2","B3","B4","B5","B6","B7"])
                            .rename(["B1_std","B2_std","B3_std","B4_std","B5_std","B6_std","B7_std"])
    var WBD_std = WBD_value.reduceRegions({
      collection: USGS_WBD,
      reducer: ee.Reducer.stdDev(),
      scale: 30 // resolution
    }).select(["GNIS_ID","B1_std","B2_std","B3_std","B4_std","B5_std","B6_std","B7_std"])
    print("std:",WBD_std);

    var WBD_value = l8Mosaic.select(["B1","B2","B3","B4","B5","B6","B7"])
                            .rename(["B1_variance","B2_variance","B3_variance","B4_variance","B5_variance","B6_variance","B7_variance"])
    var WBD_variance = WBD_value.reduceRegions({
      collection: USGS_WBD,
      reducer: ee.Reducer.variance(),
      scale: 30 // resolution
    }).select(["GNIS_ID","B1_variance","B2_variance","B3_variance","B4_variance","B5_variance","B6_variance","B7_variance"])
    print("variance:",WBD_variance);


    // =============== Join ============================
    // Join two collection
    // Use an equals filter to define how the collections match.
    var filter = ee.Filter.equals({
      leftField: 'GNIS_ID',
      rightField: 'GNIS_ID'
    });

    // Define the join.
    var innerJoin = ee.Join.inner();

    // Apply the join.
    var Join = innerJoin.apply(WBD_area, WBD_mean, filter);
    Join = Join.map(function(pair) {
      var f1 = ee.Feature(pair.get('primary'));
      var f2 = ee.Feature(pair.get('secondary'));
      return f1.set(f2.toDictionary());
    });

    // Apply the join.
    var Join = innerJoin.apply(Join, WBD_median,filter);
    Join = Join.map(function(pair) {
      var f1 = ee.Feature(pair.get('primary'));
      var f2 = ee.Feature(pair.get('secondary'));
      return f1.set(f2.toDictionary());
    });

    // // Apply the join.
    var Join = innerJoin.apply(Join, WBD_min,filter);
    Join = Join.map(function(pair) {
      var f1 = ee.Feature(pair.get('primary'));
      var f2 = ee.Feature(pair.get('secondary'));
      return f1.set(f2.toDictionary());
    });

    // // Apply the join.
    var Join = innerJoin.apply(Join, WBD_max,filter);
    Join = Join.map(function(pair) {
      var f1 = ee.Feature(pair.get('primary'));
      var f2 = ee.Feature(pair.get('secondary'));
      return f1.set(f2.toDictionary());
    });

    // // Apply the join.
    var Join = innerJoin.apply(Join, WBD10,filter);
    Join = Join.map(function(pair) {
      var f1 = ee.Feature(pair.get('primary'));
      var f2 = ee.Feature(pair.get('secondary'));
      return f1.set(f2.toDictionary());
    });

    // // Apply the join.
    var Join = innerJoin.apply(Join, WBD90,filter);
    Join = Join.map(function(pair) {
      var f1 = ee.Feature(pair.get('primary'));
      var f2 = ee.Feature(pair.get('secondary'));
      return f1.set(f2.toDictionary());
    });

    // // Apply the join.
    var Join = innerJoin.apply(Join, WBD_std,filter);
    Join = Join.map(function(pair) {
      var f1 = ee.Feature(pair.get('primary'));
      var f2 = ee.Feature(pair.get('secondary'));
      return f1.set(f2.toDictionary());
    });

    // // Apply the join.
    var Join = innerJoin.apply(Join, WBD_variance,filter);
    Join = Join.map(function(pair) {
      var f1 = ee.Feature(pair.get('primary'));
      var f2 = ee.Feature(pair.get('secondary'));
      return f1.set(f2.toDictionary());
    });
    print(Join)

    // Export the FeatureCollection.
    Export.table.toDrive({
      collection: Join,
      description: 'Monthly_Lansat8_SR',
      fileFormat: 'CSV'
    });
  • Welcome to GIS SE. As a new user, please take the Tour which emphasizes the importance of asking One question per Question. Please Edit your question to focus on your most pressing question. – Vince Apr 19 at 0:19
  • Just edited, thank you. – Yu-fen Apr 19 at 0:56
  • Your title still has an 'and' in it; this indicates multiple questions – Vince Apr 19 at 3:51
  • Sorry I thought I deleted it already. Thanks for keeping an eye – Yu-fen Apr 19 at 4:03
0

Have you tried the selectors parameter of Export.table.toDrive()? If that doesn't work, then per-filter the table to remove features that are null for one or more of the properties you want to export.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.