# How to use @turf/point-to-line-distance - 8 common examples

## To help you get started, we’ve selected a few @turf/point-to-line-distance examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

uber / nebula.gl / modules / core / src / lib / utils.js View on Github
``````export function generatePointsParallelToLinePoints(
p1: Position,
p2: Position,
groundCoords: Position
): Position[] {
const lineString: LineString = {
type: 'LineString',
coordinates: [p1, p2]
};
const pt = point(groundCoords);
const ddistance = pointToLineDistance(pt, lineString);
const lineBearing = bearing(p1, p2);

// Check if current point is to the left or right of line
// Line from A=(x1,y1) to B=(x2,y2) a point P=(x,y)
// then (x−x1)(y2−y1)−(y−y1)(x2−x1)
const isPointToLeftOfLine =
(groundCoords[0] - p1[0]) * (p2[1] - p1[1]) - (groundCoords[1] - p1[1]) * (p2[0] - p1[0]);

// Bearing to draw perpendicular to the line string
const orthogonalBearing = isPointToLeftOfLine &lt; 0 ? lineBearing - 90 : lineBearing - 270;

// Get coordinates for the point p3 and p4 which are perpendicular to the lineString
// Add the distance as the current position moves away from the lineString
const p3 = destination(p2, ddistance, orthogonalBearing);
const p4 = destination(p1, ddistance, orthogonalBearing);``````
uber / nebula.gl / modules / core / src / lib / editable-feature-collection.js View on Github
``````type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [this._clickSequence[0], groundCoords]
}
});
} else if (this._clickSequence.length === 2) {
const lineString: LineString = {
type: 'LineString',
coordinates: this._clickSequence
};

const [p1, p2] = this._clickSequence;
const pt = point(groundCoords);
const options = { units: 'miles' };
const ddistance = pointToLineDistance(pt, lineString, options);
const lineBearing = bearing(p1, p2);

// Check if current point is to the left or right of line
// Line from A=(x1,y1) to B=(x2,y2) a point P=(x,y)
// then (x−x1)(y2−y1)−(y−y1)(x2−x1)
const isPointToLeftOfLine =
(groundCoords[0] - p1[0]) * (p2[1] - p1[1]) - (groundCoords[1] - p1[1]) * (p2[0] - p1[0]);

// Bearing to draw perpendicular to the line string
const orthogonalBearing = isPointToLeftOfLine &lt; 0 ? lineBearing - 90 : lineBearing - 270;

// Get coordinates for the point p3 and p4 which are perpendicular to the lineString
// Add the distance as the current position moves away from the lineString
const p3 = destination(p2, ddistance, orthogonalBearing, options);
const p4 = destination(p1, ddistance, orthogonalBearing, options);``````
conveyal / analysis-ui / lib / utils / get-existing-stops-along-pattern.js View on Github
``````stops.forEach(stop =&gt; {
// Ignore the source segment's start and end stops
if (stop.stop_id !== segment.fromStopId &amp;&amp; stop.stop_id !== segment.toStopId) {
const stopPoint = turf.point([stop.stop_lon, stop.stop_lat])
if (pointToLineDistance(stopPoint, line) &lt;= STOP_SEARCH_BUFFER_KM) {
const pointOnLine = nearestPointOnLine(line, stopPoint)
// Ignore points too close to start or end stops
if ((!segment.stopAtStart ||
pointOnLine.properties.location &gt; APPROX_MIN_SPACING_KM) &amp;&amp;
(!segment.stopAtEnd ||
segmentLengthKm - pointOnLine.properties.location &gt; APPROX_MIN_SPACING_KM)
) {
pointOnLine.properties.stopId = stop.stop_id
pointOnLine.properties.stopCoords = [stop.stop_lon, stop.stop_lat]
candidateStops.push(pointOnLine)
}
}
}
})
// cluster stops that are close to each other``````
uber / nebula.gl / modules / layers / src / utils.js View on Github
``````export function generatePointsParallelToLinePoints(
p1: Position,
p2: Position,
groundCoords: Position
): Position[] {
const lineString: LineString = {
type: 'LineString',
coordinates: [p1, p2]
};
const pt = point(groundCoords);
const ddistance = pointToLineDistance(pt, lineString);
const lineBearing = bearing(p1, p2);

// Check if current point is to the left or right of line
// Line from A=(x1,y1) to B=(x2,y2) a point P=(x,y)
// then (x−x1)(y2−y1)−(y−y1)(x2−x1)
const isPointToLeftOfLine =
(groundCoords[0] - p1[0]) * (p2[1] - p1[1]) - (groundCoords[1] - p1[1]) * (p2[0] - p1[0]);

// Bearing to draw perpendicular to the line string
const orthogonalBearing = isPointToLeftOfLine &lt; 0 ? lineBearing - 90 : lineBearing - 270;

// Get coordinates for the point p3 and p4 which are perpendicular to the lineString
// Add the distance as the current position moves away from the lineString
const p3 = destination(p2, ddistance, orthogonalBearing);
const p4 = destination(p1, ddistance, orthogonalBearing);``````
ngageoint / geopackage-js / test / lib / features / user / testFeatureDao.js View on Github
``````if (distance &lt; closestDistance) {
closest = row;
closestDistance = distance;
} else if (distance == closestDistance &amp;&amp; closest.type != 'Point') {
closest = row;
closestDistance = distance;
}
} else if (geometry.type == 'Polygon') {
if (booleanPointInPolygon(centerPoint, geometry)) {
if (closestDistance != 0) {
closest = row;
closestDistance = 0;
}
} else {
var line = polygonToLine(geometry);
var distance = pointToLineDistance(centerPoint, line);
if (distance &lt; closestDistance) {
closest = row;
closestDistance = distance;
}
}
}
}
closest.values.name.should.be.equal('point');
foundFeatures.should.be.deep.equal(['box1', 'box2', 'line', 'point']);
});``````
ngageoint / geopackage-js / test / lib / features / user / testFeatureDao.js View on Github
``````for (var row of iterator) {
foundFeatures.push(row);
var geometry = row.geometry;

if (geometry.type == 'Point') {
var distance = pointDistance(centerPoint, geometry);
if (distance &lt; closestDistance) {
closest = row;
closestDistance = distance;
} else if (distance == closestDistance &amp;&amp; closest.type != 'Point') {
closest = row;
closestDistance = distance;
}
} else if (geometry.type == 'LineString') {
var distance = pointToLineDistance(centerPoint, geometry);
if (distance &lt; closestDistance) {
closest = row;
closestDistance = distance;
} else if (distance == closestDistance &amp;&amp; closest.type != 'Point') {
closest = row;
closestDistance = distance;
}
} else if (geometry.type == 'Polygon') {
if (booleanPointInPolygon(centerPoint, geometry)) {
if (closestDistance != 0) {
closest = row;
closestDistance = 0;
}
} else {
var line = polygonToLine(geometry);
var distance = pointToLineDistance(centerPoint, line);``````
ngageoint / geopackage-js / test / lib / features / user / testFeatureDao.js View on Github
``````for (var row of iterator) {
foundFeatures.push(row.values.name);
var geometry = row.getGeometry().toGeoJSON();

if (geometry.type == 'Point') {
var distance = pointDistance(centerPoint, geometry);
if (distance &lt; closestDistance) {
closest = row;
closestDistance = distance;
} else if (distance == closestDistance &amp;&amp; closest.type != 'Point') {
closest = row;
closestDistance = distance;
}
} else if (geometry.type == 'LineString') {
var distance = pointToLineDistance(centerPoint, geometry);
if (distance &lt; closestDistance) {
closest = row;
closestDistance = distance;
} else if (distance == closestDistance &amp;&amp; closest.type != 'Point') {
closest = row;
closestDistance = distance;
}
} else if (geometry.type == 'Polygon') {
if (booleanPointInPolygon(centerPoint, geometry)) {
if (closestDistance != 0) {
closest = row;
closestDistance = 0;
}
} else {
var line = polygonToLine(geometry);
var distance = pointToLineDistance(centerPoint, line);``````
Turfjs / turf / packages / turf-nearest-point-to-line / index.ts View on Github
``````featureEach(pts, (point) =&gt; {
const d = pointToLineDistance(point, line, { units });
if (d &lt; dist) {
dist = d;
pt = point;
}
});
/**``````

## @turf/point-to-line-distance

turf point-to-line-distance module

MIT