Skip to content

On disk rtree implementation based on rbush designed to create linked data fragment trees

License

Notifications You must be signed in to change notification settings

amohoste/rtree-ldf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 = require('rtree-ldf')

Creating a Tree

const tree = new Rtree({
	dir: './db',
	openExisting: true,
	cacheSize: 100000,
	maxEntries: 16,
});
  • dir: Directory where the tree will be saved on disk
  • openExisting (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.insert(item);

minX, minY, maxX and maxY are required. You can also add extra data properties.

Removing Data

Remove a previously inserted item:

tree.remove(item);

You can also pass a custom equals function.

tree.remove(itemCopy, function (a, b) {
    return a.id === b.id;
});

Remove all items:

tree.clear();

Bulk-Inserting Data

Load an array of data into the tree.

tree.load([item1, item2, ...]);

Search

var result = tree.search({
    minX: 40,
    minY: 20,
    maxX: 80,
    maxY: 70
});

Returns an array of data items (points or rectangles) that the given bounding box intersects.

var allItems = tree.all();

Returns all items of the tree.

Collisions

var result = tree.collides({minX: 40, minY: 20, maxX: 80, maxY: 70});

Returns true if there are any items intersecting the given bounding box, otherwise false.

Export to JSON

// export data as JSON object
var treeData = tree.toJSON();

Export to linked data fragments

tree.toFragments({
	outDir: './fragments/', 
	treeDir: 'tree', 
	dataDir: 'data', 
	collection: 'stations' , 
	manages: 'http://vocab.gtfs.org/terms#station'
});

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 to
  • collection: name of the fragment describing the collection. This fragment will be placed in the out directory
  • manages: type of data the collection manages
  • treeDir (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.

About

On disk rtree implementation based on rbush designed to create linked data fragment trees

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published