3

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.

https://code.earthengine.google.com/f7b34e63c5e9ad70f96e1ad91620d48a

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));

Map.addLayer(cc1,{},'cc1');
Map.addLayer(cc2,{},'cc2');

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

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'));

link code

2
  • Thank you Kuik. This can get messy if there many polygons. Is there a way to make this more general, so that it is applicable to a large number of polygons? Each polygon would have an ID and the resulting table should have a variable called "belongs to" and the values would correspond to the polygons IDs.
    – Raimundo
    Apr 10 '19 at 19:07
  • 1
    I edited the answer. See the new link that this would also work for many more polygons/points. And is uses the system property index so you don't have to set id's yourself. If you have already named those points/polygons, then use the name of that property instead of 'system:index'.
    – Kuik
    Apr 11 '19 at 8:28

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.