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 2 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 Benchmark = require('benchmark');
const load = require('load-json-file');
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));
Copy link
Member

Choose a reason for hiding this comment

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

A new benchmark technique we've been adding is the Single process results by adding console.time & console.timeEnd before the suite.add.

Example

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;
207 changes: 207 additions & 0 deletions packages/turf-boolean-disjoint/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
var inside = require('@turf/inside');
var invariant = require('@turf/invariant');
var lineIntersect = require('@turf/line-intersect');
var polyToLinestring = require('@turf/polygon-to-linestring');
var getGeom = invariant.getGeom;
var getCoords = invariant.getCoords;
var getGeomType = invariant.getGeomType;

/**
* 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 type1 = getGeomType(feature1);
var type2 = getGeomType(feature2);
var geom1 = getGeom(feature1);
var geom2 = getGeom(feature2);
var coords1 = getCoords(feature1);
var coords2 = getCoords(feature2);

switch (type1) {
case 'Point':
switch (type2) {
case 'Point':
return !compareCoords(coords1, coords2);
case 'MultiPoint':
return !isPointInMultiPoint(geom2, geom1);
case 'LineString':
return !isPointOnLine(geom2, geom1);
case 'Polygon':
return !inside(geom1, geom2);
}
break;
case 'MultiPoint':
switch (type2) {
case 'Point':
return !isPointInMultiPoint(coords1, coords2);
case 'MultiPoint':
return !isMultiPointInMultiPoint(geom1, geom2);
case 'LineString':
return !isMultiPointOnLine(geom2, geom1);
case 'Polygon':
return !isMultiPointInPoly(geom2, geom1);
}
break;
case 'LineString':
switch (type2) {
case 'Point':
return !isPointOnLine(geom1, geom2);
case 'MultiPoint':
return !isMultiPointOnLine(geom1, geom2);
case 'LineString':
return !isLineOnLine(geom1, geom2);
case 'Polygon':
return !isLineInPoly(geom2, geom1);
}
break;
case 'Polygon':
switch (type2) {
case 'Point':
return !inside(geom2, geom1);
case 'MultiPoint':
return !isMultiPointInPoly(geom1, geom2);
case 'LineString':
return !isLineInPoly(geom1, geom2);
case 'Polygon':
return !isPolyInPoly(geom2, geom1);
}
break;
}
};

function isPointInMultiPoint(multiPoint, point) {
for (var i = 0; i < multiPoint.coordinates.length; i++) {
if (compareCoords(multiPoint.coordinates[i], point.coordinates)) {
return true;
}
}
return false;
}

function isMultiPointInMultiPoint(multiPoint1, multiPoint2) {
for (var i = 0; i < multiPoint2.coordinates.length; i++) {
for (var i2 = 0; i2 < multiPoint1.coordinates.length; i2++) {
if (compareCoords(multiPoint2.coordinates[i], multiPoint1.coordinates[i2])) {
return true;
}
}
}
return false;
}

// 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 isMultiPointOnLine(lineString, multiPoint) {
for (var i = 0; i < multiPoint.coordinates.length; i++) {
for (var i2 = 0; i2 < lineString.coordinates.length - 1; i2++) {
if (isPointOnLineSegment(lineString.coordinates[i2], lineString.coordinates[i2 + 1], multiPoint.coordinates[i])) {
return true;
}
}
}
return false;
}

function isMultiPointInPoly(polygon, multiPoint) {
for (var i = 0; i < multiPoint.coordinates.length; i++) {
if (inside(multiPoint.coordinates[i], polygon)) {
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)) {
return dxl > 0 ? LineSegmentStart[0] <= Point[0] && Point[0] <= LineSegmentEnd[0] : LineSegmentEnd[0] <= Point[0] && Point[0] <= LineSegmentStart[0];
} else {
return dyl > 0 ? LineSegmentStart[1] <= Point[1] && Point[1] <= LineSegmentEnd[1] : 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];
}
Loading