0

You can skip many of my tries, I the fifth one is almost there:

I am trying to generate a series of summaries for an image collection, where I get the mean, median, 90th quantile and 10th quantile for a series of polygons. My goal is to export either a shapefile with the attributes of mean, median, etc. I loaded this zipfile as assets which you can find here. This is what I have tried so far:

First try (export to raster)

var collection = ee.ImageCollection('COPERNICUS/S5P/NRTI/L3_CO')
  .select('CO_column_number_density')
  .filterDate('2019-06-01', '2019-06-11');
// clip to an asset
var clip = ee.FeatureCollection('projects/ee-my-derekcorcoran/assets/CitiesChile');
var clipped = collection.map(function(image) {  
    return image.clip(clip);
});


// get the mean of each feature in the collection   

var mean = clipped.map(function(image) {    
    return image.reduceRegions({
        collection: clip,
        reducer: ee.Reducer.mean(),
        scale: 500
    });
});     

// print the mean for each feature
print(mean);




//export the mean to a tif
Export.image.toDrive({
    image: mean,   
    description: 'meanCO',
    folder: 'EarthEngine',
    fileNamePrefix: 'meanCO',
    fileFormat: 'GeoTIFF',
    formatOptions: {
      cloudOptimized: true
    },
    skipEmptyTiles: true
});

This has seem to work until the last minute where it wont export it giving the following error:

ID: ZYUPAJJGL2U2PE4LLHGMXA5A
Phase: Failed
Runtime: 0s (started 2022-11-20 21:40:47 +0100)
Attempted 1 time
Error: Image.clipToBoundsAndScale, argument 'input': Invalid type. Expected type: Image<unknown bands>. Actual type: FeatureCollection. (Error code: 3)

Second try (export to shapefile)

Which led me to believe that this was generating a shapefile insted of a raster so I tried this:

var collection = ee.ImageCollection('COPERNICUS/S5P/NRTI/L3_CO')
  .select('CO_column_number_density')
  .filterDate('2019-06-01', '2019-06-11');
// clip to an asset
var clip = ee.FeatureCollection('projects/ee-my-derekcorcoran/assets/CitiesChile');
var clipped = collection.map(function(image) {  
    return image.clip(clip);
});


// get the mean of each feature in the collection   

var mean = clipped.map(function(image) {    
    return image.reduceRegions({
        collection: clip,
        reducer: ee.Reducer.mean(),
        scale: 500
    });
});     

// print the mean for each feature
print(mean);




//export the mean to a shp
Export.table.toDrive({
    collection: mean,   
    description: 'meanCO',
    folder: 'EarthEngine',
    fileNamePrefix: 'meanCO',
    fileFormat: 'SHP'
});

Which gave me the following error

Error: Unable to use a collection in an algorithm that requires a feature or image. This may happen when trying to use a collection of collections where a collection of features is expected; use flatten, or map a function to convert inner collections to features. Use clipToCollection (instead of clip) to clip an image to a collection. (Error code: 3)

Third try (flatten)

Because of the error I tried flatten.

var collection = ee.ImageCollection('COPERNICUS/S5P/NRTI/L3_CO')
  .select('CO_column_number_density')
  .filterDate('2019-06-01', '2019-06-11');
// clip to an asset
var clip = ee.FeatureCollection('projects/ee-my-derekcorcoran/assets/CitiesChile');
var clipped = collection.map(function(image) {  
    return image.clip(clip);
});


// get the mean of each feature in the collection   

var mean = clipped.map(function(image) {    
    return image.reduceRegions({
        collection: clip,
        reducer: ee.Reducer.mean(),
        scale: 500
    });
});     

// print the mean for each feature
print(mean);


var newTest =  mean.flatten()



//export the mean to a tif
Export.table.toDrive({
    collection: newTest,   
    description: 'meanCO',
    folder: 'EarthEngine',
    fileNamePrefix: 'meanCO',
    fileFormat: 'SHP'
});

This again give me a new error:

Error: Shapefiles cannot contain multiple geometry types; found 'LineString', 'Polygon'. (Error code: 3)

