Skip to content

Commit

Permalink
Merge pull request #675 from Turfjs/turf-inside
Browse files Browse the repository at this point in the history
@turf/inside performance increase
  • Loading branch information
DenisCarriere committed Apr 17, 2017
2 parents 999c88a + 4391cb3 commit d6a3fa3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/turf-inside/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions packages/turf-inside/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
}

0 comments on commit d6a3fa3

Please sign in to comment.