fetch/fetch-parcel-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.fetchParcelDataByBbox = fetchParcelDataByBbox;
exports.parseGeometryBox = parseGeometryBox;
const axios_1 = __importDefault(require("axios"));
const polyline_utils_1 = require("../utils/polyline/polyline-utils");
const calculate_centroid_1 = require("../utils/polyline/calculate-centroid");
const generate_attributes_1 = require("../utils/attributes/generate-attributes");
const constants_1 = require("../utils/constants/constants");
/**
 * @namespace fetch
 * @description Utilities for fetching and processing parcel data.
 */
/**
 * Fetches parcel data based on a user-provided bounding box and spatial reference.
 * Clips the parcel geometry to the specified bounding box and generates attributes for each parcel.
 *
 * @memberof fetch
 * @export
 * @async
 * @function fetchParcelDataByBbox
 * @param {number} xmin - The minimum x-coordinate of the bounding box.
 * @param {number} ymin - The minimum y-coordinate of the bounding box.
 * @param {number} xmax - The maximum x-coordinate of the bounding box.
 * @param {number} ymax - The maximum y-coordinate of the bounding box.
 * @param {string} urlString - The URL endpoint for querying parcel data.
 * @param {string} spatialReference - The spatial reference system (e.g., EPSG code).
 * @param {string} county - The county name for which data is being fetched.
 * @param {SchemaKeys} schema - Schema object defining attributes for parcel data.
 * @param {string} type - The type of data being fetched (e.g., "parcel").
 * @param {string} layer - The layer name for categorizing the data.
 * @returns {Promise<object>} A promise resolving to an object containing clipped parcel features.
 * @throws {Error} Throws an error if data fetching or processing fails.
 * @example
 * const parcels = await fetchParcelDataByBbox(
 *   -80.0,
 *   30.0,
 *   -79.0,
 *   31.0,
 *   "https://example.com/api",
 *   "4326",
 *   "Horry",
 *   schemaKeys,
 *   "parcel",
 *   "land"
 * );
 * console.log(parcels.features);
 */
function fetchParcelDataByBbox(xmin, ymin, xmax, ymax, urlString, spatialReference, county, schema, type, layer) {
    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 bbox = [xmin, ymin, xmax, ymax];
            const clippedFeatures = data.features.map((feature) => {
                const geometry = feature.geometry;
                const inAttributes = Object.assign({}, feature.attributes);
                const { attributes } = (0, generate_attributes_1.generateAttributes)(inAttributes, schema, spatialReference, county);
                if (!geometry || (!geometry.rings && !geometry.paths)) {
                    console.warn('Invalid or missing geometry:', JSON.stringify(feature, null, 2));
                    return null;
                }
                if (geometry.rings) {
                    const polyline = (0, calculate_centroid_1.convertRingsToPolyline)(geometry.rings);
                    const centroid = (0, calculate_centroid_1.calculateCentroid)(polyline);
                    const filteredRings = geometry.rings.map((ring) => (0, polyline_utils_1.splitPathAtBbox)(ring, bbox)).flat().filter((ring) => ring.length > 0);
                    if (filteredRings.length > 0) {
                        return {
                            type,
                            layer,
                            attributes,
                            centroid,
                            geometry: Object.assign(Object.assign({}, geometry), { rings: filteredRings }),
                        };
                    }
                }
                if (geometry.paths) {
                    const polyline = (0, calculate_centroid_1.convertRingsToPolyline)(geometry.paths);
                    const centroid = (0, calculate_centroid_1.calculateCentroid)(polyline);
                    const filteredPaths = geometry.paths.map((path) => (0, polyline_utils_1.splitPathAtBbox)(path, bbox)).flat().filter((path) => path.length > 0);
                    if (filteredPaths.length > 0) {
                        return {
                            type,
                            layer,
                            attributes,
                            centroid,
                            geometry: Object.assign(Object.assign({}, geometry), { paths: filteredPaths }),
                        };
                    }
                }
                return null;
            }).filter((feature) => { var _a, _b; return feature && feature.geometry && (((_a = feature.geometry.rings) === null || _a === void 0 ? void 0 : _a.length) > 0 || ((_b = feature.geometry.paths) === null || _b === void 0 ? void 0 : _b.length) > 0); });
            return { features: clippedFeatures };
        }
        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 coordinates.
 *
 * @memberof fetch
 * @export
 * @function parseGeometryBox
 * @param {string} bbox - The bounding box string in the format "xmin,ymin,xmax,ymax".
 * @returns {{ xmin: number, ymin: number, xmax: number, ymax: number }} An object with numeric coordinates for the bounding box.
 * @throws {Error} Throws an error if the bounding box string is invalid.
 * @example
 * const bbox = parseGeometryBox("-80.0,30.0,-79.0,31.0");
 * console.log(bbox); // { xmin: -80.0, ymin: 30.0, xmax: -79.0, ymax: 31.0 }
 */
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 };
}