2

I am trying to work with landsat5 in Google Earth Engine and get some information for a list of locations (I have long/lat of the locations and their postal codes in a Fusion Table).

I am looking for a way to have Min, Max, Median, Mean and Count information for each location in the mentioned time period (not neighborhood buffers) based on the not-cloudy layers and I do not know how to do it. Please note that I am not interested in working with any neighborhood buffers, and what I like to export is some information about each location specifically, for the mentioned time period.

My first attempt was to first get rid of the cloudy images and then export the result. To do so, that was my first code:

var collection = ee.ImageCollection('LANDSAT/LT5_L1T')
    .filterDate('2005-07-01','2005-07-31');

// Create a cloud-free composite with custom parameters for cloud score threshold and percentile.
var customImage = ee.Algorithms.Landsat.simpleComposite({
    collection: collection,
    percentile: 50,
    cloudScoreRange: 5
});

Where the collection is an ImageCollection, but customImage is a simple Image which already got rid of the cloud based on the cloudScoreRange and computed the average of image layers (or at least that is what I am thinking it does!). Then I could find the proper value for each location in my list (by using map function) and export the final results:

var pcs_values = pcs.map(function(feature){
      var values = customImage.reduceRegion(ee.Reducer.firstNonNull(), feature.geometry(), 30);
      return feature.set({“Average”: values});
});

// Export Postal codes with NDVI for each period attached
Export.table.toDrive({
    collection: pcs_values,
    description: "Output”,
   fileFormat: 'CSV'
});

Where pcs is a FeatureCollection of the list of locations obtained from the corresponding fusion table and pcs_values is the value for each postal code (long/lat location coordinates). I had to work with reduceRegion() function because it is the only function who gets an image and returns a dictionary of values.

The problem now is that I want to have more information for each location, such as; the Average, Min, Max and Median of Landsat values and Count of the number of layers in that location for the mentioned period of time. To do so, I have to work with the original composite, and therefore the code looks like this:

var collection = ee.ImageCollection('LANDSAT/LT5_L1T').filterDate('2005-07-01','2005-07-31');

var first_reducer = ee.Reducer.firstNonNull();

var pcs_values = pcs.map(function(feature){
    var median_values = collection.median().reduceRegion(first_reducer, feature.geometry(), 30);
    var mean_values = collection.mean().reduceRegion(first_reducer, feature.geometry(), 30);
    var min_values = collection.min().reduceRegion(first_reducer, feature.geometry(), 30);
    var max_values = collection.max().reduceRegion(first_reducer, feature.geometry(), 30);
    var count_values = collection.count().reduceRegion(first_reducer, feature.geometry(), 30);
    return feature.set({'Median': median_values, 'Mean': mean_values, 'Max': max_values, 'Min': min_values, 'Count': count_values});
});

// Export Postal codes with NDVI for each period attached
Export.table.toDrive({
    collection: pcs_values,
    description: "Output”,
    fileFormat: 'CSV'
});

In this way, I first compute the Median, Mean, Min, Max and Count for the collection separately. Each of them will be an image. Then I use reduceRegion to be able to have the values in a dictionary and map them to the list of locations that I have. I will then export the output (pcs_values).

The problem is that by using the new code, I do not know how to get rid of the cloudy layers. I tried to write a loop with Iterate() function to loop over all the images in the collection and if the cloudScore of a layer is less than a threshold, get rid of it, but it is taking too long and the results are not correct.

I am looking for a way to have Min, Max, Median, Mean and Count information for each location in the mentioned time period (not neighborhood buffers) based on the not-cloudy layers and I do not know how to do it.

2
  • you want to get cloudless from a date in time is unlikely but you can mosaic images with the best cloud score using var scored_mosaic = ee.Algorithms.Landsat.simpleCloudScore(mosaic); developers.google.com/earth-engine/landsat
    – Mapperz
    Jun 26, 2017 at 19:02
  • 3
    I don't understand if you want to use one image (a composite) or all images in that period. You made both in your code and I don't get what you want. Jun 26, 2017 at 19:24

1 Answer 1

1

I could solve my problem by using the map() function:

// To find the non-cloudy layers and get rid of the rest.
var non_cloudy = collection.map(function(image){
    var cloudy_scene = image;
    // Add a cloud score band.  It is automatically called 'cloud'.
    var scored = ee.Algorithms.Landsat.simpleCloudScore(cloudy_scene);
    // Create a mask from the cloud score and combine it with the image mask.
    var mask = scored.select(['cloud']).lte(5);
    // Apply the mask to the image.
    var cloud_free_image = cloudy_scene.updateMask(mask);  
    return cloud_free_image;
});

Then I do the rest of the computations on the non_cloudy composite:

var pcs_value = pcs.map(function(feature){
    var median_values = non_cloudy.median().reduceRegion(first_reducer, feature.geometry(), 30);
    var mean_values = non_cloudy.mean().reduceRegion(first_reducer, feature.geometry(), 30);
    var min_values = non_cloudy.min().reduceRegion(first_reducer, feature.geometry(), 30);
    var max_values = non_cloudy.max().reduceRegion(first_reducer, feature.geometry(), 30);
    var count_values = non_cloudy.count().reduceRegion(first_reducer, feature.geometry(), 30);
    return feature.set({'Median': median_values, 'Mean': mean_values, 'Max': max_values, 'Min': min_values, 'Count': count_values});
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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