Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@turf/nearest-point-on-line
Advanced tools
@turf/nearest-point-on-line is a module from the Turf.js library that allows you to find the nearest point on a line from a given point. This can be useful in various geospatial analyses, such as snapping a point to a road network or finding the closest point of interest along a path.
Find Nearest Point on Line
This feature allows you to find the nearest point on a given line from a specified point. In the code sample, a line and a point are defined, and the nearest point on the line to the given point is calculated and logged.
const turf = require('@turf/turf');
const line = turf.lineString([[0, 0], [2, 2], [3, 3]]);
const point = turf.point([1, 2]);
const snapped = turf.nearestPointOnLine(line, point);
console.log(snapped);
Geolib is a library for geospatial calculations in JavaScript. It provides various functions for distance calculations, finding nearest points, and more. Compared to @turf/nearest-point-on-line, Geolib offers a broader range of geospatial utilities but may not be as specialized in line-specific operations.
Geodesy is a library for geodesic and trigonometric calculations. It includes functions for distance calculations, bearing, and more. While it provides some similar functionalities, it is more focused on geodesic calculations rather than finding the nearest point on a line.
Takes a Point and a LineString and calculates the closest Point on the (Multi)LineString.
lines
(Geometry | Feature<(LineString | MultiLineString)>) lines to snap to
pt
(Geometry | Feature<Point> | Array<number>) point to snap from
options
Object Optional parameters (optional, default {}
)
options.units
string can be degrees, radians, miles, or kilometers (optional, default 'kilometers'
)var line = turf.lineString([
[-77.031669, 38.878605],
[-77.029609, 38.881946],
[-77.020339, 38.884084],
[-77.025661, 38.885821],
[-77.021884, 38.889563],
[-77.019824, 38.892368]
]);
var pt = turf.point([-77.037076, 38.884017]);
var snapped = turf.nearestPointOnLine(line, pt, {units: 'miles'});
//addToMap
var addToMap = [line, pt, snapped];
snapped.properties['marker-color'] = '#00f';
Returns Feature<Point> closest point on the line
to point
. The properties object will contain four values: index
: closest point was found on nth line part, multiFeatureIndex
: closest point was found on the nth line of the MultiLineString
, dist
: distance between pt and the closest point, location
: distance along the line between start and the closest point.
This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.
Install this single module individually:
$ npm install @turf/nearest-point-on-line
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf
FAQs
turf nearest-point-on-line module
The npm package @turf/nearest-point-on-line receives a total of 794,413 weekly downloads. As such, @turf/nearest-point-on-line popularity was classified as popular.
We found that @turf/nearest-point-on-line demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.