Google Earth Engine – Export Monthly PRISM Precipitation Data for US Counties

exportgoogle-earth-enginegoogle-earth-engine-javascript-apiprecipitation

I'm trying to extract a monthly precipitation value from the PRISM climate dataset for each month of 2018 through 2021 (48 months total) for each US county in my shapefile (952 counties total) in Google Earth Engine. My goal is to export this as a table to then use for analyses in R. I believe I've completed the first step in the code below, but I'm unable to export the data to my Google Drive. Every time I try to export the data, the export runs for >4 hours without completion. I've tried removing the geometry portion and I've tried running this on only 2-4 months' worth of data, but my export never finishes.

Is this due to the size of the data/computer and network strength, or is there something in my code that is causing this export to continue for hours without successfully exporting?

I'm new to GEE and coding in general.


//Shapefile: 15 states, all counties
var countyList = ee.FeatureCollection(table);

print(countyList.size(), 'counties');

//Map.addLayer(countyList,{}, 'counties');
//Map.setCenter(-84.106, 42.424, 4.5);

//PRISM climate dataset
var precip = ee.ImageCollection('OREGONSTATE/PRISM/AN81m')
    .select('ppt')
    .filterBounds (countyList)
    .filterDate('2018-01', '2022-01');
    
// Specify the date range required
var startDate = ee.Date('2018-01');
var endDate = ee.Date('2022-01');
print('Observations begin on: ', startDate.format('YYYY-MM'),
  'and end on: ', endDate.format('YYYY-MM'));

// Calculate the number of months to process
var nMonths = ee.Number(endDate.difference(startDate, 'month')).round();
print('Number of months to process: ', nMonths);

//Calculate monthly precipitation for each county
var monthprecipcounty = ee.FeatureCollection(ee.List.sequence (0, nMonths.subtract(1))
  .map(function(monthoffset) {
    var month = startDate.advance(ee.Number(monthoffset), 'months')
    var monthlymean = precip
      .filterDate(month, month.advance (1, 'months'))
      .mean()
    return countyList.map(function (county) {
      var mean = monthlymean.reduceRegion({
        reducer: ee.Reducer.mean(),
        geometry: county.geometry(),
        scale: 4638.3
      })
      return county
        .set(mean)
        .set('month', month.format('yyyy-MM'))
    })
  })
  ).flatten();

//Remove geometry from data for export: I tried this but it didn't make export any simpler
//var featureCollection = monthprecipcounty.map(function(feat){
//  var nullfeat = ee.Feature(null)
//  return nullfeat.copyProperties(feat)
//})

//Export county-level precip data to Google Drive
Export.table.toDrive({
    collection: monthprecipcounty, 
    description: 'precip', 
    folder: 'this',
    fileNamePrefix: 'precip', 
    fileFormat: 'CSV',
});

https://code.earthengine.google.com/c2ae03b6ec2d7bfe5151aafac3a85a54

Best Answer

The problem is related with the geometry of complete shapefile. If you try to print monthprecipcounty Feature Collection you will get the following error message:

FeatureCollection (Error) Collection.geometry: Geometry has too many edges (2533749 > 2000000).

For avoided this error, I only considered the first 100 elements of 952 total counties and simplifying each feature geometry as follows (it was also returned features with null geometry in monthprecipcounty to eliminate the possibility of writing in the CSV file a lot of unnecessary characters to reduce processing time at the server):

//Shapefile: 15 states, all counties
var countyList = ee.FeatureCollection(table).limit(100);

print(countyList.size(), 'counties');

countyList = ee.FeatureCollection(countyList.map(function (feat) {
  
  return ee.Feature(feat.geometry().simplify({'maxError': 1}));
  //return ee.Feature(feat.geometry());
  
}));

Complete code can be obtained from here.

After running above code for 20 minutes, I got a CSV file with 4801 records (a little bit more than the 10 % of your counties). It looks as follows opened with LibreOffice Calc.

enter image description here

So, you can try it for complete shapefile or split it in smaller parts. You decide then.

Related Question