Skip to content

Instantly share code, notes, and snippets.

@morganherlocker
Created July 11, 2016 17:36
Show Gist options
  • Save morganherlocker/1805f5a4b3ed8b2375281e525238c210 to your computer and use it in GitHub Desktop.
Save morganherlocker/1805f5a4b3ed8b2375281e525238c210 to your computer and use it in GitHub Desktop.
var pts = // my point featurecollection
var polys = // my poly featurecollection
var tree = rbush()
polys.features.forEach(function(poly, i){
var bbox = turf.bbox(poly)
var idx = {
minX: bbox[0],
minY: bbox[1],
maxX: bbox[2],
maxY: bbox[3],
id: i // store polygon's bbox with its index in the featurecollection
};
})
pts.features = pts.features.filter(function(pt){
var isInside = false
// find polygons with bboxes enclosing the point
var collisions = tree.search({
minX: pt.geometry.coordinates[0],
minY: pt.geometry.coordinates[1],
maxX: pt.geometry.coordinates[0],
maxY: pt.geometry.coordinates[1]
})
collisions
.map(function(collision){
// lookup polygon by the index of the feature
return polys.features[collision.id]
})
.forEach(function(poly){
// flip the status if point is inside polygon's hull
if(turf.inside(pt, poly)) isInside = true
})
return isInside
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment