Java Spatial Index - Forked for Integer types
Clone or download
Pull request Compare This branch is 5 commits ahead of aled:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

This is a Fork

This fork of JSI is maintained by Jeff Martin and released under the exact same LGPL license.

I really only made one change. I converted all the float types in the geometry classes to integers instead.

That change makes this RTree implementation work on 32-bit integer grids without loss of precision. In other words, 2147483647 - 1 doesn't work with floats, but it does with ints.

Last time I checked, it still passes all the unit tests.

Original readme follows below.


The Java Spatial Index project aims to maintain a high performance Java version of the RTree spatial indexing algorithm as described in the 1984 paper "R-trees: A Dynamic Index Structure for Spatial Searching" by Antonin Guttman. (PDF on CiteSeerX)

The JSI spatial index is deliberately limited in features, and does a small number of things well. It particular, it is fast.

The code is open source, and released under the GNU Lesser General Public License, version 2.1 or later.


It is highly recommended to start by looking at the jsi-examples repository at

Briefly, you need to initialize the RTree like this:

// Create and initialize an rtree
SpatialIndex si = new RTree();

Then add some rectangles; each one has an ID.

final Rectangle[] rects = new Rectangle[100];
rects[0] = new Rectangle(0, 10, 0, 10);
rects[1] = new Rectangle(0, 11, 1, 20);
si.add(rects[0], 0);
si.add(rects[1], 1);

and finally query for the 3 nearest rectangles to (36.3, 84.3) by calling the nearestN() method.

  new Point(36.3f, 84.3f),      // the point for which we want to find nearby rectangles
  new TIntProcedure() {         // a procedure whose execute() method will be called with the results
    public boolean execute(int i) {"Rectangle " + i + " " + rects[i] + ", distance=" + rects[i].distance(p));
      return true;              // return true here to continue receiving results
  3,                            // the number of nearby rectangles to find
  Float.MAX_VALUE               // Don't bother searching further than this. MAX_VALUE means search everything

A binary distribution that contains the JSI jar and all the runtime dependencies is available from

Alternatively, maven users can use this repository in their pom.xml:

  <name>sourceforge jsi repository</name>



To build the JSI library from source, install maven 3 and run the following:

% cd <location-of-pom.xml>
% mvn package

This will generate the binary package (jsi-x.y.z.jar) in the target subdirectory.

The following is a list of useful maven targets:

eclipse:eclipse (generate eclipse project files; see below)
-Dtest=ReferenceCompareTest_10000 test
assembly:single (create package only)

To import the project into eclipse, run mvn eclipse:eclipse, and then set the M2_REPO variable in Window -> Preferences -> Java -> Build Path -> Classpath Variables to point to your local maven repository (e.g. ~/.m2/repository)


These are the steps needed to check that the JSI library is working correctly. Note this will take a very long time to run:

% cd <location-of-pom.xml>
% mvn test [This runs a short and quick test]
% mvn -Dtest=ReferenceCompareTest_1000 test [Long test]
% mvn -Dtest=ReferenceCompareTest_10000 test [Very long test]
% mvn -Dtest=ReferenceCompareTest_100000 test [Ridiculously long test]

If any errors occur, please raise an issue at