-
Notifications
You must be signed in to change notification settings - Fork 934
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
New module @turf/boolean-overlaps
#856
Changes from 7 commits
144b080
117aac9
ac4cb4a
f1cad4d
164ae37
309897e
0c426ed
c7a9a18
981a1d0
274c6d8
f292d27
0cf8731
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 TurfJS | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# @turf/boolean-overlaps | ||
|
||
# booleanOverlaps | ||
|
||
Takes two features and returns true or false whether or not they overlap, i.e. whether any pair of edges on the two polygons intersect. If there are any edge intersections, the polygons overlap. | ||
|
||
**Parameters** | ||
|
||
- `feature1` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<[LineString](http://geojson.org/geojson-spec.html#linestring)|[MultiLineString](http://geojson.org/geojson-spec.html#multilinestring)|[Polygon](http://geojson.org/geojson-spec.html#polygon)|[MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon)>)** GeoJSON Feature or Geometry | ||
- `feature2` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<[LineString](http://geojson.org/geojson-spec.html#linestring)|[MultiLineString](http://geojson.org/geojson-spec.html#multilinestring)|[Polygon](http://geojson.org/geojson-spec.html#polygon)|[MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon)>)** GeoJSON Feature or Geometry | ||
|
||
**Examples** | ||
|
||
```javascript | ||
var poly1 = turf.polygon([[[18.70,-34.19],[18.93,-34.19],[18.93,-34],[18.70,-34],[18.70,-34.19]]]); | ||
var poly2 = turf.polygon([[[18.52,-34.36],[18.79,-34.36],[18.79,-34.10],[18.52,-34.10],[1852,-34.36]]]); | ||
var line = turf.lineString([[18.62,-34.39],[18.87,-34.21]]); | ||
|
||
turf.booleanOverlaps(poly1, poly2) | ||
//=true | ||
turf.booleanOverlaps(poly2, line) | ||
//=false | ||
``` | ||
|
||
Returns **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true/false | ||
|
||
<!-- This file is automatically generated. Please don't edit it directly: | ||
if you find an error, edit the source file (likely index.js), and re-run | ||
./scripts/generate-readmes in the turf project. --> | ||
|
||
--- | ||
|
||
This module is part of the [Turfjs project](http://turfjs.org/), an open source | ||
module collection dedicated to geographic algorithms. It is maintained in the | ||
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create | ||
PRs and issues. | ||
|
||
### Installation | ||
|
||
Install this module individually: | ||
|
||
```sh | ||
$ npm install @turf/boolean-overlaps | ||
``` | ||
|
||
Or install the Turf module that includes it as a function: | ||
|
||
```sh | ||
$ npm install @turf/turf | ||
``` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const path = require('path'); | ||
const glob = require('glob'); | ||
const load = require('load-json-file'); | ||
const overlaps = require('./'); | ||
const Benchmark = require('benchmark'); | ||
|
||
/** | ||
* Benchmark Results | ||
* lines: 0.824ms | ||
* multiline-polygon: 0.152ms | ||
* multipolygon-line: 0.099ms | ||
* multipolygon-multiline: 1.879ms | ||
* multipolygon-polygon: 0.322ms | ||
* polygon-line: 0.044ms | ||
* polygon-with-hole-polygon: 0.088ms | ||
* polygons: 0.098ms | ||
* geometries: 0.067ms | ||
* lines: 0.904ms | ||
* lines2: 0.070ms | ||
* multiline-polygon: 0.052ms | ||
* multipolygon-line: 0.025ms | ||
* multipolygon-multiline: 0.083ms | ||
* multipolygon-polygon: 0.037ms | ||
* polygon-line: 0.122ms | ||
* polygon-with-hole-line: 0.076ms | ||
* polygon-with-hole-polygon: 0.116ms | ||
* polygons: 0.068ms | ||
* polygons2: 0.050ms | ||
* lines x 152,592 ops/sec ±3.12% (72 runs sampled) | ||
* multiline-polygon x 215,290 ops/sec ±6.04% (64 runs sampled) | ||
* multipolygon-line x 185,348 ops/sec ±3.75% (76 runs sampled) | ||
* multipolygon-multiline x 122,911 ops/sec ±2.83% (77 runs sampled) | ||
* multipolygon-polygon x 66,818 ops/sec ±2.37% (79 runs sampled) | ||
* polygon-line x 475,840 ops/sec ±1.66% (65 runs sampled) | ||
* polygon-with-hole-polygon x 85,510 ops/sec ±2.26% (77 runs sampled) | ||
* polygons x 125,798 ops/sec ±3.29% (80 runs sampled) | ||
* geometries x 470,264 ops/sec ±1.65% (79 runs sampled) | ||
* lines x 211,284 ops/sec ±3.97% (72 runs sampled) | ||
* lines2 x 615,171 ops/sec ±1.50% (81 runs sampled) | ||
* multiline-polygon x 292,558 ops/sec ±1.98% (80 runs sampled) | ||
* multipolygon-line x 719,546 ops/sec ±1.75% (77 runs sampled) | ||
* multipolygon-multiline x 152,743 ops/sec ±2.46% (80 runs sampled) | ||
* multipolygon-polygon x 535,466 ops/sec ±2.57% (77 runs sampled) | ||
* polygon-line x 446,153 ops/sec ±4.79% (77 runs sampled) | ||
* polygon-with-hole-line x 275,506 ops/sec ±2.14% (80 runs sampled) | ||
* polygon-with-hole-polygon x 134,078 ops/sec ±1.76% (77 runs sampled) | ||
* polygons x 233,564 ops/sec ±2.38% (79 runs sampled) | ||
* polygons2 x 366,677 ops/sec ±2.11% (79 runs sampled) | ||
*/ | ||
const suite = new Benchmark.Suite('turf-boolean-disjoint'); | ||
glob.sync(path.join(__dirname, 'test', '**', '*.geojson')).forEach(filepath => { | ||
const {name} = path.parse(filepath); | ||
const geojson = load.sync(filepath); | ||
const [feature1, feature2] = geojson.features; | ||
console.time(name); | ||
overlaps(feature1, feature2); | ||
console.timeEnd(name); | ||
suite.add(name, () => overlaps(feature1, feature2)); | ||
}); | ||
|
||
suite | ||
.on('cycle', e => console.log(String(e.target))) | ||
.on('complete', () => {}) | ||
.run(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/// <reference types="geojson" /> | ||
|
||
type LineString = GeoJSON.LineString | ||
type Polygon = GeoJSON.Polygon | ||
type MultiLineString = GeoJSON.MultiLineString | ||
type MultiPolygon = GeoJSON.MultiPolygon | ||
|
||
type Feature = GeoJSON.Feature<LineString | MultiLineString | Polygon | MultiPolygon> | ||
| LineString | MultiLineString | Polygon | MultiPolygon | ||
|
||
/** | ||
* http://turfjs.org/docs/#boolean-overlaps | ||
*/ | ||
declare function overlaps(feature1: Feature, feature2: Feature): boolean; | ||
declare namespace overlaps { } | ||
export = overlaps; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
var invariant = require('@turf/invariant'); | ||
var clockwise = require('@turf/boolean-clockwise'); | ||
var getGeomType = invariant.getGeomType; | ||
|
||
/** | ||
* Takes two features and returns true or false whether or not they overlap, i.e. whether any pair of edges | ||
* on the two polygons intersect. If there are any edge intersections, the polygons overlap. | ||
* | ||
* @name booleanOverlaps | ||
* @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} feature1 input | ||
* @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} feature2 input | ||
* @returns {boolean} true/false | ||
* @example | ||
* var poly1 = turf.polygon([[[18.70,-34.19],[18.93,-34.19],[18.93,-34],[18.70,-34],[18.70,-34.19]]]); | ||
* var poly2 = turf.polygon([[[18.52,-34.36],[18.79,-34.36],[18.79,-34.10],[18.52,-34.10],[18.52,-34.36]]]); | ||
* var line = turf.lineString([[18.62,-34.39],[18.87,-34.21]]); | ||
* | ||
* turf.booleanOverlaps(poly1, poly2) | ||
* //=true | ||
* turf.booleanOverlaps(poly2, line) | ||
* //=false | ||
*/ | ||
module.exports = function (feature1, feature2) { | ||
// validation | ||
if (getGeomType(feature1) === 'Point') throw new Error('feature1 Point geometry not supported'); | ||
if (getGeomType(feature2) === 'Point') throw new Error('feature2 Point geometry not supported'); | ||
|
||
var coords1 = getCoordinatesArr(feature1), | ||
coords2 = getCoordinatesArr(feature2); | ||
|
||
// Since we don't care about the overlap amount, or it's geometry, but rather just whether overlap | ||
// occurs, polygon overlap can most simply be expressed by testing whether any pair of edges on the two polygons | ||
// intersect. If there are any edge intersections, the polygons overlap. | ||
|
||
// This looks completely stupid ridiculous to have so many nested loops, but it supports | ||
// multipolygons nicely. In the case of polygons or linestrings, the outer loops are only one iteration. | ||
return coords1.some(function (rings1) { | ||
return coords2.some(function (rings2) { | ||
return rings1.some(function (ring1) { | ||
return rings2.some(function (ring2) { | ||
return doLinesIntersect(ring1, ring2); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* Returns if the two lineStrings intersect | ||
* | ||
* @private | ||
* @param {Array<Array<number|Array<number>>>} ring1 array of a LineString coordinates | ||
* @param {Array<Array<number|Array<number>>>} ring2 array of a LineString coordinates | ||
* @returns {boolean} lines intersect | ||
*/ | ||
function doLinesIntersect(ring1, ring2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should build a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, You can use it right now (after PR merged) by using |
||
for (var p1_ind = 0; p1_ind < (ring1.length - 1); p1_ind++) { | ||
var p1_line = [ring1[p1_ind], ring1[p1_ind + 1]]; | ||
for (var p2_ind = 0; p2_ind < (ring2.length - 1); p2_ind++) { | ||
var p2_line = [ring2[p2_ind], ring2[p2_ind + 1]]; | ||
|
||
if (doSegmentsIntersect(p1_line, p2_line)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Returns if the two segments intersect | ||
* | ||
* @private | ||
* @param {Array<Array<number>>} line1 array of a segment coordinates | ||
* @param {Array<Array<number>>} line2 array of a segment coordinates | ||
* @returns {boolean} segments intersect | ||
*/ | ||
function doSegmentsIntersect(line1, line2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't this be replaced with |
||
var p1 = line1[0], | ||
p2 = line1[1], | ||
p3 = line2[0], | ||
p4 = line2[1]; | ||
|
||
return (clockwise([p1, p3, p4, p1]) !== clockwise([p2, p3, p4, p2])) && (clockwise([p1, p2, p3, p1]) !== clockwise([p1, p2, p4, p1])); | ||
} | ||
|
||
/** | ||
* Takes a feature and returns nested Arrays containing the feature coordinates | ||
* | ||
* @private | ||
* @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} feature input | ||
* @returns {Array<Array<Array<number>>>} array of array of array of coordinates | ||
*/ | ||
function getCoordinatesArr(feature) { | ||
switch (feature.geometry.type) { | ||
case 'LineString': | ||
return [[feature.geometry.coordinates]]; | ||
case 'Polygon': | ||
case 'MultiLineString': | ||
return [feature.geometry.coordinates]; | ||
case 'MultiPolygon': | ||
return feature.geometry.coordinates; | ||
} | ||
return [[[]]]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "@turf/boolean-overlaps", | ||
"version": "4.5.0", | ||
"description": "turf boolean-overlaps module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"scripts": { | ||
"test": "node test.js", | ||
"bench": "node bench.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/Turfjs/turf.git" | ||
}, | ||
"keywords": [ | ||
"turf", | ||
"gis", | ||
"boolean", | ||
"overlap" | ||
], | ||
"author": "Turf Authors", | ||
"contributors": [ | ||
"Tim Channell <@tcql>", | ||
"Stefano Borghi <@stebogit>" | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Turfjs/turf/issues" | ||
}, | ||
"homepage": "https://github.com/Turfjs/turf", | ||
"devDependencies": { | ||
"@turf/helpers": "^4.5.2", | ||
"benchmark": "^2.1.4", | ||
"glob": "^7.1.1", | ||
"load-json-file": "^2.0.0", | ||
"tape": "^4.6.3" | ||
}, | ||
"dependencies": { | ||
"@turf/boolean-clockwise": "^4.5.2", | ||
"@turf/invariant": "^4.5.2" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const glob = require('glob'); | ||
const path = require('path'); | ||
const test = require('tape'); | ||
const load = require('load-json-file'); | ||
const {point, lineString} = require('@turf/helpers'); | ||
const overlaps = require('./'); | ||
|
||
test('turf-boolean-overlaps', t => { | ||
// True Fixtures | ||
glob.sync(path.join(__dirname, 'test', 'true', '**', '*.geojson')).forEach(filepath => { | ||
const {name} = path.parse(filepath); | ||
const geojson = load.sync(filepath); | ||
const [feature1, feature2] = geojson.features; | ||
const result = overlaps(feature1, feature2); | ||
|
||
if (process.env.SHAPELY) shapely.contains(feature1, feature2).then(result => t.true(result, '[true] shapely - ' + name)); | ||
t.true(result, '[true] ' + name); | ||
}); | ||
// False Fixtures | ||
glob.sync(path.join(__dirname, 'test', 'false', '**', '*.geojson')).forEach(filepath => { | ||
const {name} = path.parse(filepath); | ||
const geojson = load.sync(filepath); | ||
const [feature1, feature2] = geojson.features; | ||
const result = overlaps(feature1, feature2); | ||
|
||
if (process.env.SHAPELY) shapely.contains(feature1, feature2).then(result => t.false(result, '[false] shapely - ' + name)); | ||
t.false(result, '[false] ' + name); | ||
}); | ||
t.end(); | ||
}); | ||
|
||
test('turf-boolean-overlaps -- throws', t => { | ||
const pt = point([9, 50]); | ||
const line = lineString([[7, 50], [8, 50], [9, 50]]); | ||
|
||
t.throws(() => overlaps(null, line), '<geojson> is required'); | ||
t.throws(() => overlaps(line, null), '<geojson> is required'); | ||
t.throws(() => overlaps(pt, line), 'feature1 Point geometry not supported'); | ||
t.throws(() => overlaps(line, pt), 'feature2 Point geometry not supported'); | ||
t.throws(() => overlaps({"feature": "non GeoJSON"}, line), '<geojson> must be a Feature or Geometry Object'); | ||
t.throws(() => overlaps(line, {"feature": "non GeoJSON"}), '<geojson> must be a Feature or Geometry Object'); | ||
|
||
t.end(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have
flattenEach
which solves this "stupid ridiculous" pattern 😝 (pulling directly from comment above).