{"id":13426376,"url":"https://github.com/mourner/flatbush","last_synced_at":"2025-05-13T16:10:58.650Z","repository":{"id":29901093,"uuid":"123101271","full_name":"mourner/flatbush","owner":"mourner","description":"A very fast static spatial index for 2D points and rectangles in JavaScript 🌱","archived":false,"fork":false,"pushed_at":"2025-03-10T11:56:28.000Z","size":184,"stargazers_count":1467,"open_issues_count":7,"forks_count":58,"subscribers_count":24,"default_branch":"main","last_synced_at":"2025-04-23T20:56:40.064Z","etag":null,"topics":["algorithm","computational-geometry","data-structures","javascript","r-tree","spatial-index"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mourner.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-27T08:57:21.000Z","updated_at":"2025-04-20T09:43:16.000Z","dependencies_parsed_at":"2023-12-15T22:00:46.884Z","dependency_job_id":"ed5027c3-80f0-4a68-a33f-d414d62dc767","html_url":"https://github.com/mourner/flatbush","commit_stats":{"total_commits":149,"total_committers":11,"mean_commits":"13.545454545454545","dds":0.08053691275167785,"last_synced_commit":"82cc07a3bf91623bc99b1b38b38fe9d52beb58e9"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mourner%2Fflatbush","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mourner%2Fflatbush/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mourner%2Fflatbush/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mourner%2Fflatbush/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mourner","download_url":"https://codeload.github.com/mourner/flatbush/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250514767,"owners_count":21443208,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","computational-geometry","data-structures","javascript","r-tree","spatial-index"],"created_at":"2024-07-31T00:01:33.077Z","updated_at":"2025-04-23T20:56:46.262Z","avatar_url":"https://github.com/mourner.png","language":"JavaScript","readme":"# Flatbush\n\nA really fast **static spatial index** for 2D points and rectangles in JavaScript.\n\nAn efficient implementation of the [packed Hilbert R-tree](https://en.wikipedia.org/wiki/Hilbert_R-tree#Packed_Hilbert_R-trees) algorithm. Enables fast spatial queries on a very large number of objects (e.g. millions), which is very useful in maps, data visualizations and computational geometry algorithms. Similar to [RBush](https://github.com/mourner/rbush), with the following key differences:\n\n- **Static**: you can't add/remove items after initial indexing.\n- **Faster** indexing and search, with much lower **memory** footprint.\n- Index is stored as a single **array buffer** (to [transfer](https://developer.mozilla.org/en-US/docs/Glossary/Transferable_objects) between threads or save as a compact binary file).\n\nSupports geographic locations with the [geoflatbush](https://github.com/mourner/geoflatbush) extension. See also: [KDBush](https://github.com/mourner/kdbush), a similar library for points.\n\n[![Build Status](https://github.com/mourner/flatbush/workflows/Node/badge.svg?branch=main)](https://github.com/mourner/flatbush/actions) [![minzipped size](https://img.shields.io/bundlephobia/minzip/flatbush)](https://esm.run/flatbush) [![Simply Awesome](https://img.shields.io/badge/simply-awesome-brightgreen.svg)](https://github.com/mourner/projects)\n\n## Usage\n\n```js\n// initialize Flatbush for 1000 items\nconst index = new Flatbush(1000);\n\n// fill it with 1000 rectangles\nfor (const p of items) {\n    index.add(p.minX, p.minY, p.maxX, p.maxY);\n}\n\n// perform the indexing\nindex.finish();\n\n// make a bounding box query\nconst found = index.search(minX, minY, maxX, maxY).map((i) =\u003e items[i]);\n\n// make a k-nearest-neighbors query\nconst neighborIds = index.neighbors(x, y, 5);\n\n// instantly transfer the index from a worker to the main thread\npostMessage(index.data, [index.data]);\n\n// reconstruct the index from a raw array buffer\nconst index = Flatbush.from(e.data);\n\n```\n\n## Install\n\nInstall with NPM: `npm install flatbush`, then import as a module:\n\n```js\nimport Flatbush from 'flatbush';\n```\n\nOr use as a module directly in the browser with [jsDelivr](https://www.jsdelivr.com/esm):\n\n```html\n\u003cscript type=\"module\"\u003e\n    import Flatbush from 'https://cdn.jsdelivr.net/npm/flatbush/+esm';\n\u003c/script\u003e\n```\n\nAlternatively, there's a browser bundle with a `Flatbush` global variable:\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/flatbush\"\u003e\u003c/script\u003e\n```\n\n## API\n\n#### `new Flatbush(numItems[, nodeSize, ArrayType, ArrayBufferType])`\n\nCreates a Flatbush index that will hold a given number of items (`numItems`). Additionally accepts:\n\n- `nodeSize`: size of the tree node (`16` by default); experiment with different values for best performance (increasing this value makes indexing faster and queries slower, and vise versa).\n- `ArrayType`: the array type used for coordinates storage (`Float64Array` by default);\nother types may be faster in certain cases (e.g. `Int32Array` when your data is integer).\n- `ArrayBufferType`: the array buffer type used to store data (`ArrayBuffer` by default);\nyou may prefer `SharedArrayBuffer` if you want to share the index between threads (multiple `Worker`, `SharedWorker` or `ServiceWorker`).\n\n#### `index.add(minX, minY[, maxX, maxY])`\n\nAdds a given rectangle to the index. Returns a zero-based, incremental number that represents the newly added rectangle.\nIf not provided, `maxX` and `maxY` default to `minX` and `minY` (essentially adding a point).\n\n#### `index.finish()`\n\nPerforms indexing of the added rectangles.\nTheir number must match the one provided when creating a `Flatbush` object.\n\n#### `index.search(minX, minY, maxX, maxY[, filterFn])`\n\nReturns an array of indices of items intersecting or touching a given bounding box. Item indices refer to the value returned by [`index.add()`](#indexaddminx-miny-maxx-maxy).\n\n```js\nconst ids = index.search(10, 10, 20, 20);\n```\n\nIf given a `filterFn`, calls it on every found item (passing an item index)\nand only includes it if the function returned a truthy value.\n\n```js\nconst ids = index.search(10, 10, 20, 20, (i) =\u003e items[i].foo === 'bar');\n```\n\n#### `index.neighbors(x, y[, maxResults, maxDistance, filterFn])`\n\nReturns an array of item indices in order of distance from the given `x, y`\n(known as K nearest neighbors, or KNN). Item indices refer to the value returned by [`index.add()`](#indexaddminx-miny-maxx-maxy).\n\n```js\nconst ids = index.neighbors(10, 10, 5); // returns 5 ids\n```\n\n`maxResults` and `maxDistance` are `Infinity` by default.\nAlso accepts a `filterFn` similar to `index.search`.\n\n#### `Flatbush.from(data[, byteOffset])`\n\nRecreates a Flatbush index from raw `ArrayBuffer` or `SharedArrayBuffer` data\n(that's exposed as `index.data` on a previously indexed Flatbush instance).\nVery useful for transferring or sharing indices between threads or storing them in a file.\n\n### Properties\n\n- `data`: array buffer that holds the index.\n- `minX`, `minY`, `maxX`, `maxY`: bounding box of the data.\n- `numItems`: number of stored items.\n- `nodeSize`: number of items in a node tree.\n- `ArrayType`: array type used for internal coordinates storage.\n- `IndexArrayType`: array type used for internal item indices storage.\n\n## Performance\n\nRunning `node bench.js` with Node v14:\n\nbench | flatbush | rbush\n--- | --- | ---\nindex 1,000,000 rectangles | 273ms | 1143ms\n1000 searches 10% | 575ms | 781ms\n1000 searches 1% | 63ms | 155ms\n1000 searches 0.01% | 6ms | 17ms\n1000 searches of 100 neighbors | 24ms | 43ms\n1 search of 1,000,000 neighbors | 133ms | 280ms\n100,000 searches of 1 neighbor | 710ms | 1170ms\n\n## Ports\n\n- [chusitoo/flatbush](https://github.com/chusitoo/flatbush) (C++ port)\n- [jbuckmccready/static_aabb2d_index](https://github.com/jbuckmccready/static_aabb2d_index) (Rust port)\n- [jbuckmccready/Flatbush](https://github.com/jbuckmccready/Flatbush) (C# port)\n- [bmharper/flatbush-python](https://github.com/bmharper/flatbush-python) (Python port)\n- [FlatGeobuf](https://github.com/flatgeobuf/flatgeobuf) (a geospatial format inspired by Flatbush)\n- [IMQS/flatbush](https://github.com/IMQS/flatbush) (C++ port, no longer maintained)\n- [msfstef/flatbush-dart](https://github.com/msfstef/flatbush-dart) (Dart port)\n- [kylebarron/geo-index](https://github.com/kylebarron/geo-index) (Rust port and Python bindings, with ABI compatibility to this library)\n- [kylebarron/literate-flatbush](https://github.com/kylebarron/literate-flatbush) ([\"literate\"](https://en.wikipedia.org/wiki/Literate_programming) JS port that documents the internal algorithm)\n","funding_links":[],"categories":["JavaScript","👨‍💻 JavaScript Libraries"],"sub_categories":["Data Processing"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmourner%2Fflatbush","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmourner%2Fflatbush","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmourner%2Fflatbush/lists"}