utils/coordinate/coordinate-utils.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const polyline_utils_1 = require("../polyline/polyline-utils");
/**
 * @namespace geometryFilter
 * @description Utility functions for filtering geometry coordinates based on bounding boxes.
 */
/**
 * Filters coordinates within the specified bounding box.
 *
 * Recursively filters coordinates for different GeoJSON geometries including `Point`, `LineString`, `Polygon`, `MultiLineString`, and `MultiPolygon`.
 *
 * @memberof geometryFilter
 * @function filterCoordinates
 * @param {Array} coordinates - The GeoJSON coordinates to filter. Can be a nested array representing different geometry types.
 * @param {Array<number>} bbox - The bounding box, represented as [xmin, ymin, xmax, ymax].
 * @returns {Array} The filtered coordinates within the bounding box. The structure matches the input geometry type.
 * @example
 * const coordinates = [
 *   [[-80, 34], [-79.5, 34.5], [-79, 35]],
 *   [[-81, 33], [-80, 34], [-79.5, 34.5]]
 * ];
 * const bbox = [-80, 34, -79, 35];
 * const filtered = filterCoordinates(coordinates, bbox);
 * console.log(filtered);
 * // Output: [
 * //   [[-80, 34], [-79.5, 34.5]],
 * //   []
 * // ]
 */
const filterCoordinates = (coordinates, bbox) => {
    if (Array.isArray(coordinates[0])) {
        // Handles nested geometries like LineString, Polygon, MultiLineString, MultiPolygon
        return coordinates
            .map((coord) => filterCoordinates(coord, bbox))
            .filter((coord) => coord.length > 0);
    }
    // Handles Point and MultiPoint geometries
    return coordinates.filter((point) => (0, polyline_utils_1.isPointInBbox)(point, bbox));
};
exports.default = filterCoordinates;