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

New module @turf/boolean-disjoint #805

Merged
merged 8 commits into from
Jun 20, 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
20 changes: 20 additions & 0 deletions packages/turf-boolean-disjoint/LICENSE
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.
60 changes: 60 additions & 0 deletions packages/turf-boolean-disjoint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# @turf/boolean-disjoint

# booleanDisjoint

Boolean-disjoint returns (TRUE) if the intersection of the two geometries is an empty set.

**Parameters**

- `feature1` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** GeoJSON Feature or Geometry
- `feature2` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** GeoJSON Feature or Geometry

**Examples**

```javascript
const point = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [2, 2]
}
}
const line = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [[1, 1], [1, 2], [1, 3], [1, 4]]
}
}
turf.booleanDisjoint(line, point);
//=true
```

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-disjoint
```

Or install the Turf module that includes it as a function:

```sh
$ npm install @turf/turf
```
67 changes: 67 additions & 0 deletions packages/turf-boolean-disjoint/bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const path = require('path');
const glob = require('glob');
const load = require('load-json-file');
const Benchmark = require('benchmark');
const disjoint = require('./');

/**
* Benchmark Results
*
* LineIsNotDisjointFromLine: 3.464ms
* LineIsNotDisjointFromPolygon: 1.199ms
* MultiPointNotDisjointFromLine: 0.213ms
* MultiPointNotDisjointFromMultipoint: 0.193ms
* MultiPointNotDisjointFromPoly: 1.468ms
* PointIsNotDisjointFromLineString: 0.184ms
* PointNotDisjointFromInternalPoly: 0.651ms
* PointNotDisjointFromMultipoint: 0.165ms
* PointNotDisjointFromPoint: 0.020ms
* PointNotDisjointFromPoly: 0.025ms
* PolyIsNotDisjointFromPoly: 0.209ms
* LineIsDisjointFromLine: 0.280ms
* LineIsDisjointFromPolygon: 0.230ms
* MultiPointDisjointFromLine: 0.046ms
* MultiPointDisjointFromMultipoint: 0.015ms
* MultiPointIsDisjointFromPoly: 0.034ms
* PointDisjointFromMultipoint: 0.155ms
* PointDisjointFromPoint: 0.021ms
* PointIsDisjointFromLinestring: 0.962ms
* PointIsDisjointFromPoly: 0.131ms
* PolyIsDisjointFromPoly: 0.080ms
* LineIsNotDisjointFromLine x 50,655 ops/sec ±3.66% (63 runs sampled)
* LineIsNotDisjointFromPolygon x 49,947 ops/sec ±2.79% (42 runs sampled)
* MultiPointNotDisjointFromLine x 7,431,677 ops/sec ±1.24% (85 runs sampled)
* MultiPointNotDisjointFromMultipoint x 7,399,444 ops/sec ±0.99% (87 runs sampled)
* MultiPointNotDisjointFromPoly x 2,298,298 ops/sec ±1.02% (88 runs sampled)
* PointIsNotDisjointFromLineString x 10,068,082 ops/sec ±1.02% (89 runs sampled)
* PointNotDisjointFromInternalPoly x 2,136,512 ops/sec ±1.24% (87 runs sampled)
* PointNotDisjointFromMultipoint x 10,712,566 ops/sec ±0.98% (88 runs sampled)
* PointNotDisjointFromPoint x 16,663,034 ops/sec ±0.98% (88 runs sampled)
* PointNotDisjointFromPoly x 2,509,098 ops/sec ±1.06% (87 runs sampled)
* PolyIsNotDisjointFromPoly x 2,078,010 ops/sec ±1.60% (85 runs sampled)
* LineIsDisjointFromLine x 62,985 ops/sec ±4.13% (64 runs sampled)
* LineIsDisjointFromPolygon x 58,937 ops/sec ±2.84% (62 runs sampled)
* MultiPointDisjointFromLine x 4,634,718 ops/sec ±1.94% (85 runs sampled)
* MultiPointDisjointFromMultipoint x 6,602,132 ops/sec ±3.76% (81 runs sampled)
* MultiPointIsDisjointFromPoly x 1,203,406 ops/sec ±1.01% (87 runs sampled)
* PointDisjointFromMultipoint x 11,373,917 ops/sec ±1.33% (87 runs sampled)
* PointDisjointFromPoint x 18,149,666 ops/sec ±1.10% (85 runs sampled)
* PointIsDisjointFromLinestring x 7,572,815 ops/sec ±0.97% (89 runs sampled)
* PointIsDisjointFromPoly x 2,217,658 ops/sec ±1.04% (83 runs sampled)
* PolyIsDisjointFromPoly x 551,254 ops/sec ±1.18% (90 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);
disjoint(feature1, feature2);
console.timeEnd(name);
suite.add(name, () => disjoint(feature1, feature2));
});

suite
.on('cycle', e => console.log(String(e.target)))
.on('complete', () => {})
.run();
10 changes: 10 additions & 0 deletions packages/turf-boolean-disjoint/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="geojson" />

type Feature = GeoJSON.Feature<any> | GeoJSON.GeometryObject;

/**
* http://turfjs.org/docs/#boolean-disjoint
*/
declare function booleanDisjoint(feature1: Feature, feature2: Feature): boolean;
declare namespace booleanDisjoint { }
export = booleanDisjoint;
165 changes: 165 additions & 0 deletions packages/turf-boolean-disjoint/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
var inside = require('@turf/inside');
var flattenEach = require('@turf/meta').flattenEach;
var lineIntersect = require('@turf/line-intersect');
var polyToLinestring = require('@turf/polygon-to-linestring');

/**
* Boolean-disjoint returns (TRUE) if the intersection of the two geometries is an empty set.
*
* @name booleanDisjoint
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature or Geometry
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature or Geometry
* @returns {Boolean} true/false
* @example
* const point = {
* "type": "Feature",
* "properties": {},
* "geometry": {
* "type": "Point",
* "coordinates": [2, 2]
* }
* }
* const line = {
* "type": "Feature",
* "properties": {},
* "geometry": {
* "type": "LineString",
* "coordinates": [[1, 1], [1, 2], [1, 3], [1, 4]]
* }
* }
* turf.booleanDisjoint(line, point);
* //=true
*/
module.exports = function (feature1, feature2) {
var boolean;
flattenEach(feature1, function (flatten1) {
flattenEach(feature2, function (flatten2) {
if (boolean === false) return false;
boolean = disjoint(flatten1.geometry, flatten2.geometry);
});
});
return boolean;
};

/**
* Disjoint operation for simple Geometries (Point/LineString/Polygon)
*
* @private
* @param {Geometry<any>} geom1 GeoJSON Geometry
* @param {Geometry<any>} geom2 GeoJSON Geometry
* @returns {Boolean} true/false
*/
function disjoint(geom1, geom2) {
switch (geom1.type) {
case 'Point':
switch (geom2.type) {
case 'Point':
return !compareCoords(geom1.coordinates, geom2.coordinates);
case 'LineString':
return !isPointOnLine(geom2, geom1);
case 'Polygon':
return !inside(geom1, geom2);
}
/* istanbul ignore next */
break;
case 'LineString':
switch (geom2.type) {
case 'Point':
return !isPointOnLine(geom1, geom2);
case 'LineString':
return !isLineOnLine(geom1, geom2);
case 'Polygon':
return !isLineInPoly(geom2, geom1);
}
/* istanbul ignore next */
break;
case 'Polygon':
switch (geom2.type) {
case 'Point':
return !inside(geom2, geom1);
case 'LineString':
return !isLineInPoly(geom1, geom2);
case 'Polygon':
return !isPolyInPoly(geom2, geom1);
}
}
}

// http://stackoverflow.com/a/11908158/1979085
function isPointOnLine(lineString, point) {
for (var i = 0; i < lineString.coordinates.length - 1; i++) {
if (isPointOnLineSegment(lineString.coordinates[i], lineString.coordinates[i + 1], point.coordinates)) {
return true;
}
}
return false;
}

function isLineOnLine(lineString1, lineString2) {
var doLinesIntersect = lineIntersect(lineString1, lineString2);
if (doLinesIntersect.features.length > 0) {
return true;
}
return false;
}

function isLineInPoly(polygon, lineString) {
var doLinesIntersect = lineIntersect(lineString, polyToLinestring(polygon));
if (doLinesIntersect.features.length > 0) {
return true;
}
return false;
}

/**
* Is Polygon (geom1) in Polygon (geom2)
* Only takes into account outer rings
* See http://stackoverflow.com/a/4833823/1979085
*
* @private
* @param {Geometry|Feature<Polygon>} feature1 Polygon1
* @param {Geometry|Feature<Polygon>} feature2 Polygon2
* @returns {Boolean} true/false
*/
function isPolyInPoly(feature1, feature2) {
for (var i = 0; i < feature1.coordinates[0].length; i++) {
if (inside(feature1.coordinates[0][i], feature2)) {
return true;
}
}
return false;
}

function isPointOnLineSegment(LineSegmentStart, LineSegmentEnd, Point) {
var dxc = Point[0] - LineSegmentStart[0];
var dyc = Point[1] - LineSegmentStart[1];
var dxl = LineSegmentEnd[0] - LineSegmentStart[0];
var dyl = LineSegmentEnd[1] - LineSegmentStart[1];
var cross = dxc * dyl - dyc * dxl;
if (cross !== 0) {
return false;
}
if (Math.abs(dxl) >= Math.abs(dyl)) {
if (dxl > 0) {
return LineSegmentStart[0] <= Point[0] && Point[0] <= LineSegmentEnd[0];
} else {
return LineSegmentEnd[0] <= Point[0] && Point[0] <= LineSegmentStart[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rowanwins Any thoughts how to hit these lines (146 & 151) with Test Fixtures?

$ tap test.js --coverage
test.js ............................................. 74/74
total ............................................... 74/74

  74 passing (4s)

  ok
----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |    96.36 |       85 |      100 |     96.3 |                |
 index.js |    96.36 |       85 |      100 |     96.3 |        146,151 |
----------|----------|----------|----------|----------|----------------|

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put it into a seperate module and test that module 😀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this source code come from?

Lack of comments is hard to troubleshoot.

}
} else if (dyl > 0) {
return LineSegmentStart[1] <= Point[1] && Point[1] <= LineSegmentEnd[1];
} else {
return LineSegmentEnd[1] <= Point[1] && Point[1] <= LineSegmentStart[1];
}
}

/**
* compareCoords
*
* @private
* @param {[number, number]} pair1 point [x,y]
* @param {[number, number]} pair2 point [x,y]
* @returns {boolean} true/false if coord pairs match
*/
function compareCoords(pair1, pair2) {
return pair1[0] === pair2[0] && pair1[1] === pair2[1];
}
48 changes: 48 additions & 0 deletions packages/turf-boolean-disjoint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@turf/boolean-disjoint",
"version": "4.0.0",
"description": "turf boolean-disjoint 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",
"disjoint",
"boolean",
"de-9im"
],
"author": "Turf Authors",
"contributors": [
"Rowan Winsemius <@rowanwins>",
"Denis Carriere <@DenisCarriere>"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/Turfjs/turf/issues"
},
"homepage": "https://github.com/Turfjs/turf",
"devDependencies": {
"@turf/helpers": "^4.3.0",
"benchmark": "^2.1.4",
"boolean-shapely": "^0.1.2",
"load-json-file": "^2.0.0",
"tape": "^4.6.3"
},
"dependencies": {
"@turf/inside": "^4.4.0",
"@turf/line-intersect": "^4.4.0",
"@turf/meta": "^4.4.0",
"@turf/polygon-to-linestring": "^4.4.0"
}
}
Loading