123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- "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;
|