Skip to content

Commit

Permalink
Add epsilon option to booleanPointOnLine (#2051)
Browse files Browse the repository at this point in the history
* Add precision option to booleanPointOnLine

* Increase precision to 10

* Update parameter to epsilon

This also preserves backwards compatibility

Co-authored-by: mfedderly <mdfedderly@mdfedderly.com>
  • Loading branch information
okcoker and mfedderly committed Mar 15, 2021
1 parent 11d5e4f commit 8411070
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
16 changes: 13 additions & 3 deletions packages/turf-boolean-point-on-line/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getCoord, getCoords } from "@turf/invariant";
* @param {Feature<LineString>} line GeoJSON LineString
* @param {Object} [options={}] Optional parameters
* @param {boolean} [options.ignoreEndVertices=false] whether to ignore the start and end vertices.
* @param {number} [options.epsilon] Fractional number to compare with the cross product result. Useful for dealing with floating points such as lng/lat points
* @returns {boolean} true/false
* @example
* var pt = turf.point([0, 0]);
Expand All @@ -22,6 +23,7 @@ function booleanPointOnLine(
line: Feature<LineString> | LineString,
options: {
ignoreEndVertices?: boolean;
epsilon?: number;
} = {}
): boolean {
// Normalize inputs
Expand All @@ -47,7 +49,8 @@ function booleanPointOnLine(
lineCoords[i],
lineCoords[i + 1],
ptCoords,
ignoreBoundary
ignoreBoundary,
typeof options.epsilon === "undefined" ? null : options.epsilon
)
) {
return true;
Expand All @@ -57,20 +60,23 @@ function booleanPointOnLine(
}

// See http://stackoverflow.com/a/4833823/1979085
// See https://stackoverflow.com/a/328122/1048847
/**
* @private
* @param {Position} lineSegmentStart coord pair of start of line
* @param {Position} lineSegmentEnd coord pair of end of line
* @param {Position} pt coord pair of point to check
* @param {boolean|string} excludeBoundary whether the point is allowed to fall on the line ends.
* @param {number} epsilon Fractional number to compare with the cross product result. Useful for dealing with floating points such as lng/lat points
* If true which end to ignore.
* @returns {boolean} true/false
*/
function isPointOnLineSegment(
lineSegmentStart: number[],
lineSegmentEnd: number[],
pt: number[],
excludeBoundary: string | boolean
excludeBoundary: string | boolean,
epsilon: number | null
): boolean {
const x = pt[0];
const y = pt[1];
Expand All @@ -83,7 +89,11 @@ function isPointOnLineSegment(
const dxl = x2 - x1;
const dyl = y2 - y1;
const cross = dxc * dyl - dyc * dxl;
if (cross !== 0) {
if (epsilon !== null) {
if (Math.abs(cross) > epsilon) {
return false;
}
} else if (cross !== 0) {
return false;
}
if (!excludeBoundary) {
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-boolean-point-on-line/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test("turf-boolean-point-on-line", (t) => {
.forEach((filepath) => {
const name = path.parse(filepath).name;
const geojson = load.sync(filepath);
const options = geojson.propeties;
const options = geojson.properties;
const feature1 = geojson.features[0];
const feature2 = geojson.features[1];
const result = pointOnLine(feature1, feature2, options);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"type": "FeatureCollection",
"properties": {
"epsilon": 10e-18
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [-75.25737143565107, 39.99673377198139]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-75.2580499870244, 40.00180204907801],
[-75.25676601413157, 39.992211720827044]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [-75.25737143565107, 39.99673377198139]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-75.2580499870244, 40.00180204907801],
[-75.25676601413157, 39.992211720827044]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"type": "FeatureCollection",
"properties": {
"epsilon": 10e-17
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [-75.25737143565107, 39.99673377198139]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-75.2580499870244, 40.00180204907801],
[-75.25676601413157, 39.992211720827044]
]
}
}
]
}

0 comments on commit 8411070

Please sign in to comment.