0

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");
  • You will need to change the permissions on the Fusion Table; right now it's not publicly accessible. You've also got a typo in your code; there's a line break in the fusion table ID that shouldn't be there. Generally, for Earth Engine, the easiest way to share a code snippet is to paste a link to the code using the Get Link button in the code editor. – svangordon Sep 6 '18 at 16:00
1

Unless I'm misunderstanding your objectives, your code works just fine, you're just not exporting the collection with the distances. Export the withNearestDist collection instead of the hasNearest collection: Export.table.toDrive(withNearestDist).

If you want the name of the nearest country, you can add it in the map call where you set the nearestDist property.

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.