Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relocate geojson-rbush module into turf monorepo for easier maintenance #2523

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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