"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.fetchParcelByPolygon = void 0;
const axios_1 = __importDefault(require("axios"));
const generate_attributes_1 = require("../utils/attributes/generate-attributes");
const calculate_centroid_1 = require("../utils/polyline/calculate-centroid");
const constants_1 = require("../utils/constants/constants");
/**
* @namespace fetch
* @description Fetch utilities for handling parcel data queries and geometry operations.
*/
/**
* Fetches parcel data based on a user-provided bounding box geometry.
* Converts the geometry into a queryable format and processes the response to extract parcel features, centroids, and attributes.
*
* @memberof fetch
* @async
* @function fetchParcelByPolygon
* @param {string} county - The name of the county for which data is being fetched.
* @param {string} urlString - The URL endpoint for querying parcel data.
* @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 keys for extracting parcel attributes.
* @param {string} layer - The layer name for categorizing the returned features.
* @returns {Promise<FetchParcelResult>} A promise resolving to an object containing processed parcel features.
* @throws {Error} Throws an error if data fetching or processing fails.
* @example
* const result = await fetchParcelByPolygon(
* "Horry",
* "https://example.com/api/query",
* "4326",
* { rings: [[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]] },
* schemaKeys,
* "parcels"
* );
* console.log(result.features);
*/
const fetchParcelByPolygon = (county_1, urlString_1, spatialReference_1, geometry_1, schema_1, layer_1, ...args_1) => __awaiter(void 0, [county_1, urlString_1, spatialReference_1, geometry_1, schema_1, layer_1, ...args_1], void 0, function* (county, urlString, spatialReference, geometry, schema, layer, type = 'parcel') {
const dataParam = new URLSearchParams({
outFields: "*",
geometryType: "esriGeometryPolygon",
geometry: JSON.stringify(geometry), // Stringify the geometry object
inSR: spatialReference,
spatialRel: "esriSpatialRelTouches",
outSR: spatialReference,
f: "pjson"
});
console.log(dataParam.toString()); // Log the query parameters for debugging
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 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;
}
// Process geometry with rings (polygons)
if (geometry.rings) {
const polyline = (0, calculate_centroid_1.convertRingsToPolyline)(geometry.rings);
const centroid = (0, calculate_centroid_1.calculateCentroid)(polyline);
return {
type: type,
layer,
attributes,
centroid,
geometry,
};
}
// Process geometry with paths (polylines)
if (geometry.paths) {
const polyline = (0, calculate_centroid_1.convertRingsToPolyline)(geometry.paths);
const centroid = (0, calculate_centroid_1.calculateCentroid)(polyline);
return {
type: type,
layer,
attributes,
centroid,
geometry: Object.assign(Object.assign({}, geometry), { paths: geometry.paths }),
};
}
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');
}
});
exports.fetchParcelByPolygon = fetchParcelByPolygon;