# GeoStore Spatial Indexes

Spatial Indexes are an extremely important part of the GeoStore. Spatial Indexes allow for very quick elimination of `Features` and are part of the core of the GeoStore.

Spatial Indexes are asyncronous and use Node.js style callbacks.

## Link Existing Spatial Indexes Back to Top

Terraformer currently ships with a single spatial index, with more coming soon.

- RTree - RTree Index, works in Node.js and the browser

## Link Writing a Spatial Index Back to Top

Spatial Indexes are designed for searching spatial data, whether in 2D or 3D. The Terraformer GeoStore platform has defined a set of indexes that allow for multiple spatial index types to be used. As long as the methods are asyncronous and conform to the Terraformer GeoStore interface, any type of spatial index can be used.

### Link Methods Back to Top

#### Link new SpatialIndex() Back to Top

Instantiating a `SpatialIndex` should return a new object containing method signatures conforming to the `SpatialIndex` interface.

You can pass any needed arguments while instantiating.

Example:

    var si = new SpatialIndex({
      "width": 45,
      "dateline": false
    });

#### Link SpatialIndex.insert(geojson | envelope, id, callback) Back to Top

Add a `geojson` object or `envelope` to a `SpatialIndex`. It is important to include an `id`, as this is the key that is returned from searches.

Option | Value | Description
--- | --- | ---
`GeoJSON` or `Envelope` | `object` | Must be either `GeoJSON` or `Envelope`
id | `String` or `Number` | The `id` of the spatial area
callback | `function` | Callback to be fired when the `insert` has been completed

Example:

    si.insert(geojson, id, function (err, res) {
      // Node.js style callback
    });

#### Link SpatialIndex.remove(geojson | envelope, id, callback) Back to Top

Remove a `geojson` or `envelope` object already in a SpatialIndex.

Option | Value | Description
--- | --- | ---
`GeoJSON` or `Envelope` | `object` | Must be either `GeoJSON` or `Envelope`
id | `String` or `Number` | The `id` of the spatial area
callback | `function` | Callback to be fired when the `remove` has been completed

Example:

    si.remove(geojson, id, function (err, res) {
      // Node.js style callback
    });

#### Link SpatialIndex.search(geojson | envelope, callback) Back to Top

Searches for any `id`’s that can contain the `geojson` or `envelope` passed in. These are returned as an `Array`.

Option | Value | Description
--- | --- | ---
`GeoJSON` or `Envelope` | `object` | Must be either `GeoJSON` or `Envelope`
callback | `function` | Callback to be fired when the `search` has been completed

Example:

    si.search(geojson, function (err, res) {
      // Node.js style callback
    });

#### Link SpatialIndex.within(geojson | envelope, callback) Back to Top

Searches for any `id`’s that are within the `geojson` or `envelope` passed in. These are returned as an `Array`.

Option | Value | Description
--- | --- | ---
`GeoJSON` or `Envelope` | `object` | Must be either `GeoJSON` or `Envelope`
callback | `function` | Callback to be fired when the `search` has been completed

Example:

    si.within(geojson, function (err, res) {
      // Node.js style callback
    });