mourner / rbush

RBush — a high-performance JavaScript R-tree-based 2D spatial index for points and rectangles

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to search by id ?

hoogw opened this issue · comments

commented

How to return item id?

For example I insert 2 item, with id field 1, 2
`const item = {
minX: 20,
minY: 40,
maxX: 30,
maxY: 50,
id: 1
};

const item2= {
minX: 10,
minY: 20,
maxX: 20,
maxY: 30,
id: 2
};

tree.insert(item);
tree.insert(item2);`

Now I want the result return id, so I can use this id to link to other resources

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

Assuming the result have 1 record, it is item2
would the result have full dataset of item2, include "id:2"?

I have 500k record, if only return the reference id:2 will be enough,
return minX....MaxY will be extra memory cost, will kill my browser.

How to config it only return reference id field? Not the other minX, .....MaxY?

If you want to search by ID, you should be looking for a hashmap and not an R-tree. If you are worried about four properties being a memory hog, you are forgetting about how references to variables work and you're probably doing premature optimization (and you shouldn't). If you want to return only a property of the item, then map the array returned by a rbush search with something like function (item) {return item.id;}.

commented

If you want to search by ID, you should be looking for a hashmap and not an R-tree. If you are worried about four properties being a memory hog, you are forgetting about how references to variables work and you're probably doing premature optimization (and you shouldn't). If you want to return only a property of the item, then map the array returned by a rbush search with something like function (item) {return item.id;}.

ok, function (item) {return item.id;} will works for me.