gis-tools-ts - v0.6.0
    Preparing search index...

    Class BoxIndex<T>

    BoxIndex

    An Index for points and rectangles

    A really fast static spatial index for 2D points and rectangles in JavaScript. Uses either a fast simple Hilbert curve algorithm or a more complex Hilbert curve (S2) algorithm.

    This is a partial port/typescript port of the flatbush codebase.

    import { BoxIndex } from 'gis-tools-ts';
    import type { BoxIndexAccessor } from 'gis-tools-ts';

    interface Item { minX: number; minY: number; maxX: number; maxY: number }

    // define how to access the minX, minY, maxX, and maxY properties
    const accessor: BoxIndexAccessor<Item> = (item: Item) => [item.minX, item.minY, item.maxX, item.maxY];
    // create the index
    const flatbush = new BoxIndex<Item>(items, accessor);

    // make a bounding box query
    const found = index.search(minX, minY, maxX, maxY);

    // make a k-nearest-neighbors query
    const neighborIds = index.neighbors(x, y, 5);

    Type Parameters

    • T
    Index

    Constructors

    Methods

    Constructors

    • Create a BoxIndex index that will hold a given number of items.

      Type Parameters

      • T

      Parameters

      • items: T[]

        The items to index.

      • accessor: BoxIndexAccessor<T>

        A function for accessing the minX, minY, maxX, and maxY properties of the items.

      • OptionalnodeSize: number = 16

        Size of the tree node (16 by default).

      Returns BoxIndex<T>

    Methods

    • Search items in order of distance from the given point.

      Parameters

      • x: number

        The x coordinate of the query point.

      • y: number

        The y coordinate of the query point.

      • OptionalmaxResults: number = Infinity

        The maximum number of results to return.

      • OptionalmaxDistance: number = Infinity

        The maximum distance to search.

      • OptionalfilterFn: (item: T) => boolean

        An optional function for filtering the results.

      Returns T[]

      An array of indices of items found.

    • Search the index by a bounding box.

      Parameters

      • minX: number

        The minimum x coordinate of the query point.

      • minY: number

        The minimum y coordinate of the query point.

      • maxX: number

        The maximum x coordinate of the query point.

      • maxY: number

        The maximum y coordinate of the query point.

      • OptionalfilterFn: (item: T) => boolean

        An optional function that is called on every found item; if supplied, only items for which this function returns true will be included in the results array.

      Returns T[]

      An array of indices of items intersecting or touching the given bounding box.