utils/polyline/turf-buffer.js

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.offsetPolygon = offsetPolygon;
const proj4_1 = __importDefault(require("proj4"));
const turf = __importStar(require("@turf/turf"));
// Function to offset a polygon by a given distance
/**
 * Offsets a polygon by a given distance and reprojects the coordinates.
 *
 * @param polygonCoords - The coordinates of the polygon to be offset. It is a 3D array of numbers.
 * @param offsetDistance - The distance by which to offset the polygon.
 * @param spatialReference - The spatial reference system identifier (e.g., EPSG code) for the input coordinates.
 * @returns The new polygon with offset coordinates in the original projection.
 * @throws Will throw an error if the offset polygon is undefined.
 */
function offsetPolygon(polygonCoords, offsetDistance, spatialReference) {
    const WGS84 = proj4_1.default.WGS84;
    const sourceProjection = `EPSG:${spatialReference}`;
    const reprojectedCoords = polygonCoords[0].map((coord) => {
        return (0, proj4_1.default)(sourceProjection, WGS84, coord);
    });
    //console.log(reprojectedCoords)
    // Create a GeoJSON Polygon feature from the coordinates
    const polygon = turf.polygon([reprojectedCoords]);
    //console.log(polygon.geometry.coordinates)
    // Offset (buffer) the polygon by the given distance
    const offsetPolygon = turf.buffer(polygon, offsetDistance, { units: 'feet', steps: 5 });
    // Reproject the offset polygon back to the original projection
    if (!offsetPolygon) {
        throw new Error('Offset polygon is undefined');
    }
    const offsetCoords = offsetPolygon.geometry.coordinates[0].map((coord) => {
        return (0, proj4_1.default)(WGS84, sourceProjection, coord);
    });
    // Return the new polygon in the original projection
    return turf.polygon([offsetCoords]);
}