Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@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 992,197 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.