UNPKG

@ahamove/polygon-lookup

Version:

A fork from pelias/polygon-lookup with updated dependencies

130 lines 4.93 kB
/** * Exports a `PolygonLookup` class, which constructs a data-structure for * quickly finding the polygon that a point intersects in a (potentially very * large) set. */ import type { Feature, FeatureCollection, MultiPolygon, Polygon } from "geojson"; import type { PolygonLookupOptions } from "./types.js"; declare class PolygonLookup { /** * The type of spatial index being used. */ private indexType; /** * Optional custom node size for the spatial index. */ private nodeSize?; /** * R-tree spatial index containing bounding boxes with polygon references. * Used when indexType is 'rbush'. */ private rtree; /** * Flatbush spatial index. * Used when indexType is 'flatbush'. */ private flatIndex; /** * Array of bounding box data for Flatbush index lookup. * Flatbush returns indices, so we need to store bbox objects separately. */ private bboxData; /** * Array of indexed GeoJSON polygon features. */ polygons: Array<Feature<Polygon>>; /** * Create a new PolygonLookup instance. * * @param featureCollection - Optional GeoJSON FeatureCollection to index immediately * @param options - Configuration options * @param options.indexType - Spatial index backend ('rbush' or 'flatbush') * @param options.nodeSize - Node size for spatial index (affects performance) * @throws Error if featureCollection is invalid * * @example * // Default: RBush backend * const lookup = new PolygonLookup(geojson); * * @example * // Performance mode: Flatbush backend * const lookup = new PolygonLookup(geojson, { indexType: 'flatbush' }); */ constructor(featureCollection?: FeatureCollection<Polygon | MultiPolygon>, options?: PolygonLookupOptions); /** * Build RBush spatial index from bounding boxes. * @private * @param bboxes - Array of bounding boxes with polyId references */ private buildRbushIndex; /** * Build Flatbush spatial index from bounding boxes. * @private * @param bboxes - Array of bounding boxes with polyId references */ private buildFlatbushIndex; /** * Search for bounding boxes using RBush index. * @private * @param x - The x-coordinate to search for * @param y - The y-coordinate to search for * @returns Array of bounding boxes that may contain the point */ private searchRbush; /** * Search for bounding boxes using Flatbush index. * @private * @param x - The x-coordinate to search for * @param y - The y-coordinate to search for * @returns Array of bounding boxes that may contain the point */ private searchFlatbush; /** * Internal helper method to return a single matching polygon. * @private * @param x - The x-coordinate to search for. * @param y - The y-coordinate to search for. * @returns The first polygon that intersects (x, y), or undefined if none found. */ private searchForOnePolygon; /** * Internal helper method to return multiple matching polygons, up to a given limit. * @private * @param x - The x-coordinate to search for. * @param y - The y-coordinate to search for. * @param limit - Maximum number of results to return. Use -1 for unlimited. * @returns A GeoJSON FeatureCollection containing matching polygons (up to limit). */ private searchForMultiplePolygons; /** * Find the first polygon that contains the given point. * * @param x - The x-coordinate (longitude) * @param y - The y-coordinate (latitude) * @returns The first intersecting polygon feature, or undefined if none found */ search(x: number, y: number): Feature<Polygon> | undefined; /** * Find multiple polygons that contain the given point, up to a specified limit. * * @param x - The x-coordinate (longitude) * @param y - The y-coordinate (latitude) * @param limit - Maximum number of results to return. Use -1 for all results. * @returns FeatureCollection containing matching polygon features (up to limit) */ search(x: number, y: number, limit: number): FeatureCollection<Polygon>; /** * Build spatial index from a GeoJSON FeatureCollection. * * MultiPolygons are automatically expanded into individual Polygons. * Properly handles polygons with holes. * * @param collection - GeoJSON FeatureCollection with Polygon or MultiPolygon features * @throws Error if collection is null, undefined, or missing features property */ loadFeatureCollection(collection: FeatureCollection<Polygon | MultiPolygon>): void; } export default PolygonLookup; export type { BoundingBox } from "./types.js"; export { getBoundingBox } from "./utils.js"; //# sourceMappingURL=index.d.ts.map