fetch/fetch-ngs-data-by-box.js

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchNGSdataByBox = fetchNGSdataByBox;
exports.parseGeometryBox = parseGeometryBox;
const axios_1 = __importDefault(require("axios"));
const constants_1 = require("../utils/constants/constants");
/**
 * @namespace fetch
 * @description Namespace for GIS Cad Connector functions and utilities.
 */
/**
 * Fetches NGS data based on user-provided bounding box and spatial reference.
 *
 * @async
 * @function fetchNGSdataByBox
 * @memberof fetch
 * @param {number} xmin - Minimum X coordinate of the bounding box.
 * @param {number} ymin - Minimum Y coordinate of the bounding box.
 * @param {number} xmax - Maximum X coordinate of the bounding box.
 * @param {number} ymax - Maximum Y coordinate of the bounding box.
 * @param {string} urlString - The base URL for the ESRI service.
 * @param {string} spatialReference - The spatial reference for the bounding box.
 * @returns {Promise<object>} The fetched NGS data, filtered by attributes and geometry.
 * @throws Will throw an error if the fetch fails or if there are response issues.
 */
function fetchNGSdataByBox(xmin, ymin, xmax, ymax, urlString, spatialReference) {
    return __awaiter(this, void 0, void 0, function* () {
        const geometry = `${xmin},${ymin},${xmax},${ymax}`;
        const url = `${urlString}?outFields=*&geometry=${encodeURIComponent(geometry)}&geometryType=esriGeometryEnvelope&inSR=${spatialReference}&spatialRel=esriSpatialRelIntersects&outSR=${spatialReference}&f=pjson`;
        try {
            const response = yield axios_1.default.get(url, { httpsAgent: constants_1.agent });
            const data = response.data;
            const ngsData = data.features.map((feature) => {
                const geometry = feature.geometry;
                const attributes = Object.assign({}, feature.attributes);
                if (attributes.ORTHO_HT && attributes.PID && attributes.VERT_DATUM === "NAVD 88") {
                    if (!geometry) {
                        console.warn('Invalid or missing geometry:', JSON.stringify(feature, null, 2));
                        return null;
                    }
                    return {
                        centroid: Object.assign(Object.assign({ PID: attributes.PID }, geometry), { z: Number(attributes.ORTHO_HT) }),
                    };
                }
                return null;
            }).filter((feature) => feature && feature.centroid);
            return { features: ngsData };
        }
        catch (error) {
            console.error('Error fetching parcel data:', error);
            if (error.response) {
                console.error('Response data:', JSON.stringify(error.response.data, null, 2));
                console.error('Response status:', error.response.status);
            }
            throw new Error('An error occurred while fetching parcel data');
        }
    });
}
;
/**
 * Parses a bounding box string into an object with xmin, ymin, xmax, ymax.
 *
 * @function parseGeometryBox
 * @memberof gisCadConnector
 * @param {string} bbox - The bounding box string in the format "xmin,ymin,xmax,ymax".
 * @returns {{ xmin: number, ymin: number, xmax: number, ymax: number }} Parsed bounding box as an object.
 * @throws Will throw an error if the bounding box string is invalid or missing.
 */
function parseGeometryBox(bbox) {
    if (!bbox) {
        throw new Error('Bounding box parameter is missing.');
    }
    const parts = bbox.split(',');
    if (parts.length !== 4) {
        throw new Error('Bounding box must contain exactly four comma-separated values.');
    }
    const [xmin, ymin, xmax, ymax] = parts.map(parseFloat);
    if ([xmin, ymin, xmax, ymax].some(isNaN)) {
        throw new Error('Invalid numeric values in bounding box.');
    }
    return { xmin, ymin, xmax, ymax };
}