@turf/boolean-point-on-line
Advanced tools
Comparing version 4.7.3 to 5.0.0
@@ -1,11 +0,12 @@ | ||
/// <reference types="geojson" /> | ||
import { LineString, Feature, Coord } from '@turf/helpers' | ||
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; | ||
export default function ( | ||
point: Coord, | ||
linestring: Feature<LineString> | LineString, | ||
options?: { | ||
ignoreEndVertices?: boolean | ||
} | ||
): boolean; |
68
index.js
@@ -1,2 +0,3 @@ | ||
var getCoords = require('@turf/invariant').getCoords; | ||
import { getCoords, getCoord } from '@turf/invariant'; | ||
import { isObject } from '@turf/helpers'; | ||
@@ -7,5 +8,6 @@ /** | ||
* @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. | ||
* @param {Coord} pt GeoJSON Point | ||
* @param {Feature<LineString>} line GeoJSON LineString | ||
* @param {Object} [options={}] Optional parameters | ||
* @param {boolean} [options.ignoreEndVertices=false] whether to ignore the start and end vertices. | ||
* @returns {boolean} true/false | ||
@@ -18,5 +20,17 @@ * @example | ||
*/ | ||
module.exports = function (point, linestring, ignoreEndVertices) { | ||
var pointCoords = getCoords(point); | ||
var lineCoords = getCoords(linestring); | ||
function booleanPointOnLine(pt, line, options) { | ||
// Optional parameters | ||
options = options || {}; | ||
var ignoreEndVertices = options.ignoreEndVertices; | ||
if (!isObject(options)) throw new Error('invalid options'); | ||
// Validate input | ||
if (!pt) throw new Error('pt is required'); | ||
if (!line) throw new Error('line is required'); | ||
// Normalize inputs | ||
var ptCoords = getCoord(pt); | ||
var lineCoords = getCoords(line); | ||
// Main | ||
for (var i = 0; i < lineCoords.length - 1; i++) { | ||
@@ -29,6 +43,6 @@ var ignoreBoundary = false; | ||
} | ||
if (isPointOnLineSegment(lineCoords[i], lineCoords[i + 1], pointCoords, ignoreBoundary)) return true; | ||
if (isPointOnLineSegment(lineCoords[i], lineCoords[i + 1], ptCoords, ignoreBoundary)) return true; | ||
} | ||
return false; | ||
}; | ||
} | ||
@@ -40,11 +54,17 @@ // See http://stackoverflow.com/a/4833823/1979085 | ||
* @param {Array<number>} lineSegmentEnd coord pair of end of line | ||
* @param {Array<number>} point coord pair of point to check | ||
* @param {Array<number>} pt 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]; | ||
function isPointOnLineSegment(lineSegmentStart, lineSegmentEnd, pt, excludeBoundary) { | ||
var x = pt[0]; | ||
var y = pt[1]; | ||
var x1 = lineSegmentStart[0]; | ||
var y1 = lineSegmentStart[1]; | ||
var x2 = lineSegmentEnd[0]; | ||
var y2 = lineSegmentEnd[1]; | ||
var dxc = pt[0] - x1; | ||
var dyc = pt[1] - y1; | ||
var dxl = x2 - x1; | ||
var dyl = y2 - y1; | ||
var cross = dxc * dyl - dyc * dxl; | ||
@@ -56,21 +76,23 @@ if (cross !== 0) { | ||
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 dxl > 0 ? x1 <= x && x <= x2 : x2 <= x && x <= x1; | ||
} | ||
return dyl > 0 ? lineSegmentStart[1] <= point[1] && point[1] <= lineSegmentEnd[1] : lineSegmentEnd[1] <= point[1] && point[1] <= lineSegmentStart[1]; | ||
return dyl > 0 ? y1 <= y && y <= y2 : y2 <= y && y <= y1; | ||
} 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 dxl > 0 ? x1 < x && x <= x2 : x2 <= x && x < x1; | ||
} | ||
return dyl > 0 ? lineSegmentStart[1] < point[1] && point[1] <= lineSegmentEnd[1] : lineSegmentEnd[1] <= point[1] && point[1] < lineSegmentStart[1]; | ||
return dyl > 0 ? y1 < y && y <= y2 : y2 <= y && y < y1; | ||
} 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 dxl > 0 ? x1 <= x && x < x2 : x2 < x && x <= x1; | ||
} | ||
return dyl > 0 ? lineSegmentStart[1] <= point[1] && point[1] < lineSegmentEnd[1] : lineSegmentEnd[1] < point[1] && point[1] <= lineSegmentStart[1]; | ||
return dyl > 0 ? y1 <= y && y < y2 : y2 < y && y <= y1; | ||
} 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 dxl > 0 ? x1 < x && x < x2 : x2 < x && x < x1; | ||
} | ||
return dyl > 0 ? lineSegmentStart[1] < point[1] && point[1] < lineSegmentEnd[1] : lineSegmentEnd[1] < point[1] && point[1] < lineSegmentStart[1]; | ||
return dyl > 0 ? y1 < y && y < y2 : y2 < y && y < y1; | ||
} | ||
} | ||
export default booleanPointOnLine; |
{ | ||
"name": "@turf/boolean-point-on-line", | ||
"version": "4.7.3", | ||
"version": "5.0.0", | ||
"description": "turf boolean-point-on-line module", | ||
"main": "index.js", | ||
"main": "main", | ||
"module": "index", | ||
"jsnext:main": "index", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
"index.d.ts", | ||
"main.js" | ||
], | ||
"scripts": { | ||
"test": "node test.js", | ||
"bench": "node bench.js" | ||
"pretest": "rollup -c ../../rollup.config.js", | ||
"test": "node -r @std/esm test.js", | ||
"bench": "node -r @std/esm bench.js" | ||
}, | ||
@@ -33,11 +37,18 @@ "repository": { | ||
"devDependencies": { | ||
"benchmark": "2.1.4", | ||
"glob": "7.1.2", | ||
"load-json-file": "2.0.0", | ||
"tape": "4.7.0", | ||
"write-json-file": "2.2.0" | ||
"@std/esm": "*", | ||
"benchmark": "*", | ||
"glob": "*", | ||
"load-json-file": "*", | ||
"rollup": "*", | ||
"tape": "*", | ||
"write-json-file": "*" | ||
}, | ||
"dependencies": { | ||
"@turf/invariant": "^4.7.3" | ||
"@turf/helpers": "5.x", | ||
"@turf/invariant": "5.x" | ||
}, | ||
"@std/esm": { | ||
"esm": "js", | ||
"cjs": true | ||
} | ||
} |
# @turf/boolean-point-on-line | ||
# booleanPointOnLine | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
## booleanPointOnLine | ||
Returns true if a point is on a line. Accepts a optional parameter to ignore the start and end vertices of the linestring. | ||
@@ -9,5 +11,6 @@ | ||
- `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`) | ||
- `point` **Coord** GeoJSON Point | ||
- `linestring` **[Feature](http://geojson.org/geojson-spec.html#feature-objects)<[LineString](http://geojson.org/geojson-spec.html#linestring)>** GeoJSON LineString | ||
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Optional parameters (optional, default `{}`) | ||
- `options.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`) | ||
@@ -14,0 +17,0 @@ **Examples** |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11321
6
187
51
2
7
1
+ Added@turf/helpers@5.x
+ Added@turf/helpers@5.1.5(transitive)
+ Added@turf/invariant@5.2.0(transitive)
- Removed@turf/invariant@4.7.3(transitive)
Updated@turf/invariant@5.x