I am trying to compute distances from points stored in a fusion table to the nearest political border (also from a Table) in Google Earth Engine.
I need to compute distances between points. I would need the country name at the nearest border (it is stored under the variable 'name' in the border-table).
Please find the code below,
var fg_points = ee.FeatureCollection('ft:1DVI9dvjC00QhP-
hZgO3wQeH45RO2FDtdVcvF0fZq');
var borders = ee.FeatureCollection('USDOS/LSIB/2013');
Map.setCenter(74.582748, 42.882004, 8);
// Define a spatial filter with max distance to points is 500.
var spatialFilter = ee.Filter.withinDistance({
distance: 500000,
leftField: '.geo',
rightField: '.geo',
maxError: 10
})
//join the points to borders
var joined = ee.Join.saveAll({
matchesKey: 'neighbors',
measureKey: 'distance',
ordering: 'distance'
}).apply({
primary: fg_points,
secondary: borders,
condition: spatialFilter
});
// Get rid of points w/o neighbors.
var hasNearest = joined.map(function(f) {
var neighsSize = ee.List(f.get('neighbors')).size();
return f.set('neighsSize', neighsSize);
}).filter(ee.Filter.gt('neighsSize', 1));
Map.addLayer(hasNearest, {color: 'red'}, 'hasNearest');
// Get distance to nearest point.
var withNearestDist = hasNearest.map(function(f) {
var nearestDist = ee.Feature(ee.List(f.get('neighbors')).get(1))
.get('distance');
return f.set('nearestDist', nearestDist);
});
Export.table.toDrive(hasNearest,
"Distance",
"Distance",
"Distance");
Get Link
button in the code editor.