I am trying to do a spatial join of two feature collections: points to watershed polygons. Many of the points are outside of the polygon area so I want to join the point to the nearest polygon. Ideally the output would be a table of points with the properties of the nearest watershed appended.
I have done a simple spatial join but my problems at the moment are:
- This outputs all the nearest polygons in the search area; and
- The output doesn't include the secondary feature collection properties in the output table.
How do I do this please?
Here is the code:
//var sg_fc: Table //imported table of points
// Load the HydroATLAS dataset.
var basinATLAS = ee.FeatureCollection('WWF/HydroATLAS/v1/Basins/level12');
// Define a spatial filter, with distance 10 km.
var distFilter = ee.Filter.withinDistance({
distance: 10000,
leftField: '.geo',
rightField: '.geo',
maxError: 10
});
// Define a saveAll join.
var distSaveAll = ee.Join.saveAll({
matchesKey: 'points',
measureKey: 'distance',
ordering: 'distance'
});
// Apply the join.
var spatialJoined = distSaveAll.apply(basinATLAS,sg_fc,distFilter);
Map.addLayer(spatialJoined, {}, 'spatial join');
Map.addLayer(sg_fc, {}, 'sites')
Export.table.toDrive(spatialJoined);
Link to code editor: https://code.earthengine.google.com/90243d26764b46441efc1b477db185cf