forked from Turfjs/turf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of booleanPointOnLine (Turfjs#858)
* Initial commit of booleanPointOnLine * Changed folder of module. Built readme. * Better handling for start and end vertices * add invariant dependency * Added yarn lock. Fixed support for lines with only 1 segment. * Fix typescript defintions * Update index.d.ts
- Loading branch information
1 parent
46756c2
commit 2af43f6
Showing
21 changed files
with
1,031 additions
and
0 deletions.
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-point-on-line | ||
|
||
# booleanPointOnLine | ||
|
||
Returns true if a point is on a line. Accepts a optional parameter to ignore the start and end vertices of the linestring. | ||
|
||
**Parameters** | ||
|
||
- `point` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<[Point](http://geojson.org/geojson-spec.html#point)>)** GeoJSON Feature or Geometry | ||
- `linestring` **([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 | ||
- `ignoreEndVertices` **\[[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** whether to ignore the start and end vertices. (optional, default `false`) | ||
|
||
**Examples** | ||
|
||
```javascript | ||
var pt = turf.point([0, 0]); | ||
var line = turf.lineString([[-1, -1],[1, 1],[1.5, 2.2]]); | ||
var isPointOnLine = turf.booleanPointOnLine(pt, line); | ||
//=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-point-on-line | ||
``` | ||
|
||
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,41 @@ | ||
const path = require('path'); | ||
const glob = require('glob'); | ||
const load = require('load-json-file'); | ||
const Benchmark = require('benchmark'); | ||
const booleanPointOnLine = require('./'); | ||
|
||
/** | ||
* Benchmark Results | ||
* | ||
* LineWithOnly1Segment: 0.557ms | ||
* LineWithOnly1SegmentOnStart: 0.024ms | ||
* PointOnFirstSegment: 0.023ms | ||
* PointOnLastSegment: 0.040ms | ||
* PointOnLineEnd: 0.073ms | ||
* PointOnLineMidpoint: 0.007ms | ||
* PointOnLineMidVertice: 0.011ms | ||
* PointOnLineStart: 0.007ms | ||
* LineWithOnly1Segment x 14,778,798 ops/sec ±3.14% (82 runs sampled) | ||
* LineWithOnly1SegmentOnStart x 13,982,962 ops/sec ±3.47% (76 runs sampled) | ||
* PointOnFirstSegment x 15,369,530 ops/sec ±4.47% (81 runs sampled) | ||
* PointOnLastSegment x 12,944,744 ops/sec ±1.29% (90 runs sampled) | ||
* PointOnLineEnd x 13,012,269 ops/sec ±1.52% (89 runs sampled) | ||
* PointOnLineMidpoint x 17,516,146 ops/sec ±0.57% (93 runs sampled) | ||
* PointOnLineMidVertice x 17,351,167 ops/sec ±1.69% (92 runs sampled) | ||
* PointOnLineStart x 14,669,195 ops/sec ±6.96% (78 runs sampled) | ||
*/ | ||
const suite = new Benchmark.Suite('turf-booleanPointOnLine'); | ||
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; | ||
console.time(name); | ||
booleanPointOnLine(feature1, feature2); | ||
console.timeEnd(name); | ||
suite.add(name, () => booleanPointOnLine(feature1, feature2)); | ||
}); | ||
|
||
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 Point = GeoJSON.Feature<GeoJSON.Point> | GeoJSON.Point; | ||
type Line = GeoJSON.Feature<GeoJSON.LineString> | GeoJSON.LineString; | ||
|
||
/** | ||
* http://turfjs.org/docs/#booleanpointonline | ||
*/ | ||
declare function booleanPointOnLine(point: Point, linestring: Line, ignoreEndVertices?: boolean): boolean; | ||
declare namespace booleanPointOnLine { } | ||
export = booleanPointOnLine; |
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,71 @@ | ||
var getCoords = require('@turf/invariant').getCoords; | ||
|
||
/** | ||
* Returns true if a point is on a line. Accepts a optional parameter to ignore the start and end vertices of the linestring. | ||
* | ||
* @name booleanPointOnLine | ||
* @param {Geometry|Feature<Point>} point GeoJSON Feature or Geometry | ||
* @param {Geometry|Feature<LineString>} linestring GeoJSON Feature or Geometry | ||
* @param {boolean} [ignoreEndVertices=false] whether to ignore the start and end vertices. | ||
* @returns {boolean} true/false | ||
* @example | ||
* var pt = turf.point([0, 0]); | ||
* var line = turf.lineString([[-1, -1],[1, 1],[1.5, 2.2]]); | ||
* var isPointOnLine = turf.booleanPointOnLine(pt, line); | ||
* //=true | ||
*/ | ||
module.exports = function (point, linestring, ignoreEndVertices) { | ||
var pointCoords = getCoords(point); | ||
var lineCoords = getCoords(linestring); | ||
for (var i = 0; i < lineCoords.length - 1; i++) { | ||
var ignoreBoundary = false; | ||
if (ignoreEndVertices) { | ||
if (i === 0) ignoreBoundary = 'start'; | ||
if (i === lineCoords.length - 2) ignoreBoundary = 'end'; | ||
if (i === 0 && i + 1 === lineCoords.length - 1) ignoreBoundary = 'both'; | ||
} | ||
if (isPointOnLineSegment(lineCoords[i], lineCoords[i + 1], pointCoords, ignoreBoundary)) return true; | ||
} | ||
return false; | ||
}; | ||
|
||
// See http://stackoverflow.com/a/4833823/1979085 | ||
/** | ||
* @private | ||
* @param {Array} lineSegmentStart coord pair of start of line | ||
* @param {Array} lineSegmentEnd coord pair of end of line | ||
* @param {Array} point coord pair of point to check | ||
* @param {boolean|String} excludeBoundary whether the point is allowed to fall on the line ends. If true which end to ignore. | ||
* @returns {boolean} true/false | ||
*/ | ||
function isPointOnLineSegment(lineSegmentStart, lineSegmentEnd, point, excludeBoundary) { | ||
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 (!excludeBoundary) { | ||
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]; | ||
} | ||
return dyl > 0 ? lineSegmentStart[1] <= point[1] && point[1] <= lineSegmentEnd[1] : lineSegmentEnd[1] <= point[1] && point[1] <= lineSegmentStart[1]; | ||
} else if (excludeBoundary === 'start') { | ||
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]; | ||
} | ||
return dyl > 0 ? lineSegmentStart[1] < point[1] && point[1] <= lineSegmentEnd[1] : lineSegmentEnd[1] <= point[1] && point[1] < lineSegmentStart[1]; | ||
} else if (excludeBoundary === 'end') { | ||
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]; | ||
} | ||
return dyl > 0 ? lineSegmentStart[1] <= point[1] && point[1] < lineSegmentEnd[1] : lineSegmentEnd[1] < point[1] && point[1] <= lineSegmentStart[1]; | ||
} else if (excludeBoundary === 'both') { | ||
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]; | ||
} | ||
return dyl > 0 ? lineSegmentStart[1] < point[1] && point[1] < lineSegmentEnd[1] : lineSegmentEnd[1] < point[1] && point[1] < lineSegmentStart[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,42 @@ | ||
{ | ||
"name": "@turf/boolean-point-on-line", | ||
"version": "4.0.0", | ||
"description": "turf boolean-point-on-line 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", | ||
"booleanPointOnLine" | ||
], | ||
"author": "Turf Authors", | ||
"contributors": [ | ||
"Rowan Winsemius <@rowanwins>" | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Turfjs/turf/issues" | ||
}, | ||
"homepage": "https://github.com/Turfjs/turf", | ||
"devDependencies": { | ||
"benchmark": "^2.1.4", | ||
"glob": "^7.1.2", | ||
"load-json-file": "^2.0.0", | ||
"tape": "^4.6.3", | ||
"write-json-file": "^2.2.0" | ||
}, | ||
"dependencies": { | ||
"@turf/invariant": "^4.5.2" | ||
} | ||
} |
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,27 @@ | ||
const glob = require('glob'); | ||
const path = require('path'); | ||
const test = require('tape'); | ||
const load = require('load-json-file'); | ||
const pointOnLine = require('./'); | ||
|
||
test('turf-boolean-point-on-line', 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 = pointOnLine(feature1, feature2, feature1.properties.ignoreEndPoint); | ||
|
||
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 = pointOnLine(feature1, feature2, feature1.properties.ignoreEndPoint); | ||
|
||
t.false(result, '[false] ' + name); | ||
}); | ||
t.end(); | ||
}); |
35 changes: 35 additions & 0 deletions
35
...urf-boolean-point-on-line/test/false/LineWithOnly1SegmentIgnoreBoundary.geojson
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,35 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": { | ||
"ignoreEndPoint": true | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
0, | ||
0 | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[ | ||
0, | ||
0 | ||
], | ||
[ | ||
3, | ||
3 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
35 changes: 35 additions & 0 deletions
35
...-boolean-point-on-line/test/false/LineWithOnly1SegmentIgnoreBoundaryEnd.geojson
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,35 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": { | ||
"ignoreEndPoint": true | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
3, | ||
3 | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[ | ||
0, | ||
0 | ||
], | ||
[ | ||
3, | ||
3 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
37 changes: 37 additions & 0 deletions
37
...urf-boolean-point-on-line/test/false/PointOnEndFailsWhenIgnoreEndpoints.geojson
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,37 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": { | ||
"ignoreEndPoint": true | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
38.3203125, | ||
5.965753671065536 | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[ | ||
0,0 | ||
], | ||
[ | ||
3,3 | ||
], | ||
[ | ||
38.3203125, | ||
5.965753671065536 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.