fetch/fetch-flood-by-post.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 __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.fetchFloodByPost = void 0;
const axios_1 = __importDefault(require("axios"));
const turf = __importStar(require("@turf/turf"));
const calculate_centroid_1 = require("../utils/polyline/calculate-centroid");
const fetch_parcel_by_pin_1 = require("./fetch-parcel-by-pin");
const constants_1 = require("../utils/constants/constants");
const geometry_helpers_1 = require("../utils/geometry/geometry-helpers");
/**
 * @namespace fetch
 * @description Module for fetching and processing FEMA flood data.
 */
/**
 * Fetches FEMA flood data based on user-provided geometry and additional parameters.
 *
 * @memberof fetch
 * @name floodFetch
 * @async
 * @function
 * @param {string} county - The name of the county for which data is fetched.
 * @param {string} urlString - The URL endpoint for flood data queries.
 * @param {string} spatialReference - The spatial reference system (e.g., EPSG code).
 * @param {object} geometry - Geometry object representing the area of interest.
 * @param {SchemaKeys} schema - The schema defining attributes to extract.
 * @param {string} layer - The layer name for categorization.
 * @returns {Promise<object>} A promise resolving to an object containing features with geometry intersections and attributes.
 * @throws {Error} Throws an error if data fetching or processing fails.
 * @example
 * const data = await fetchFloodByPost(
 *   "Horry",
 *   "https://example.com/floodData",
 *   "4326",
 *   { rings: [[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]] },
 *   schemaKeys,
 *   "floodLayer"
 * );
 * console.log(data.features);
 */
