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-
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'
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))
return f.set('nearestDist', nearestDist);

  • 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
    Commented Sep 6, 2018 at 16:00

2 Answers 2


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.


First, you need to calculate the Point you want such as Define a Point object and Define other inputs then you get the point. distance(...) =............... this number is instead of distance 500000. Code for Point Distance below:

// Define a Point object.
var point = ee.Geometry.Point(101.88041615585351,21.15823665733664);

// Define other inputs.
var inputGeom = ee.Geometry.Point(107.26371693710351,14.915008918230788);

// Apply the distance method to the Point object.
var pointDistance = point.distance({'right': inputGeom, 'maxError': 1});

// Print the result to the console.
print('point.distance(...) =', pointDistance);

// Display relevant geometries on the map.
Map.centerObject(table, 8);
             {'color': 'black'},
             'Geometry [black]: point');
             {'color': 'blue'},
             'Parameter [blue]: inputGeom');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.