diff --git a/packages/turf-inside/bench.js b/packages/turf-inside/bench.js index 88c015adbc..cd028a793f 100644 --- a/packages/turf-inside/bench.js +++ b/packages/turf-inside/bench.js @@ -14,9 +14,9 @@ var multiPolyHole = JSON.parse(fs.readFileSync(__dirname + '/test/in/multipoly-w /** * Benchmark Results * - * simple x 3,185,225 ops/sec ±0.99% (90 runs sampled) - * multiPolyHole - inside x 1,234,815 ops/sec ±0.90% (90 runs sampled) - * multiPolyHole - outside x 1,644,657 ops/sec ±1.26% (88 runs sampled) + * simple x 3,219,331 ops/sec ±1.14% (91 runs sampled) + * multiPolyHole - inside x 1,171,486 ops/sec ±1.10% (90 runs sampled) + * multiPolyHole - outside x 7,697,033 ops/sec ±0.89% (89 runs sampled) */ var suite = new Benchmark.Suite('turf-inside'); suite diff --git a/packages/turf-inside/index.js b/packages/turf-inside/index.js index 439f1e0101..0d1e83aa78 100644 --- a/packages/turf-inside/index.js +++ b/packages/turf-inside/index.js @@ -38,6 +38,10 @@ module.exports = function (point, polygon) { var pt = getCoord(point); var polys = getCoords(polygon); var type = (polygon.geometry) ? polygon.geometry.type : polygon.type; + var bbox = polygon.bbox; + + // Quick elimination if point is not inside bbox + if (bbox && inBBox(pt, bbox) === false) return false; // normalize to multipolygon if (type === 'Polygon') polys = [polys]; @@ -85,3 +89,17 @@ function inRing(pt, ring, ignoreBoundary) { } return isInside; } + +/** + * inBBox + * + * @param {[number, number]} pt point [x,y] + * @param {[number, number, number, number]} bbox BBox [west, south, east, north] + * @returns {boolean} true/false if point is inside BBox + */ +function inBBox(pt, bbox) { + return bbox[0] <= pt[0] && + bbox[1] <= pt[1] && + bbox[2] >= pt[0] && + bbox[3] >= pt[1]; +}