But I know I loaded only polygons

Fourth try (based on another question)

I found this question so I tried to adapt it to my code, this is what I did:

var collection = ee.ImageCollection('COPERNICUS/S5P/NRTI/L3_CO')
  .select('CO_column_number_density')
  .filterDate('2019-06-01', '2019-06-15');
// clip to an asset
var clip = ee.FeatureCollection('projects/ee-my-derekcorcoran/assets/CitiesChile');

var createData = function(img){
  var valueMean = img.reduceRegion({reducer: ee.Reducer.mean(), geometry: clip, scale: 30, maxPixels: 16016779});
  var valueMedian = img.reduceRegion({reducer: ee.Reducer.median(), geometry: clip, scale: 30, maxPixels: 16016779});
  var valueStdDev = img.reduceRegion({reducer: ee.Reducer.stdDev(), geometry: clip, scale: 30, maxPixels: 16016779});
  var valueVariance = img.reduceRegion({reducer: ee.Reducer.variance(), geometry: clip, scale: 30, maxPixels: 16016779});

  var ft = ee.Feature(null, {'Mean': valueMean.get('CO_column_number_density'),
                             'Median': valueMedian.get('CO_column_number_density'),
                             'stdDev': valueStdDev.get('CO_column_number_density'),
                             'Variance': valueVariance.get('CO_column_number_density'),
  });
  return ft;
};
// Apply the function to each image in dataset
var serie = collection.map(createData);

Export.table.toDrive({collection: serie, 
                      folder: 'EarthEngine',
                      description: 'datatableCO', 
                      fileFormat: 'CSV'})

This runned for 9 hours and no results

Fifth try:

this is almost there:

// Composite an image collection and clip it to a boundary.

// Load Landsat 7 raw imagery and filter it to April-July 2000.
var collection = ee.ImageCollection('COPERNICUS/S5P/NRTI/L3_CO')
  .select('CO_column_number_density')
  .filterDate('2019-06-01', '2019-12-12');

// Reduce the collection by taking the median.
var median = collection.median();

// Load a table of state boundaries and filter.
var fc = ee.FeatureCollection('projects/ee-my-derekcorcoran/assets/CitiesChile');

// Clip to the output image to the Nevada and Arizona state boundaries.
var clipped = median.clipToCollection(fc);

// Display the result.
Map.centerObject(fc);
var band_viz = {
  min: 0,
  max: 0.03,
  palette: ['black', 'blue', 'purple', 'cyan', 'green', 'yellow', 'red']
};

Map.addLayer(clipped, band_viz, 'S5P N02');

I can see the mean, now what is left to do is to summarise to transform each raster into the polygons as a mean for each, and export as shapefiles

1 Answer 1

0

Finally this worked

    
var collection = ee.ImageCollection('COPERNICUS/S5P/NRTI/L3_CO')
  .select('CO_column_number_density')
  .filterDate('2019-06-01', '2019-12-12');

// Reduce the collection by taking the median.
var median = collection.median();

// Load a table of state boundaries and filter.
var fc = ee.FeatureCollection('projects/ee-my-derekcorcoran/assets/CitiesChile').filter(ee.Filter.or(
        ee.Filter.eq('Ciudad', 'antofagasta'),
        ee.Filter.eq('Ciudad', 'chicureo')));

Map.centerObject(fc);
var band_viz = {
  min: 0,
  max: 0.03,
  palette: ['black', 'blue', 'purple', 'cyan', 'green', 'yellow', 'red']
};

//Map.addLayer(median, band_viz, 'S5P N02');

var bscale = median.projection().nominalScale();
print('median scale:', bscale); 

print(median)

var cityCO = median.reduceRegions({
  collection: fc,
  reducer: ee.Reducer.mean(),
  scale: 30 // the resolution of the GRIDMET dataset
});
print(cityCO);

Export.table.toDrive({
  collection: cityCO,
  description: 'meanCO2',
  folder: 'EarthEngine',
  fileFormat: 'SHP'
});   



Your Answer

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

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