const fetchFloodByPost = (county, urlString, spatialReference, geometry, schema, layer) => __awaiter(void 0, void 0, void 0, function* () {
    // Convert geometry to a JSON string
    let panel = "";
    let effectiveDate = "";
    const dataParam = new URLSearchParams({
        outFields: "*",
        geometryType: "esriGeometryPolygon",
        geometry: JSON.stringify(geometry), // Stringify the geometry object
        inSR: spatialReference,
        spatialRel: "esriSpatialRelIndexIntersects",
        outSR: spatialReference,
        f: "pjson"
    });
    const firmPanelUrl = 'https://hazards.fema.gov/arcgis/rest/services/public/NFHL/MapServer/3/query';
    let panelCount = 0;
    try {
        const response = yield axios_1.default.post(firmPanelUrl, dataParam.toString(), {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            httpsAgent: constants_1.agent
        });
        const data = response.data;
        panelCount = data.features.length;
        console.log("Panel count:", panelCount);
        if (data.features && panelCount === 0) {
            console.warn("No firm panel data found.");
        }
        if (data.features && panelCount === 1) {
            panel = `${data.features[0].attributes.DFIRM_ID} ${data.features[0].attributes.PANEL} ${data.features[0].attributes.SUFFIX}`;
            effectiveDate = (0, fetch_parcel_by_pin_1.formatTimestampToDate)(data.features[0].attributes.EFF_DATE);
        }
    }
    catch (error) {
        console.error("Error fetching firm panel data:", error);
        throw new Error("An error occurred while fetching firm panel data");
    }
    try {
        const response = yield axios_1.default.post(urlString, dataParam.toString(), {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            httpsAgent: constants_1.agent
        });
        const data = response.data;
        if (!data.features) {
            throw new Error(`No features returned in response: ${JSON.stringify(data, null, 2)}`);
        }
        const poly1 = turf.polygon(geometry.rings);
        const clippedFeatures = (yield Promise.all(data.features.map((feature) => __awaiter(void 0, void 0, void 0, function* () {
            const geometry = feature.geometry;
            if (!geometry || (!geometry.rings && !geometry.paths)) {
                console.warn('Invalid or missing geometry:', JSON.stringify(feature, null, 2));
                return null;
            }
            // Handle the case where geometry.rings is present
            if (geometry.rings) {
                const poly2 = turf.polygon(geometry.rings);
                const fc = turf.featureCollection([poly1, poly2]);
                // Ensure you pass two valid Feature<Polygon> objects
                if (poly1 && poly2) {
                    try {
                        const intersection = turf.intersect(fc);
                        if (intersection) {
                        }
                        else {
                            return null;
                        }
                        const flattenedIntersection = (0, geometry_helpers_1.flattenGeometryRings)(intersection.geometry);
                        const intersectionCoordinates = flattenedIntersection.coordinates;
                        let centroid = {
                            x: 0,
                            y: 0
                        };
                        if (intersectionCoordinates.length > 0) {
                            const polylineRings = (0, calculate_centroid_1.convertRingsToPolyline)(intersectionCoordinates);
                            const centroidXY = (0, calculate_centroid_1.calculateCentroid)(polylineRings);
                            if (centroidXY) {
                                centroid = centroidXY;
                            }
                        }
                        if (flattenedIntersection) {
                            // Fetch firm panel data for each intersection
                            // If multiple firm panels are found, it updates the panel and effective date by making a post request using the centroid of the intersection to determine the firm panel
                            if (panelCount > 1) {
                                const pointX = centroid.x;
                                const pointY = centroid.y;
                                const panelDataParam = new URLSearchParams({
                                    outFields: "*",
                                    geometryType: "esriGeometryPoint",
                                    geometry: `${pointX},${pointY}`,
                                    inSR: spatialReference,
                                    spatialRel: "esriSpatialRelWithin",
                                    outSR: spatialReference,
                                    returnGeometry: "false",
                                    f: "pjson"
                                });
                                const response = yield axios_1.default.post(firmPanelUrl, panelDataParam.toString(), {
                                    headers: {
                                        "Content-Type": "application/x-www-form-urlencoded",
                                    },
                                    httpsAgent: constants_1.agent
                                });
                                const data = response.data;
                                if (data.features && data.features[0]) {
                                    const attributes = data.features[0].attributes;
                                    panel = `${attributes.DFIRM_ID} ${attributes.PANEL} ${attributes.SUFFIX}`;
                                    effectiveDate = (0, fetch_parcel_by_pin_1.formatTimestampToDate)(attributes.EFF_DATE);
                                }
                            }
                            if (flattenedIntersection.coordinates.length > 1) {
                                const multipleArr = flattenedIntersection.coordinates.map((coordinate) => ({
                                    type: 'flood',
                                    layer,
                                    attributes: {
                                        FLD_ZONE: formatFemaFloodZoneLabels(feature.attributes).FLD_ZONE,
                                        PANEL: panel,
                                        EFF_DATE: effectiveDate
                                    },
                                    centroid: (function () {
                                        if (coordinate) {
                                            const polyline = (0, calculate_centroid_1.convertRingsToPolyline)([coordinate]);
                                            const centroidXY = (0, calculate_centroid_1.calculateCentroid)(polyline);
                                            if (centroidXY) {
                                                centroid = centroidXY;
                                            }
                                            return centroid;
                                        }
                                        return centroid;
                                    })(),
                                    geometry: {
                                        rings: [coordinate]
                                    }
                                }));
                                return [...multipleArr];
                            }
                            else {
                                return {
                                    type: 'flood',
                                    layer,
                                    attributes: {
                                        FLD_ZONE: formatFemaFloodZoneLabels(feature.attributes).FLD_ZONE,
                                        PANEL: panel,
                                        EFF_DATE: effectiveDate
                                    },
                                    centroid,
                                    geometry: {
                                        rings: flattenedIntersection.coordinates
                                    }
                                };
                            }
                        }
                        else {
                            console.warn('No intersection found between the polygons.');
                        }
                    }
                    catch (error) {
                        if (error instanceof Error) {
                            console.error('Error during intersection:', error.message);
                        }
                        else {
                            console.error('Error during intersection:', error);
                        }
                    }
                }
                else {
                    console.error('Invalid polygons. Cannot perform intersection.');
                }
            }
            return null;
        })))).filter((feature) => feature);
        return { features: clippedFeatures.flat() };
    }
    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');
    }
});
exports.fetchFloodByPost = fetchFloodByPost;
/**
* Formats FEMA flood zone labels based on attributes.
*
* @memberof fetch
* @function formatFemaFloodZoneLabels
* @param {object} attributes - Attributes containing flood zone information.
* @returns {{ FLD_ZONE: string }} An object with the formatted flood zone label.
* @example
* const attributes = {
*   FLD_ZONE: "X",
*   ZONE_SUBTY: "0.2 PCT ANNUAL CHANCE FLOOD HAZARD",
*   STATIC_BFE: -9999
* };
* const label = formatFemaFloodZoneLabels(attributes);
* console.log(label.FLD_ZONE); // "X-SHADED"
*/
function formatFemaFloodZoneLabels(attributes) {
    let FLD_ZONE = attributes.FLD_ZONE;
    let ZONE_SUBTY = attributes.ZONE_SUBTY;
    let STATIC_BFE = attributes.STATIC_BFE;
    if (FLD_ZONE === 'X' && ZONE_SUBTY === '0.2 PCT ANNUAL CHANCE FLOOD HAZARD' && STATIC_BFE === -9999) {
        FLD_ZONE = 'X-SHADED';
    }
    else if (FLD_ZONE && STATIC_BFE && STATIC_BFE !== -9999) {
        FLD_ZONE = `${FLD_ZONE}-${STATIC_BFE}`;
    }
    else {
        FLD_ZONE = 'X';
    }
    return { FLD_ZONE };
}