Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@turf/inside performance increase #675

Merged
merged 1 commit into from
Apr 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
}