I have a feature collection of points, some of which overlap with a polygon. I want to filter out the points that do not overlap with the polygon, and create a new feature collection. I am able to create a new feature collection of the points that do overlap with the polygon, but cannot isolate the other points. I also tried an if function, but it returned all as true.
Here is the link to Google Earth engine and the code I have used:
///// ADD POINT COLLECTION
var point1 = ee.Geometry.Point(-70.14380589573864,-41.572652921712034);
var point2 = ee.Geometry.Point(-69.55054417698864,-41.441014516371794);
var point3 = ee.Geometry.Point(-70.20972386448864,-42.218690593895474);
var point4 = ee.Geometry.Point(-69.50659886448864,-42.0067921623679);
var Points = ee.FeatureCollection([
ee.Feature(point1, {name: 'Point 1'}),
ee.Feature(point2, {name: 'Point 2'}),
ee.Feature(point3, {name: 'Point 3'}),
ee.Feature(point4, {name: 'Point 4'})
]);
Map.addLayer(Points,{},'points')
///// ADD POLYGON
var poly1 = [
[-70.70410863011364,-41.19760487450451],
[-70.56128636448864,-41.7245260870179],
[-68.99024144261364,-41.593197242859866],
[-69.08911839573864,-40.957435215547406]
];
var poly_1 = ee.Geometry.Polygon(poly1);
Map.addLayer(poly_1,{},'poly1')
/////// USING INTERSECTION FUNCTION- RETURNS THE INTERSECTING POINTS
// var Intersect = Points.geometry().intersection(poly_1, ee.ErrorMargin(1));
var Intersect = poly_1.intersection(Points, ee.ErrorMargin(1));
Map.addLayer(Intersect,{},'Intersect');
/////// USING AN IF FUNCTION-- RETURNS ALL AS TRUE
var checkOverlap = function(feature) {
var point = ee.Feature(feature);
var isOverlap = ee.Algorithms.If(
poly_1.intersection(point.geometry()), // Check if the point intersects the polygon
true, // Set the property to true if there is an overlap
false // Set the property to false if there is no overlap
);
return point.set('overlaps', isOverlap); // Add the 'overlaps' property to the point
};
// Map over the point collection and apply the overlap check function to each point
var pointsWithOverlap = Points.map(checkOverlap);
print(pointsWithOverlap);
Map.addLayer(pointsWithOverlap,{},'points w overlap')
var TrueOverlap = pointsWithOverlap.filter(ee.Filter.eq('overlaps', true));
Map.addLayer(TrueOverlap,{},'True Overlap');