Coder Social home page Coder Social logo

rtree's Introduction

#rTree Build Status

A non-recursive R-Tree library in pure JavaScript with no dependencies. Fork of Jon-Carlos Rivera's fantastic library which sadly seems not to be maintained. MIT Licensed.

##So far:

  • Bug fix when deleting points.
  • Common.js module.
  • Updated tests.
  • Factory function for constructor.
  • Method for dealing with GeoJSON.
  • All methods now accept callbacks.
  • Query by bbox instead of rectangle.
  • Submit to NPM.
  • Update examples.
  • add closure
  • add GruntFile
  • fix syntax (make it pass jslint)
  • more modular
  • that bug with deleting

##API

  • RTree ( [ Number max_node_width, Function callback ] )

###Parameters:

  • max_node_width : optional : The maximum width of a node before a split is performed1.

###Returns:

  • An empty rTree object.

###Usage:

  • Make a new rTree with a max node width of 10:
  • var myRTree = RTree(10);

##rTree.insert

  • rTree.insert ( Rectangle3 bounds, Object element)

###Parameters:

  • bounds : required : A minimally bounding box for element.
  • element : required : An object to add to the R-Tree.

###Returns:

  • True.

###Usage:

  • Insert a 10x10 object that starts at position 10x10:
  • myRTree.insert({x:10, y:10, w:10, h:10}, myObject);

