-
Notifications
You must be signed in to change notification settings - Fork 955
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-parallel
#941
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# @turf/boolean-parallel | ||
|
||
# booleanParallel | ||
|
||
Boolean-Parallel returns True if each segment of `line1` is parallel to the correspondent segment of `line2` | ||
|
||
**Parameters** | ||
|
||
- `line1` **([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)>)** GeoJSON Feature or Geometry | ||
- `line2` **([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)>)** GeoJSON Feature or Geometry | ||
|
||
**Examples** | ||
|
||
```javascript | ||
var line1 = turf.lineString([[0, 0], [0, 1]]); | ||
var line2 = turf.lineString([[1, 0], [1, 1]]); | ||
|
||
turf.booleanParallel(line1, line2); | ||
//=true | ||
``` | ||
|
||
Returns **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true/false if the lines are parallel | ||
|
||
<!-- 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-parallel | ||
``` | ||
|
||
Or install the Turf module that includes it as a function: | ||
|
||
```sh | ||
$ npm install @turf/turf | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const path = require('path'); | ||
const glob = require('glob'); | ||
const load = require('load-json-file'); | ||
const Benchmark = require('benchmark'); | ||
const booleanParallel = require('./'); | ||
|
||
/** | ||
* Benchmark Results | ||
* | ||
* line1: 2.578ms | ||
* line2: 0.137ms | ||
* city-line: 0.096ms | ||
* fiji: 0.073ms | ||
* geometry: 0.123ms | ||
* line1: 0.068ms | ||
* line3-reverse: 0.065ms | ||
* line3: 0.429ms | ||
* resolute: 0.077ms | ||
* segment1: 0.203ms | ||
* segment2: 0.087ms | ||
* segment3: 0.698ms | ||
* | ||
* line1 x 171,462 ops/sec ±2.03% (79 runs sampled) | ||
* line2 x 160,366 ops/sec ±6.55% (80 runs sampled) | ||
* city-line x 120,544 ops/sec ±8.47% (73 runs sampled) | ||
* fiji x 101,793 ops/sec ±5.09% (75 runs sampled) | ||
* geometry x 93,106 ops/sec ±7.57% (74 runs sampled) | ||
* line1 x 102,175 ops/sec ±4.95% (80 runs sampled) | ||
* line3-reverse x 129,695 ops/sec ±2.11% (82 runs sampled) | ||
* line3 x 129,860 ops/sec ±2.32% (83 runs sampled) | ||
* resolute x 136,275 ops/sec ±2.89% (79 runs sampled) | ||
* segment1 x 193,214 ops/sec ±4.31% (76 runs sampled) | ||
* segment2 x 205,418 ops/sec ±2.16% (83 runs sampled) | ||
* segment3 x 212,381 ops/sec ±1.79% (83 runs sampled) | ||
*/ | ||
const suite = new Benchmark.Suite('turf-boolean-parallel'); | ||
glob.sync(path.join(__dirname, 'test', '**', '*.geojson')).forEach(filepath => { | ||
const {name} = path.parse(filepath); | ||
const geojson = load.sync(filepath); | ||
const [line1, line2] = geojson.features; | ||
|
||
console.time(name); | ||
booleanParallel(line1, line2); | ||
console.timeEnd(name); | ||
suite.add(name, () => booleanParallel(line1, line2)); | ||
}); | ||
|
||
suite | ||
.on('cycle', e => console.log(String(e.target))) | ||
.on('complete', () => {}) | ||
.run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference types="geojson" /> | ||
|
||
type Geom = GeoJSON.LineString; | ||
type Feature<Geom extends GeoJSON.GeometryObject> = GeoJSON.Feature<Geom>; | ||
|
||
/** | ||
* http://turfjs.org/docs/#booleanparallel | ||
*/ | ||
declare function booleanParallel(line1: Feature<Geom> | Geom, line2: Feature<Geom> | Geom): boolean; | ||
declare namespace booleanParallel { } | ||
export = booleanParallel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
var cleanCoords = require('@turf/clean-coords'); | ||
var lineSegment = require('@turf/line-segment'); | ||
var rhumbBearing = require('@turf/rhumb-bearing'); | ||
var bearingToAngle = require('@turf/helpers').bearingToAngle; | ||
|
||
|
||
/** | ||
* Boolean-Parallel returns True if each segment of `line1` is parallel to the correspondent segment of `line2` | ||
* | ||
* @name booleanParallel | ||
* @param {Geometry|Feature<LineString>} line1 GeoJSON Feature or Geometry | ||
* @param {Geometry|Feature<LineString>} line2 GeoJSON Feature or Geometry | ||
* @returns {Boolean} true/false if the lines are parallel | ||
* @example | ||
* var line1 = turf.lineString([[0, 0], [0, 1]]); | ||
* var line2 = turf.lineString([[1, 0], [1, 1]]); | ||
* | ||
* turf.booleanParallel(line1, line2); | ||
* //=true | ||
*/ | ||
module.exports = function (line1, line2) { | ||
// validation | ||
if (!line1) throw new Error('line1 is required'); | ||
if (!line2) throw new Error('line2 is required'); | ||
var type1 = getType(line1, 'line1'); | ||
if (type1 !== 'LineString') throw new Error('line1 must be a LineString'); | ||
var type2 = getType(line2, 'line2'); | ||
if (type2 !== 'LineString') throw new Error('line2 must be a LineString'); | ||
|
||
var segments1 = lineSegment(cleanCoords(line1)).features; | ||
var segments2 = lineSegment(cleanCoords(line2)).features; | ||
|
||
for (var i = 0; i < segments1.length; i++) { | ||
var segment1 = segments1[i].geometry.coordinates; | ||
if (!segments2[i]) break; | ||
var segment2 = segments2[i].geometry.coordinates; | ||
if (!isParallel(segment1, segment2)) return false; | ||
} | ||
return true; | ||
}; | ||
|
||
|
||
/** | ||
* Compares slopes and return result | ||
* | ||
* @private | ||
* @param {Geometry|Feature<LineString>} segment1 Geometry or Feature | ||
* @param {Geometry|Feature<LineString>} segment2 Geometry or Feature | ||
* @returns {boolean} if slopes are equal | ||
*/ | ||
function isParallel(segment1, segment2) { | ||
var slope1 = bearingToAngle(rhumbBearing(segment1[0], segment1[1])); | ||
var slope2 = bearingToAngle(rhumbBearing(segment2[0], segment2[1])); | ||
return slope1 === slope2; | ||
} | ||
|
||
|
||
/** | ||
* Returns Feature's type | ||
* | ||
* @private | ||
* @param {Geometry|Feature<any>} geojson Geometry or Feature | ||
* @param {string} name of the variable | ||
* @returns {string} Feature's type | ||
*/ | ||
function getType(geojson, name) { | ||
if (geojson.geometry && geojson.geometry.type) return geojson.geometry.type; | ||
if (geojson.type) return geojson.type; // if GeoJSON geometry | ||
throw new Error('Invalid GeoJSON object for ' + name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "@turf/boolean-parallel", | ||
"version": "4.0.0", | ||
"description": "turf boolean-parallel 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", | ||
"parallel", | ||
"boolean", | ||
"boolean-parallel" | ||
], | ||
"author": "Turf Authors", | ||
"contributors": [ | ||
"Stefano Borghi <@stebogit>" | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Turfjs/turf/issues" | ||
}, | ||
"homepage": "https://github.com/Turfjs/turf", | ||
"devDependencies": { | ||
"benchmark": "^2.1.4", | ||
"write-json-file": "^2.2.0", | ||
"load-json-file": "^2.0.0", | ||
"tape": "^4.6.3" | ||
}, | ||
"dependencies": { | ||
"@turf/clean-coords": "4.7.1", | ||
"@turf/helpers": "4.7.1", | ||
"@turf/line-segment": "4.7.1", | ||
"@turf/rhumb-bearing": "4.7.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const glob = require('glob'); | ||
const path = require('path'); | ||
const test = require('tape'); | ||
const load = require('load-json-file'); | ||
const {lineString, polygon} = require('@turf/helpers'); | ||
const booleanParallel = require('./'); | ||
|
||
test('turf-boolean-parallel', t => { | ||
// True Fixtures | ||
glob.sync(path.join(__dirname, 'test', 'true', '**', '*.geojson')).forEach(filepath => { | ||
const {name} = path.parse(filepath); | ||
const geojson = load.sync(filepath); | ||
const [line1, line2] = geojson.features; | ||
const result = booleanParallel(line1, line2); | ||
|
||
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 [line1, line2] = geojson.features; | ||
const result = booleanParallel(line1, line2); | ||
|
||
t.false(result, '[false] ' + name); | ||
}); | ||
t.end(); | ||
}); | ||
|
||
|
||
test('turf-boolean-parallel -- throws', t => { | ||
const line = lineString([[0, 0], [0, 1]]); | ||
const poly = polygon([[[0, 0], [0, 1], [1, 1], [0, 0]]]); | ||
|
||
t.throws(() => booleanParallel(null, line), /line1 is required/, 'missing line1'); | ||
t.throws(() => booleanParallel(line, null), /line2 is required/, 'missing line2'); | ||
t.throws(() => booleanParallel(poly, line), /line1 must be a LineString/, 'different types'); | ||
t.throws(() => booleanParallel(line, poly), /line2 must be a LineString/, 'different types'); | ||
t.throws(() => booleanParallel({}, line), /Invalid GeoJSON object for line1/, 'invalid types'); | ||
t.throws(() => booleanParallel(line, {}), /Invalid GeoJSON object for line2/, 'invalid types'); | ||
|
||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[ | ||
-111.544189453125, | ||
24.186847428521244 | ||
], | ||
[ | ||
-110.687255859375, | ||
24.966140159912975 | ||
], | ||
[ | ||
-110.4510498046875, | ||
24.467150664739002 | ||
], | ||
[ | ||
-109.9951171875, | ||
25.180087808990645 | ||
] | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[ | ||
-111.4617919921875, | ||
24.05148034322011 | ||
], | ||
[ | ||
-110.8795166015625, | ||
24.681961205014595 | ||
], | ||
[ | ||
-110.841064453125, | ||
24.14174098050432 | ||
], | ||
[ | ||
-109.97863769531249, | ||
24.617057340809524 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@stebogit This private looks great, we should add this to
@turf/invariant
.Similar to
getGeomType
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.
Yeah, I always wondered why we didn't have that there 😄
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.
👍 I'll add it