# Keeping properties in an Intersection - Google Earth Engine

I have a set of points and I need to classify them according to which polygon they belong. The natural way of doing this is creating the intersections between all points and each polygon. The points have properties I want to keep and the intersection function requires me to have the polygon on the left side and I lose the properties of the input of the right side. How should I go about this? If I use the points on the left side it doesn't work.

I want an output that I can download as a .csv with each point, it's characteristics, and an indicator of the polygon they belong.

Code:

``````var p1 = ee.Feature(p1, {id: 'id1'});
var p2 = ee.Feature(p2, {id: 'id2'});
var p3 = ee.Feature(p3, {id: 'id3'});
var p4 = ee.Feature(p4, {id: 'id4'});

var points=ee.FeatureCollection([p1,p2,p3,p4]);
print(points,' points');

var cc1 = poly1.intersection(points, ee.ErrorMargin(1));
var cc2 = poly2.intersection(points, ee.ErrorMargin(1));

var ccs=ee.FeatureCollection([cc1,cc2]);
print(ccs,'ccs');
``````

I think it's best practice to (i) construct a feature collection of the points; (ii) construct a feature collection of the polygons you have; then (iii) map over both collections to calculate the intersection for every single point; finally, (iv) add the properties to the existing properties of the feature and filter out the ones which did not intersect. Then export the feature collection as csv:

``````// Map over the feature collection of points so we
// can inspect every single elements intersection and property
var ccs = points.map(function(feat){
feat = ee.Feature(feat);
var point = feat.geometry();
// now map over the polygons
var mappedPolys = polygons.map(function(poly){
var intersects = poly.intersects(point, ee.ErrorMargin(1));
// check if the point intersected with the polygon
var property = ee.String(ee.Algorithms.If(intersects, poly.get('system:index'), 'FALSE'));
// set a property to each individual feature
return feat.set('belongsTo',  ee.String('poly').cat(property))
.set('pointID', ee.String('id').cat(ee.String(feat.get('system:index'))));
});
// return the point with the polygon they intersect with or a property called 'polyFALSE'
return mappedPolys;
})
// Flatten the collection of collections and filter out the 'polyFALSE' properties
.flatten().filter(ee.Filter.neq('belongsTo', 'polyFALSE'));
``````