Rtree LDF
Rtree-ldf is based on rbush, a high-performance JavaScript library for 2D spatial indexing of points and rectangles. It is completely disk based and uses a nosql-database for storage and a lru-cache for improved performance. Please note that this implementation isn't optimal and an implementation in another language such as c++ will be much faster. Performance will also greatly depend on the cache size given to the tree.
Install
Install with NPM (npm install --save rtree-ldf
).
Usage
const Rtree =
Creating a Tree
const tree = dir: './db' openExisting: true cacheSize: 100000 maxEntries: 16;
dir
: Directory where the tree will be saved on diskopenExisting
(Opt.): Open an existing tree located in dir (default: false)cacheSize
(Opt.): Amount of nodes that will can be cached (max 1.000.000, default: 100.000)maxEntries
(Opt.): defines the maximum number of entries in a tree node (default: 9)
Closing a tree
If you want to make sure your tree is completely saved to the disk, make sure to call tree.close()
when you are done.
Adding Data
Insert an item:
const item = minX: 20 minY: 40 maxX: 30 maxY: 50 "@id": "gtfs:station" foo: bar;tree;
minX
, minY
, maxX
and maxY
are required. You can also add extra data properties.
Removing Data
Remove a previously inserted item:
tree;
You can also pass a custom equals
function.
tree;
Remove all items:
treeclear;
Bulk-Inserting Data
Load an array of data into the tree.
tree;
Search
var result = tree;
Returns an array of data items (points or rectangles) that the given bounding box intersects.
var allItems = treeall;
Returns all items of the tree.
Collisions
var result = tree;
Returns true
if there are any items intersecting the given bounding box, otherwise false
.
Export to JSON
// export data as JSON objectvar treeData = tree;
Export to linked data fragments
tree;
The tree will be exported into fragments conform to the TreeOntology. The fragments are formatted in JSON-LD and most will be around 500 kB which should give a fragment size of around 50 kB after compression.
outDir
: base directory where the fragments will be exported tocollection
: name of the fragment describing the collection. This fragment will be placed in the out directorymanages
: type of data the collection managestreeDir
(opt.): directory starting from outDir where the treeFragments will be exported to. The fragments are exported to the outDir by default.dataDir
(opt.): directory starting from outDir where the dataFragments will be exported to. The fragments are exported to the outDir by default.