2

I am trying to export one table out of the 18 feature collections I have created, each feature collection has the same geometry (counties of the state of California) and has the County ID (property 'COUNTYFP'), and value of the land cover classification in hectares (e.g. '11' for the feature collection area11). How can I do this? I've tried Inner Join.. but no luck

The table I want would have column headers:

STATEFP, COUNTYFP, 11, 12, 21, 22, ... 90, 95

The rows would be the counties and their values.

MY CODE:

print('starting...');
// Load a FeatureCollection of US Counties.
var Counties = ee.FeatureCollection("TIGER/2016/Counties");

//Imports NLCD Land Cover Data
var LandCover = ee.Image('USGS/NLCD/NLCD2011');

var FP = '06';

var state = Counties.filter(ee.Filter.eq('STATEFP',FP));
Map.addLayer(state, {}, 'state');

// Clip the image to the polygon geometry
var LandCover = LandCover.clip(state);


// Extract the landcover band
var landcover = LandCover.select('landcover');
Map.addLayer(landcover, {}, 'landcover');

// create masks for each class, then covert into area by multiplying by pixel
// area and dividing by class #, then sum reduce over the counties
// then export each as a table (if you can aggregate into one table)
// https://gis.stackexchange.com/questions/230774/extracting-land-cover-type-in-google-earth-engine
var pixarea = ee.Image.pixelArea();

//Another solution can be found in this stackexchange forum question
// https://gis.stackexchange.com/questions/313288/calculating-area-by-classified-image-in-earth-engine


var lc11 = landcover.eq(11);
var lc12 = landcover.eq(12);
var lc21 = landcover.eq(21);
var lc22 = landcover.eq(22);
var lc23 = landcover.eq(23);
var lc24 = landcover.eq(24);
var lc31 = landcover.eq(31);
var lc41 = landcover.eq(41);
var lc42 = landcover.eq(42);
var lc43 = landcover.eq(43);
var lc51 = landcover.eq(51);
var lc52 = landcover.eq(52);
var lc71 = landcover.eq(71);
var lc72 = landcover.eq(72);
var lc73 = landcover.eq(73);
var lc74 = landcover.eq(74);
var lc81 = landcover.eq(81);
var lc82 = landcover.eq(82);
var lc90 = landcover.eq(90);
var lc95 = landcover.eq(95);

var mask11 = landcover.mask(lc11);
var mask12 = landcover.mask(lc12);
var mask21 = landcover.mask(lc21);
var mask22 = landcover.mask(lc22);
var mask23 = landcover.mask(lc23);
var mask24 = landcover.mask(lc24);
var mask31 = landcover.mask(lc31);
var mask41 = landcover.mask(lc41);
var mask42 = landcover.mask(lc42);
var mask43 = landcover.mask(lc43);
var mask51 = landcover.mask(lc51);
var mask52 = landcover.mask(lc52);
var mask71 = landcover.mask(lc71);
var mask72 = landcover.mask(lc72);
var mask73 = landcover.mask(lc73);
var mask74 = landcover.mask(lc74);
var mask81 = landcover.mask(lc81);
var mask82 = landcover.mask(lc82);
var mask90 = landcover.mask(lc90);
var mask95 = landcover.mask(lc95);


var pix2area11 = mask11.select('landcover').multiply(pixarea.select('area')).divide(110000);
var pix2area12 = mask12.select('landcover').multiply(pixarea.select('area')).divide(120000);
var pix2area21 = mask21.select('landcover').multiply(pixarea.select('area')).divide(210000);
var pix2area22 = mask22.select('landcover').multiply(pixarea.select('area')).divide(220000);
var pix2area23 = mask23.select('landcover').multiply(pixarea.select('area')).divide(230000);
var pix2area24 = mask24.select('landcover').multiply(pixarea.select('area')).divide(240000);
var pix2area31 = mask31.select('landcover').multiply(pixarea.select('area')).divide(310000);
var pix2area41 = mask41.select('landcover').multiply(pixarea.select('area')).divide(410000);
var pix2area42 = mask42.select('landcover').multiply(pixarea.select('area')).divide(420000);
var pix2area43 = mask43.select('landcover').multiply(pixarea.select('area')).divide(430000);
var pix2area51 = mask51.select('landcover').multiply(pixarea.select('area')).divide(510000);
var pix2area52 = mask52.select('landcover').multiply(pixarea.select('area')).divide(520000);
var pix2area71 = mask71.select('landcover').multiply(pixarea.select('area')).divide(710000);
var pix2area72 = mask72.select('landcover').multiply(pixarea.select('area')).divide(720000);
var pix2area73 = mask73.select('landcover').multiply(pixarea.select('area')).divide(730000);
var pix2area74 = mask74.select('landcover').multiply(pixarea.select('area')).divide(740000);
var pix2area81 = mask81.select('landcover').multiply(pixarea.select('area')).divide(810000);
var pix2area82 = mask82.select('landcover').multiply(pixarea.select('area')).divide(820000);
var pix2area90 = mask90.select('landcover').multiply(pixarea.select('area')).divide(900000);
var pix2area95 = mask95.select('landcover').multiply(pixarea.select('area')).divide(950000);


Map.addLayer(pix2area11, {}, 'Open Water');
Map.addLayer(pix2area81, {}, 'Pasture Land');
Map.addLayer(pix2area82, {}, 'Crop Land');

pix2area11 = pix2area11.select('landcover');
pix2area12 = pix2area12.select('landcover');
pix2area21 = pix2area21.select('landcover');
pix2area22 = pix2area22.select('landcover');
pix2area23 = pix2area23.select('landcover');
pix2area24 = pix2area24.select('landcover');
pix2area31 = pix2area31.select('landcover');
pix2area41 = pix2area41.select('landcover');
pix2area42 = pix2area42.select('landcover');
pix2area43 = pix2area43.select('landcover');
pix2area51 = pix2area51.select('landcover');
pix2area52 = pix2area52.select('landcover');
pix2area71 = pix2area71.select('landcover');
pix2area72 = pix2area72.select('landcover');
pix2area73 = pix2area73.select('landcover');
pix2area74 = pix2area74.select('landcover');
pix2area81 = pix2area81.select('landcover');
pix2area82 = pix2area82.select('landcover');
pix2area90 = pix2area90.select('landcover');
pix2area95 = pix2area95.select('landcover');

var area11 = pix2area11.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area12 = pix2area12.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area21 = pix2area21.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area22 = pix2area22.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area23 = pix2area23.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area24 = pix2area24.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area31 = pix2area31.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area41 = pix2area41.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area42 = pix2area42.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area43 = pix2area43.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area51 = pix2area51.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area52 = pix2area52.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area71 = pix2area71.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area72 = pix2area72.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area73 = pix2area73.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area74 = pix2area74.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area81 = pix2area81.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area82 = pix2area82.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area90 = pix2area90.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

var area95 = pix2area95.reduceRegions({
  collection: state,
  reducer: ee.Reducer.sum(),
  scale: 30,
});


//////////////// EXPORT HERE  ///////////////////
//https://stackoverflow.com/questions/48562815/joining-two-feature-collections-by-id-in-earth-engine-javascript-api
//https://developers.google.com/earth-engine/debugging
// USE REDUCE REGION NOT REDUCE REGIONSSS!!!! Tried but still timing now. 


// Print the first feature, to illustrate the result.
print(ee.Feature(area11.first()));

// Translate feature collection to table with necessary identifiers

// Turn the property name 'sum' into the land cover classification:

area11 = area11.select(['sum', 'COUNTYFP', 'STATEFP'], ['11','COUNTYFP','STATEFP']);
area12 = area12.select(['sum', 'COUNTYFP', 'STATEFP'], ['12','COUNTYFP','STATEFP']);
area21 = area21.select(['sum', 'COUNTYFP', 'STATEFP'], ['21','COUNTYFP','STATEFP']);
area22 = area22.select(['sum', 'COUNTYFP', 'STATEFP'], ['22','COUNTYFP','STATEFP']);
area23 = area23.select(['sum', 'COUNTYFP', 'STATEFP'], ['23','COUNTYFP','STATEFP']);
area24 = area24.select(['sum', 'COUNTYFP', 'STATEFP'], ['24','COUNTYFP','STATEFP']);
area31 = area31.select(['sum', 'COUNTYFP', 'STATEFP'], ['31','COUNTYFP','STATEFP']);
area41 = area41.select(['sum', 'COUNTYFP', 'STATEFP'], ['41','COUNTYFP','STATEFP']);
area42 = area42.select(['sum', 'COUNTYFP', 'STATEFP'], ['42','COUNTYFP','STATEFP']);
area43 = area43.select(['sum', 'COUNTYFP', 'STATEFP'], ['43','COUNTYFP','STATEFP']);
area51 = area21.select(['sum', 'COUNTYFP', 'STATEFP'], ['51','COUNTYFP','STATEFP']);
area52 = area22.select(['sum', 'COUNTYFP', 'STATEFP'], ['52','COUNTYFP','STATEFP']);
area71 = area21.select(['sum', 'COUNTYFP', 'STATEFP'], ['71','COUNTYFP','STATEFP']);
area72 = area22.select(['sum', 'COUNTYFP', 'STATEFP'], ['72','COUNTYFP','STATEFP']);
area73 = area23.select(['sum', 'COUNTYFP', 'STATEFP'], ['73','COUNTYFP','STATEFP']);
area74 = area24.select(['sum', 'COUNTYFP', 'STATEFP'], ['74','COUNTYFP','STATEFP']);
area81 = area21.select(['sum', 'COUNTYFP', 'STATEFP'], ['81','COUNTYFP','STATEFP']);
area82 = area22.select(['sum', 'COUNTYFP', 'STATEFP'], ['82','COUNTYFP','STATEFP']);
area90 = area23.select(['sum', 'COUNTYFP', 'STATEFP'], ['90','COUNTYFP','STATEFP']);
area95 = area24.select(['sum', 'COUNTYFP', 'STATEFP'], ['95','COUNTYFP','STATEFP']);

print(area11);
Map.addLayer(area11, {}, 'area11');


var List = ee.List([11 , area11.get('COUNTYFP')])
print(List)

// Can I join a feature collections??? Merge???
//https://developers.google.com/earth-engine/joins_simple
// Use an equals filter to define how the collections match.
var filter = ee.Filter.equals({
  leftField: 'COUNTYFP',
  rightField: 'COUNTYFP'
});



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


// Apply the join.
var Joined = Join.apply(area11, area12, filter);
//var Joined2 = Join.apply(Joined, area21, filter);
print(Joined.limit(1));

// Export the FeatureCollection.
//Export.table.toDrive({
//  collection: Joined,
//  description: 'test',
//  fileFormat: 'CSV'
//});


// https://developers.google.com/earth-engine/exporting

print('done');
1

You are on the right track with inner join as it is working like it should. What join does is it returns features where your join condition match and put the matching features as properties named 'primary' and 'secondary' of a feature with null geometry. All that is left is to reformat it to a proper feature collection with properties from both feature that I like to call cleaning the join.

function cleanJoin(feature){
  return ee.Feature(feature.get('primary')).copyProperties(feature.get('secondary'));
}
Joined = Joined.map(cleanJoin);

I also think there is a much shorter and simpler way you could achieve the same things that you want. If you are interested in that you can view that here

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.