utils/polyline/polyline-utils.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.splitPathAtBbox = exports.isPointInBbox = void 0;
/**
 * @namespace utils
 * @description Utility functions for geometric operations, including bounding box checks and path splitting.
 */
/**
 * Checks if a given point lies inside a bounding box.
 *
 * @memberof utils
 * @function isPointInBbox
 * @param {Position} point - A GeoJSON position represented as [longitude, latitude].
 * @param {BBOX} bbox - The bounding box, represented as [xmin, ymin, xmax, ymax].
 * @returns {boolean} Returns `true` if the point is inside the bounding box, otherwise `false`.
 * @example
 * const point: Position = [-79.5, 34.5];
 * const bbox: [number, number, number, number] = [-80, 34, -79, 35];
 * console.log(isPointInBbox(point, bbox)); // true
 */
const isPointInBbox = (point, bbox) => {
    const [lng, lat] = point;
    return lng >= bbox[0] && lng <= bbox[2] && lat >= bbox[1] && lat <= bbox[3];
};
exports.isPointInBbox = isPointInBbox;
/**
 * Splits a path at the edges of a bounding box.
 * Iterates through the path and creates segments of points that are inside the bounding box.
 *
 * @memberof utils
 * @function splitPathAtBbox
 * @param {Position[]} path - An array of GeoJSON positions representing the path to split.
 * @param {BBOX} bbox - The bounding box, represented as [xmin, ymin, xmax, ymax].
 * @returns {Position[][]} An array of path segments, where each segment is an array of positions inside the bounding box.
 * @example
 * const path: Position[] = [[-80, 34], [-79.5, 34.5], [-79, 35]];
 * const bbox: [number, number, number, number] = [-80, 34, -79, 35];
 * const segments = splitPathAtBbox(path, bbox);
 * console.log(segments);
 * // Output: [
 * //   [[-80, 34], [-79.5, 34.5], [-79, 35]]
 * // ]
 */
const splitPathAtBbox = (path, bbox) => {
    const splitPaths = [];
    let currentPath = [];
    for (let i = 0; i < path.length; i++) {
        const point = path[i];
        const inBbox = (0, exports.isPointInBbox)(point, bbox);
        if (inBbox) {
            currentPath.push(point);
        }
        else {
            if (currentPath.length > 0) {
                splitPaths.push(currentPath);
                currentPath = [];
            }
        }
    }
    if (currentPath.length > 0) {
        splitPaths.push(currentPath);
    }
    return splitPaths;
};
exports.splitPathAtBbox = splitPathAtBbox;