turf-within

Returns a feature collection of points representing all points that fall withing a collection of polygons.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>turf-within</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://rawgit.com/Turfjs/turf/master/turf.min.js'></script>
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.0.1/mapbox.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.0.1/mapbox.css' rel='stylesheet' />
<style>
  body { margin:0; padding:0; }
  #map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-draw/v0.2.2/leaflet.draw.js'></script>
<script>
L.mapbox.accessToken = 'pk.eyJ1IjoianZyb3Vzc2VhdSIsImEiOiJYYUNlcVRZIn0.lp0867Jn5ynlj72kMwICSA';
var poly_fc;
var within;

var polygon = turf.polygon([
    [
        [-97.5, 35.460], [-97.5, 35.468], [-97.52, 35.468], [-97.52, 35.460]
    ]
], {
    "fill": "#6BC65F",
    "stroke": "#6BC65F",
    "stroke-width": 5
});

var points = [
    turf.point(-97.502754, 35.463455, {
        "marker-color": "#6BC65F"
    }),
    turf.point(-97.508269, 35.463245, {
        "marker-color": "#6BC65F"
    }),
    turf.point(-97.516809, 35.465779, {
        "marker-color": "#6BC65F"
    }),
    turf.point(-97.515372, 35.467072, {
        "marker-color": "#6BC65F"
    }),
    turf.point(-97.509363, 35.463053, {
        "marker-color": "#6BC65F"
    }),
    turf.point(-97.511123, 35.466601, {
        "marker-color": "#6BC65F"
    }),
    turf.point(-97.522259, 35.469100, {
        "marker-color": "#6BC65F"
    }),
    turf.point(-97.518547, 35.469327, {
        "marker-color": "#6BC65F"
    }),
    turf.point(-97.519706, 35.469659, {
        "marker-color": "#6BC65F"
    }),
    turf.point(-97.517839, 35.466998, {
        "marker-color": "#6BC65F"
    }),
    turf.point(-97.508678, 35.464942, {
        "marker-color": "#6BC65F"
    }),
    turf.point(-97.514914, 35.463453, {
        "marker-color": "#6BC65F"
    })
];

var point_fc = turf.featurecollection(points);


var map = L.mapbox.map('map', 'jvrousseau.h4h90e5o')
    .setView([35.463453, -97.508014], 15);

within_fc = L.mapbox.featureLayer().addTo(map);

within_poly = L.mapbox.featureLayer().setGeoJSON(polygon).addTo(map);

within_poly.eachLayer(function(layer) {
    layer.editing.enable();
    updateWithinLayer(layer);
    layer.on('edit', function(e) {
        updateWithinLayer(e.target);
    });
});

function updateWithinLayer(layer) {
    poly_fc = turf.featurecollection([layer.toGeoJSON()]);
    within = turf.within(point_fc, poly_fc);
    within_fc.setGeoJSON(within);
}
</script>