在Earth Engine Javascript API中通过ID加入两个要素集

时间:2018-02-01 12:29:05

标签: javascript google-earth-engine

我只想加入这两个集合,即连接的要素集合具有主要和次要要素集合的所有属性。

// Create the primary collection.
var primaryFeatures = ee.FeatureCollection([
  ee.Feature(null, {foo: 0, ID: 'a'}),
  ee.Feature(null, {foo: 1, ID: 'b'}),
  ee.Feature(null, {foo: 1, ID: 'c'}),
  ee.Feature(null, {foo: 2, ID: 'd'}),
]);

// Create the secondary collection.
var secondaryFeatures = ee.FeatureCollection([
  ee.Feature(null, {bar: 1, ID: 'a'}),
  ee.Feature(null, {bar: 1, ID: 'b'}),
  ee.Feature(null, {bar: 2, ID: 'c'}),
  ee.Feature(null, {bar: 3, ID: 'd'}),
]);

// Use an equals filter to specify how the collections match.
var toyFilter = ee.Filter.equals({
  leftField: 'ID',
  rightField: 'ID'
});

// Define the join.
var innerJoin = ee.Join.simple()

// Apply the join.
var toyJoin = innerJoin.apply(primaryFeatures, secondaryFeatures,  toyFilter);


// Print the result.
print('Inner join toy example:', toyJoin);

最终的toyJoin功能集应该具有5个功能,包含3个属性ID,foo和bar。非常感谢你!

1 个答案:

答案 0 :(得分:2)

我不希望输出中有5个功能,因为您的输入集合中的ID之间只有四个唯一ID和1:1关系。无论如何,要获得内部联接,请使用ee.Join.inner()。要合并结果,请在内部联接上进行映射:

// Create the primary collection.
var primaryFeatures = ee.FeatureCollection([
  ee.Feature(null, {foo: 0, ID: 'a'}),
  ee.Feature(null, {foo: 1, ID: 'b'}),
  ee.Feature(null, {foo: 1, ID: 'c'}),
  ee.Feature(null, {foo: 2, ID: 'd'}),
]);

// Create the secondary collection.
var secondaryFeatures = ee.FeatureCollection([
  ee.Feature(null, {bar: 1, ID: 'a'}),
  ee.Feature(null, {bar: 1, ID: 'b'}),
  ee.Feature(null, {bar: 2, ID: 'c'}),
  ee.Feature(null, {bar: 3, ID: 'd'}),
]);

// Use an equals filter to specify how the collections match.
var toyFilter = ee.Filter.equals({
  leftField: 'ID',
  rightField: 'ID'
});

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

// Apply the join.
var toyJoin = innerJoin.apply(primaryFeatures, secondaryFeatures,  toyFilter);

// Print the result.
print('Inner join toy example:', toyJoin);

print(toyJoin.map(function(pair) {
  var f1 = ee.Feature(pair.get('primary'));
  var f2 = ee.Feature(pair.get('secondary'));
  return f1.set(f2.toDictionary());
}));