Skip to content

Commit

Permalink
Relocate geojson-rbush module into turf monorepo for easier maintenan…
Browse files Browse the repository at this point in the history
…ce (#2523)

* Verbatim copy of latest Turfjs/geojson-rbush (e36e77a) to pull into the turf monorepo for easier maintenance. Not building yet. Only establishing a clear snapshot for handover between the repos.

* Building and testing ok in module and as part of monorepo.

* Renaming package from turf-rbush to turf-geojson-rbush per review comment - in case we need to add a vanilla rbush module one day.

* Removing historical changelog and license from old repo. Making minor changes to readme for now as it should eventually be generated automatically from the code. Will work on JSDocs at the same time as we start using this module from other places within the monorepo.
  • Loading branch information
smallsaucepan authored Nov 3, 2023
1 parent f17fc88 commit c11f76b
Show file tree
Hide file tree
Showing 18 changed files with 1,522 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/turf-geojson-rbush/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2017 TurfJS

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
211 changes: 211 additions & 0 deletions packages/turf-geojson-rbush/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
# GeoJSON RBush used in TurfJS

[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/DenisCarriere/geojson-rbush/master/LICENSE)

GeoJSON implementation of [RBush](https://github.com/mourner/rbush) — a high-performance JavaScript R-tree-based 2D spatial index for points and rectangles.

## Install

**npm**

```bash
$ npm install --save geojson-rbush
```

## API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### Table of Contents

- [rbush](#rbush)
- [insert](#insert)
- [load](#load)
- [remove](#remove)
- [clear](#clear)
- [search](#search)
- [collides](#collides)
- [all](#all)
- [toJSON](#tojson)
- [fromJSON](#fromjson)

### rbush

GeoJSON implementation of [RBush](https://github.com/mourner/rbush#rbush) spatial index.

**Parameters**

- `maxEntries` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** defines the maximum number of entries in a tree node. 9 (used by default) is a
reasonable choice for most applications. Higher value means faster insertion and slower search, and vice versa. (optional, default `9`)

**Examples**

```javascript
var geojsonRbush = require('geojson-rbush').default;
var tree = geojsonRbush();
```

Returns **RBush** GeoJSON RBush

### insert

[insert](https://github.com/mourner/rbush#data-format)

**Parameters**

- `feature` **Feature** insert single GeoJSON Feature

**Examples**

```javascript
var poly = turf.polygon([[[-78, 41], [-67, 41], [-67, 48], [-78, 48], [-78, 41]]]);
tree.insert(poly)
```

Returns **RBush** GeoJSON RBush

### load

[load](https://github.com/mourner/rbush#bulk-inserting-data)

**Parameters**

- `features` **(FeatureCollection | [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;Feature>)** load entire GeoJSON FeatureCollection

**Examples**

```javascript
var polys = turf.polygons([
[[[-78, 41], [-67, 41], [-67, 48], [-78, 48], [-78, 41]]],
[[[-93, 32], [-83, 32], [-83, 39], [-93, 39], [-93, 32]]]
]);
tree.load(polys);
```

Returns **RBush** GeoJSON RBush

### remove

[remove](https://github.com/mourner/rbush#removing-data)

**Parameters**

- `feature` **Feature** remove single GeoJSON Feature
- `equals` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Pass a custom equals function to compare by value for removal.

**Examples**

```javascript
var poly = turf.polygon([[[-78, 41], [-67, 41], [-67, 48], [-78, 48], [-78, 41]]]);

tree.remove(poly);
```

Returns **RBush** GeoJSON RBush

### clear

[clear](https://github.com/mourner/rbush#removing-data)

**Examples**

```javascript
tree.clear()
```

Returns **RBush** GeoJSON Rbush

### search

[search](https://github.com/mourner/rbush#search)

**Parameters**

- `geojson` **(BBox | FeatureCollection | Feature)** search with GeoJSON

**Examples**

```javascript
var poly = turf.polygon([[[-78, 41], [-67, 41], [-67, 48], [-78, 48], [-78, 41]]]);

tree.search(poly);
```

Returns **FeatureCollection** all features that intersects with the given GeoJSON.

### collides

[collides](https://github.com/mourner/rbush#collisions)

**Parameters**

- `geojson` **(BBox | FeatureCollection | Feature)** collides with GeoJSON

**Examples**

```javascript
var poly = turf.polygon([[[-78, 41], [-67, 41], [-67, 48], [-78, 48], [-78, 41]]]);

tree.collides(poly);
```

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true if there are any items intersecting the given GeoJSON, otherwise false.

### all

[all](https://github.com/mourner/rbush#search)

**Examples**

```javascript
tree.all()
```

Returns **FeatureCollection** all the features in RBush

### toJSON

[toJSON](https://github.com/mourner/rbush#export-and-import)

**Examples**

```javascript
var exported = tree.toJSON()
```

Returns **any** export data as JSON object

### fromJSON

[fromJSON](https://github.com/mourner/rbush#export-and-import)

**Parameters**

- `json` **any** import previously exported data

**Examples**

```javascript
var exported = {
"children": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [110, 50]
},
"properties": {},
"bbox": [110, 50, 110, 50]
}
],
"height": 1,
"leaf": true,
"minX": 110,
"minY": 50,
"maxX": 110,
"maxY": 50
}
tree.fromJSON(exported)
```

Returns **RBush** GeoJSON RBush
38 changes: 38 additions & 0 deletions packages/turf-geojson-rbush/bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const Benchmark = require("benchmark");
const { randomPoint, randomPolygon } = require("@turf/random");
const geojsonRbush = require("./").default;

// Fixtures
const points = randomPoint(3);
const point = points.features[0];
const polygons = randomPolygon(3);
const polygon = polygons.features[0];

// Load trees before (used to benchmark search)
const pointsTree = geojsonRbush();
pointsTree.load(points);
const polygonsTree = geojsonRbush();
polygonsTree.load(polygons);

/**
* Benchmark Results
*
* rbush.points x 313,979 ops/sec ±10.60% (67 runs sampled)
* rbush.polygons x 428,333 ops/sec ±1.69% (70 runs sampled)
* search.points x 5,986,675 ops/sec ±7.95% (77 runs sampled)
* search.polygons x 6,481,248 ops/sec ±0.93% (90 runs sampled)
*/
new Benchmark.Suite("geojson-rbush")
.add("rbush.points", () => {
const tree = geojsonRbush();
tree.load(points);
})
.add("rbush.polygons", () => {
const tree = geojsonRbush();
tree.load(polygons);
})
.add("search.points", () => pointsTree.search(point))
.add("search.polygons", () => polygonsTree.search(polygon))
.on("cycle", (e) => console.log(String(e.target)))
.on("complete", () => {})
.run();
30 changes: 30 additions & 0 deletions packages/turf-geojson-rbush/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
BBox,
Feature,
FeatureCollection,
Geometry,
GeoJsonProperties,
} from "geojson";

declare class RBush<G extends Geometry, P extends GeoJsonProperties> {
insert(feature: Feature<G, P>): RBush<G, P>;
load(features: FeatureCollection<G, P> | Feature<G, P>[]): RBush<G, P>;
remove(
feature: Feature<G, P>,
equals?: (a: Feature<G, P>, b: Feature<G, P>) => boolean
): RBush<G, P>;
clear(): RBush<G, P>;
search(geojson: Feature | FeatureCollection | BBox): FeatureCollection<G, P>;
all(): FeatureCollection<any>;
collides(geosjon: Feature | FeatureCollection | BBox): boolean;
toJSON(): any;
fromJSON(data: any): RBush<G, P>;
}

/**
* https://github.com/mourner/rbush
*/
export default function rbush<
G extends Geometry = Geometry,
P extends GeoJsonProperties = GeoJsonProperties,
>(maxEntries?: number): RBush<G, P>;
Loading

0 comments on commit c11f76b

Please sign in to comment.