all these changes

This commit is contained in:
Jake Kasper
2026-04-09 13:19:47 -05:00
parent e83a51a051
commit 65315f36d1
39102 changed files with 7932979 additions and 567 deletions

View File

@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2017 TurfJS
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,83 @@
# @turf/nearest-point-on-line
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
## nearestPointOnLine
Takes a [Point][1] and a [LineString][2] and calculates the closest Point on the (Multi)LineString.
**Parameters**
- `lines` **([Geometry][3] \| [Feature][4]&lt;([LineString][5] \| [MultiLineString][6])>)** lines to snap to
- `pt` **([Geometry][3] \| [Feature][4]&lt;[Point][7]> | [Array][8]&lt;[number][9]>)** point to snap from
- `options` **[Object][10]** Optional parameters (optional, default `{}`)
- `options.units` **[string][11]** can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
**Examples**
```javascript
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][4]&lt;[Point][7]>** closest point on the `line` to `point`. The properties object will contain three values: `index`: closest point was found on nth line part, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.
[1]: https://tools.ietf.org/html/rfc7946#section-3.1.2
[2]: https://tools.ietf.org/html/rfc7946#section-3.1.4
[3]: https://tools.ietf.org/html/rfc7946#section-3.1
[4]: https://tools.ietf.org/html/rfc7946#section-3.2
[5]: https://tools.ietf.org/html/rfc7946#section-3.1.4
[6]: https://tools.ietf.org/html/rfc7946#section-3.1.5
[7]: https://tools.ietf.org/html/rfc7946#section-3.1.2
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[11]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
<!-- This file is automatically generated. Please don't edit it directly:
if you find an error, edit the source file (likely index.js), and re-run
./scripts/generate-readmes in the turf project. -->
---
This module is part of the [Turfjs project](http://turfjs.org/), an open source
module collection dedicated to geographic algorithms. It is maintained in the
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
PRs and issues.
### Installation
Install this module individually:
```sh
$ npm install @turf/nearest-point-on-line
```
Or install the Turf module that includes it as a function:
```sh
$ npm install @turf/turf
```

View File

@@ -0,0 +1,88 @@
import bearing from "@turf/bearing";
import distance from "@turf/distance";
import destination from "@turf/destination";
import lineIntersects from "@turf/line-intersect";
import { flattenEach } from "@turf/meta";
import { point, lineString, } from "@turf/helpers";
import { getCoords } from "@turf/invariant";
/**
* Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString.
*
* @name nearestPointOnLine
* @param {Geometry|Feature<LineString|MultiLineString>} lines lines to snap to
* @param {Geometry|Feature<Point>|number[]} pt point to snap from
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
* @returns {Feature<Point>} closest point on the `line` to `point`. The properties object will contain three values: `index`: closest point was found on nth line part, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.
* @example
* 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';
*/
function nearestPointOnLine(lines, pt, options) {
if (options === void 0) { options = {}; }
var closestPt = point([Infinity, Infinity], {
dist: Infinity,
});
var length = 0.0;
flattenEach(lines, function (line) {
var coords = getCoords(line);
for (var i = 0; i < coords.length - 1; i++) {
//start
var start = point(coords[i]);
start.properties.dist = distance(pt, start, options);
//stop
var stop_1 = point(coords[i + 1]);
stop_1.properties.dist = distance(pt, stop_1, options);
// sectionLength
var sectionLength = distance(start, stop_1, options);
//perpendicular
var heightDistance = Math.max(start.properties.dist, stop_1.properties.dist);
var direction = bearing(start, stop_1);
var perpendicularPt1 = destination(pt, heightDistance, direction + 90, options);
var perpendicularPt2 = destination(pt, heightDistance, direction - 90, options);
var intersect = lineIntersects(lineString([
perpendicularPt1.geometry.coordinates,
perpendicularPt2.geometry.coordinates,
]), lineString([start.geometry.coordinates, stop_1.geometry.coordinates]));
var intersectPt = null;
if (intersect.features.length > 0) {
intersectPt = intersect.features[0];
intersectPt.properties.dist = distance(pt, intersectPt, options);
intersectPt.properties.location =
length + distance(start, intersectPt, options);
}
if (start.properties.dist < closestPt.properties.dist) {
closestPt = start;
closestPt.properties.index = i;
closestPt.properties.location = length;
}
if (stop_1.properties.dist < closestPt.properties.dist) {
closestPt = stop_1;
closestPt.properties.index = i + 1;
closestPt.properties.location = length + sectionLength;
}
if (intersectPt &&
intersectPt.properties.dist < closestPt.properties.dist) {
closestPt = intersectPt;
closestPt.properties.index = i;
}
// update length
length += sectionLength;
}
});
return closestPt;
}
export default nearestPointOnLine;

View File

@@ -0,0 +1 @@
{"type":"module"}

View File

@@ -0,0 +1,39 @@
import { Feature, Point, LineString, MultiLineString, Coord, Units } from "@turf/helpers";
export interface NearestPointOnLine extends Feature<Point> {
properties: {
index?: number;
dist?: number;
location?: number;
[key: string]: any;
};
}
/**
* Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString.
*
* @name nearestPointOnLine
* @param {Geometry|Feature<LineString|MultiLineString>} lines lines to snap to
* @param {Geometry|Feature<Point>|number[]} pt point to snap from
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
* @returns {Feature<Point>} closest point on the `line` to `point`. The properties object will contain three values: `index`: closest point was found on nth line part, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.
* @example
* 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';
*/
declare function nearestPointOnLine<G extends LineString | MultiLineString>(lines: Feature<G> | G, pt: Coord, options?: {
units?: Units;
}): NearestPointOnLine;
export default nearestPointOnLine;

View File

@@ -0,0 +1,93 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var bearing_1 = __importDefault(require("@turf/bearing"));
var distance_1 = __importDefault(require("@turf/distance"));
var destination_1 = __importDefault(require("@turf/destination"));
var line_intersect_1 = __importDefault(require("@turf/line-intersect"));
var meta_1 = require("@turf/meta");
var helpers_1 = require("@turf/helpers");
var invariant_1 = require("@turf/invariant");
/**
* Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString.
*
* @name nearestPointOnLine
* @param {Geometry|Feature<LineString|MultiLineString>} lines lines to snap to
* @param {Geometry|Feature<Point>|number[]} pt point to snap from
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
* @returns {Feature<Point>} closest point on the `line` to `point`. The properties object will contain three values: `index`: closest point was found on nth line part, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.
* @example
* 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';
*/
function nearestPointOnLine(lines, pt, options) {
if (options === void 0) { options = {}; }
var closestPt = helpers_1.point([Infinity, Infinity], {
dist: Infinity,
});
var length = 0.0;
meta_1.flattenEach(lines, function (line) {
var coords = invariant_1.getCoords(line);
for (var i = 0; i < coords.length - 1; i++) {
//start
var start = helpers_1.point(coords[i]);
start.properties.dist = distance_1.default(pt, start, options);
//stop
var stop_1 = helpers_1.point(coords[i + 1]);
stop_1.properties.dist = distance_1.default(pt, stop_1, options);
// sectionLength
var sectionLength = distance_1.default(start, stop_1, options);
//perpendicular
var heightDistance = Math.max(start.properties.dist, stop_1.properties.dist);
var direction = bearing_1.default(start, stop_1);
var perpendicularPt1 = destination_1.default(pt, heightDistance, direction + 90, options);
var perpendicularPt2 = destination_1.default(pt, heightDistance, direction - 90, options);
var intersect = line_intersect_1.default(helpers_1.lineString([
perpendicularPt1.geometry.coordinates,
perpendicularPt2.geometry.coordinates,
]), helpers_1.lineString([start.geometry.coordinates, stop_1.geometry.coordinates]));
var intersectPt = null;
if (intersect.features.length > 0) {
intersectPt = intersect.features[0];
intersectPt.properties.dist = distance_1.default(pt, intersectPt, options);
intersectPt.properties.location =
length + distance_1.default(start, intersectPt, options);
}
if (start.properties.dist < closestPt.properties.dist) {
closestPt = start;
closestPt.properties.index = i;
closestPt.properties.location = length;
}
if (stop_1.properties.dist < closestPt.properties.dist) {
closestPt = stop_1;
closestPt.properties.index = i + 1;
closestPt.properties.location = length + sectionLength;
}
if (intersectPt &&
intersectPt.properties.dist < closestPt.properties.dist) {
closestPt = intersectPt;
closestPt.properties.index = i;
}
// update length
length += sectionLength;
}
});
return closestPt;
}
exports.default = nearestPointOnLine;

View File

@@ -0,0 +1,67 @@
{
"name": "@turf/nearest-point-on-line",
"version": "6.5.0",
"description": "turf nearest-point-on-line module",
"author": "Turf Authors",
"license": "MIT",
"bugs": {
"url": "https://github.com/Turfjs/turf/issues"
},
"homepage": "https://github.com/Turfjs/turf",
"repository": {
"type": "git",
"url": "git://github.com/Turfjs/turf.git"
},
"funding": "https://opencollective.com/turf",
"publishConfig": {
"access": "public"
},
"main": "dist/js/index.js",
"module": "dist/es/index.js",
"exports": {
"./package.json": "./package.json",
".": {
"import": "./dist/es/index.js",
"require": "./dist/js/index.js"
}
},
"types": "dist/js/index.d.ts",
"sideEffects": false,
"files": [
"dist"
],
"scripts": {
"bench": "ts-node bench.js",
"build": "npm-run-all build:*",
"build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
"build:js": "tsc",
"docs": "node ../../scripts/generate-readmes",
"test": "npm-run-all test:*",
"test:tape": "ts-node -r esm test.js",
"test:types": "tsc --esModuleInterop --noEmit types.ts"
},
"devDependencies": {
"@turf/along": "^6.5.0",
"@turf/length": "^6.5.0",
"@turf/truncate": "^6.5.0",
"@types/tape": "*",
"benchmark": "*",
"load-json-file": "*",
"npm-run-all": "*",
"tape": "*",
"ts-node": "*",
"tslint": "*",
"typescript": "*",
"write-json-file": "*"
},
"dependencies": {
"@turf/bearing": "^6.5.0",
"@turf/destination": "^6.5.0",
"@turf/distance": "^6.5.0",
"@turf/helpers": "^6.5.0",
"@turf/invariant": "^6.5.0",
"@turf/line-intersect": "^6.5.0",
"@turf/meta": "^6.5.0"
},
"gitHead": "5375941072b90d489389db22b43bfe809d5e451e"
}