//Purpose: To determine percentage of buffer
//of Breeding Bird Survey route start point is
//composed of croplands, urban and built-up lands, 
//and cropland/natural vegetation mosaics

//////////
//Assets
/////////

//Load and point to Google Earth Engine Asset representing
//the Lower 48 states
var l48 = ee.FeatureCollection("Path to Lower 48 states asset")
.geometry()
.dissolve();

//Buffered the start points of Breeding Bird Survey routes
//by 39.4 km and upload as an asset to Google Earth Engine
//Due to memory limitations, the buffers were 
//split into two separate, complementary feature classes
//and uploaded as different assets. The script was then
//run for each set

var bbs = ee.FeatureCollection("Path to first set of BBS start point buffers");
//var bbs = ee.FeatureCollection("Path to second set of BBS start point buffers"); //currently commented out

//MODIS Land Cover 
var lc = ee.ImageCollection("MODIS/061/MCD12Q1")
.filter(ee.Filter.calendarRange(2004,2004,"year"))
.select("LC_Type1");

//////////
//Settings
/////////

//Coordinate reference system and scale
var crs = lc.first().projection();
var nScale = lc.first().projection().nominalScale();

//////////
//Processing
/////////

//Creating image using 2004
var lcI = lc.toBands()
.reproject(crs,null,nScale.multiply(2));

//Creating image to count all pixels
var lcAll = ee.Image(0)
.where(lcI.gt(0).and(lcI.lt(255)),1);

//Creating image to count all human influence pixels
var lcHuman = ee.Image(0)
.where(lcI.eq(12).or(lcI.eq(13).or(lcI.eq(14))),1);

//Combining into a single image collection
var lcCount = ee.ImageCollection([lcAll,lcHuman])
.toBands()
.rename(["all","human"]);

//Determining mean for each BBS buffer
var bbsLC = lcCount.reduceRegions({
  collection: bbs,
  reducer: ee.Reducer.sum().unweighted(),
  scale: nScale.multiply(2),
  crs: crs
});

//////////
//Exporting
//////////

Export.table.toDrive({
  collection: bbsLC,
  description:'bbsLC',
  folder: 'GEE',
  fileFormat: 'CSV'
});