##rTree.remove

  • rTree.remove ( Rectangle3 area _[, Object element)

###Parameters:

  • area : required : An area to search within.
  • element : optional : An object to remove from the R-Tree. If no object is specified, all elements that touch area are deleted.

###Returns:

  • An array of leafs deleted from the R-Tree.

###Usage:

  • Deletes all object that touch the 10x10 rectangle starting at position 10x10:
  • var myDelCount = myRTree.delete({x:10, y:10, w:10, h:10});
  • Delete only specific object if it touches the 10x10 rectangle starting at position 10x10:
  • var myDelCount = myRTree.delete({x:10, y:10, w:10, h:10}, specific_object);

##rTree.geoJSON:

  • rTree.geoJSON ( Object or Array geoJSON)

###Parameters

  • geoJSON : required : Either an Object representing a GeoJSON feature collection or an Array representing a list of GeoJSON features.

###Usage:

myRTree.geoJSON({
	"type":"FeatureCollection",
	"features":[
		{
			"type":"Feature",
			"geometry":{
				"type":"Point",
				"coordinates":[100,1]
			},
			"properties":{
				"prop0":"value0"
			}
		},
		{
			"type":"Feature",
			"geometry":{
				"type":"LineString",
				"coordinates":[
					[100,0],
					[101,1]
				]
			},
			"properties":{
				"prop0":"value0"
			}
		}
	]
});

##rTree.bbox:

  • rTree.bbox ( Bounds area)

###Parameters

  • area : required : Area to search, this can either be represented by a single parameter bounds array [[x1,y1],[x2,y2]], two parameters representing the southwest and northeast corners [x1,y1],[x2,y2], or 4 parameters of [x1,y1,x2,y2].

###Returns:

  • An array of matched features.

###Usage:

  • Search a 10x10 area that starts at position 10x10 (these are all equivalent):
  • var myObjects1 = myRTree.bbox([[10,10],[20,20]]);
  • var myObjects2 = myRTree.bbox([[10,10],[20,20]]);
  • var myObjects3 = myRTree.bbox([10,10],[20,20]);
  • var myObjects4 = myRTree.bbox([10,10],[20,20]);
  • var myObjects5 = myRTree.bbox(10,10,20,20);
  • var myObjects6 = myRTree.bbox(10,10,20,20);

##rTree.search

  • RTree.search ( Rectangle3 area [, Boolean return node, Array return_array ])

###Parameters:

  • area : required : An area to search within.
  • return node : optional : Whether to return the entire node, mainly internal option.
  • return array : optional : An existing array to add the results to, defaults to [], mainly internal option.

###Returns:

  • An array of objects that overlap or touch area.

###Usage:

  • Search a 10x10 area that starts at position 10x10:
  • var myObjects = myRTree.search({x:10, y:10, w:10, h:10});

###Notes

1 Default max node width is currently 6.

3 A Rectangle is any object with public x, y, w, h properties. The object itself is not saved or used directly but copies are made of its x, y, w, h properties.

rtree's People

Contributors

calvinmetcalf avatar imbcmdth avatar mtoonen avatar peterdavehello avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rtree's Issues

Comparison with rbush?

The README.md says that is is a port of Jon-Carlos Rivera's library. We used Jon-Carlos's RTree library in OpenLayers 3, but found that @mourner's rbush was much, much better. Compare the two screenshots below: the first is RTree, the second is rbush:
rtree
rbush
If this library is based on RTree, you may wish to consider switching to rbush.

more tests

need tests for rTree.geoJSON and rTree.bbox

Floating point precision issues when searching for exact points

Hi there, so I've run into an issue with floating point precision when searching for exact points in an rtree. For a demonstration of this bug, grab my test case located on my fork here and run it a few times on the current build. You'll get some output similar to this:

$ node searchexact.test.js
TAP version 13
# RTree Searching for exact point
not ok 1 1k In-Bounds Searches
  ---
    operator: equal
    expected: 1000
    actual:   997
    at: Test.<anonymous> (/home/centos/proj/rtree/RTree/test/searchexact.test.js:23:5)
  ...

1..1
# tests 1
# pass  0
# fail  1

$ node searchexact.test.js
TAP version 13
# RTree Searching for exact point
not ok 1 1k In-Bounds Searches
  ---
    operator: equal
    expected: 1000
    actual:   997
    at: Test.<anonymous> (/home/centos/proj/rtree/RTree/test/searchexact.test.js:23:5)
  ...

1..1
# tests 1
# pass  0
# fail  1

$ node searchexact.test.js
TAP version 13
# RTree Searching for exact point
not ok 1 1k In-Bounds Searches
  ---
    operator: equal
    expected: 1000
    actual:   998
    at: Test.<anonymous> (/home/centos/proj/rtree/RTree/test/searchexact.test.js:23:5)
  ...

1..1
# tests 1
# pass  0
# fail  1

I've created a patch and submitted a PR (#34) for review. My solution was to introduce an epsilon and to check bounds relative to this epsilon.

Node tests

Should probably have a version of the tests that runs in node.

First Insertion

I've been tinkering with this module. In my tests this happens:

  1. insert initial node
  2. insert more nodes
  3. perform searches for those nodes

Any nodes inserted after the first will not be revealed in a search if they do not intersect with the initial rectangle. The initial insert is the root bounding box. Is this by design?

IE7/8 support

I noticed references to JSON.parse and JSON.stringify, which are not available on IE7 and 8. Which kind of browsers do you intent to support?

Pass GeoJSON feature to rtree.geoJSON fails

When passing a single geojson feature to the rtree, it fails with the message "this isn't what we're looking for".
I propose a patch which checks if 'prelim' is an Object. If so, we expect it to be a single feature, and then we put it in an Array, so it fits in the rest of the code nicely. Would that be acceptable? If so, how to clone (see #23): Do I implement a pure javascript clone function, or JSON.parse(JSON.stringify(prelim))? See http://heyjavascript.com/4-creative-ways-to-clone-objects/. I would be happy to contribute this patch.
The patch I proposed yesterday I did in lib/rtree.js because I assumed dist/rtree.min.js and dist/rtree.js are build from there. Was that correct?

geometry collections

currently rTree.geoJSON can't handle geometry collections, this should be fixed.

Extract GeoJSON stuff into an extension

It would be nice to have a separate supported RTree implementation without the GeoJSON stuff that can be used for general use cases, and additionally a rtree-geojson.js extension or something like this.

I don't see any reason to have them combined if this is going to become a standard RTree JS implementation.

not all deleted nodes are returned

As can be seen in the tests, not all of the deleted nodes are returned. The actual circumstances seem to be depended on both the nodes and the shape of pages as adding the same data into the tree in a different order can effect this.

Remove callbacks in methods

What is the reason behind adding the callback arguments to purely synchronous methods? It doesn't add any value since all the methods are still sync, but complicates the code.

What happened to 'delete' function?

The examples show how to use delete function

###Usage:
•Deletes all object that touch the 10x10 rectangle starting at position 10x10:
• var myDelCount = myRTree.delete({x:10, y:10, w:10, h:10});
•Delete only specific object if it touches the 10x10 rectangle starting at position 10x10:
• var myDelCount = myRTree.delete({x:10, y:10, w:10, h:10}, specific_object);

But no such function appears to be defined.

NPM module

We can either try to see if @maxogden (ping) wants to update the current one, or think up a witty name for this on npm, if it's the latter case I'm thinking "arrTree"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.