Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@turf/boolean-point-on-line

Package Overview
Dependencies
Maintainers
4
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turf/boolean-point-on-line - npm Package Compare versions

Comparing version 4.7.3 to 5.0.0

main.js

15

index.d.ts

@@ -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;

@@ -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)&lt;[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)&lt;[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)&lt;[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**

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc