Skip to content

Commit

Permalink
fix: remove dependency on nodejs lib "assert"
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgengrimnes committed Jan 1, 2022
1 parent 7d5390c commit 5f6e6f5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
17 changes: 14 additions & 3 deletions README.md
Expand Up @@ -2,10 +2,16 @@

This is an implementation of a Hilbert Packed R-Tree without any external dependencies. R-Trees are a special data structure for indexing spatial data. To improve the performance of query operations on the data structure, the R-Tree may be packed using the space filling [Hilbert Curve](https://en.wikipedia.org/wiki/Hilbert_curve).

## Install
## Requirements

```
npm install hilbert-rtree
This library may be used in browser environments and does not depend on NodeJS libraries.

Without polyfilling, the implementation depends on features available in node `>= 8`.

## Installation

```bash
npm i -S hilbert-rtree
```

## Usage
Expand Down Expand Up @@ -35,3 +41,8 @@ const result = tree.search(boundingRectangle);

console.log(result); // -> prints: [ "This can be any data type" ]
```

## API

#### [Documentation is available here](https://jorgenkg.github.io/hilbert-rtree/index.html)

62 changes: 45 additions & 17 deletions lib/r-tree/RTree.ts
@@ -1,4 +1,3 @@
import * as assert from "assert";
import { RTreeRectangle } from "./RTreeRectangle.js";
import { sortRectanglesByHilbertCoordinates } from "../misc/sortRectanglesByHilbertCoordinates.js";
import { splitIntoTwo } from "../misc/splitIntoTwo.js";
Expand Down Expand Up @@ -44,11 +43,21 @@ export class RTree {
/** Find data records that overlap with the bounding box.
* @returns List of `Record["data"]` from overlapped Records. */
public search(searchBoundary: BoundingBox) {
assert(this.rootNode, "Expect tree to be created");
assert(searchBoundary.x >= 0, "Expect X coordinate to be >= 0");
assert(searchBoundary.y >= 0, "Expect Y coordinate to be >= 0");
assert(searchBoundary.height >= 0, "Expect `height` to be >= 0");
assert(searchBoundary.width >= 0, "Expect `width` to be >= 0");
if(this.rootNode === undefined) {
throw new Error("Expect tree to be created");
}
if(searchBoundary.x < 0) {
throw new Error("Expect X coordinate to be >= 0");
}
if(searchBoundary.y < 0) {
throw new Error("Expect Y coordinate to be >= 0");
}
if(searchBoundary.height < 0) {
throw new Error("Expect `height` to be >= 0");
}
if(searchBoundary.width < 0) {
throw new Error("Expect `width` to be >= 0");
}

const searchRect = new RTreeRectangle(searchBoundary);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
Expand All @@ -60,10 +69,18 @@ export class RTree {

/** Insert a single record to the RTree and re-balance the tree if it violates `maxChildrenPerNode`. */
public insert(record: Record): void {
assert(record.x >= 0, "Expect X coordinate to be >= 0");
assert(record.y >= 0, "Expect Y coordinate to be >= 0");
"height" in record && assert(record.height >= 0, "Expect `height` to be >= 0 if defined");
"width" in record && assert(record.width >= 0, "Expect `width` to be >= 0 if defined");
if(record.x < 0) {
throw new Error("Expect X coordinate to be >= 0");
}
if(record.y < 0) {
throw new Error("Expect Y coordinate to be >= 0");
}
if("height" in record && record.height < 0) {
throw new Error("Expect `height` to be >= 0 if defined");
}
if("width" in record && record.width < 0) {
throw new Error("Expect `width` to be >= 0 if defined");
}

// Rectangle representation of the data point to insert into the RTree
const insertRect = new RTreeRectangle(record);
Expand Down Expand Up @@ -151,13 +168,22 @@ export class RTree {
/** List of data records to insert in a R-tree structure. */
records: Array<Record>
) {
assert(this.rootNode === undefined, "Expect tree to be empty before batch inserting nodes");

if(this.rootNode !== undefined) {
throw new Error("Expect tree to be empty before batch inserting nodes");
}
for(const record of records) {
assert(record.x >= 0, "Expect X coordinate to be >= 0");
assert(record.y >= 0, "Expect Y coordinate to be >= 0");
"height" in record && assert(record.height >= 0, "Expect `height` to be >= 0 if defined");
"width" in record && assert(record.width >= 0, "Expect `width` to be >= 0 if defined");
if(record.x < 0) {
throw new Error("Expect X coordinate to be >= 0");
}
if(record.y < 0) {
throw new Error("Expect Y coordinate to be >= 0");
}
if("height" in record && record.height < 0) {
throw new Error("Expect `height` to be >= 0 if defined");
}
if("width" in record && record.width < 0) {
throw new Error("Expect `width` to be >= 0 if defined");
}
}

const rectangles = records
Expand All @@ -170,7 +196,9 @@ export class RTree {

/** Move `leaf` if the node's parent contains more than `maxChildrenPerNode` children. */
private balanceTreePath(leaf: RTreeRectangle): void {
assert(leaf.isLeafNode(), "Expect the provided node to be a leaf node");
if(!leaf.isLeafNode()) {
throw new Error("Expect the provided node to be a leaf node");
}

const observedNodes = [leaf.parent];

Expand Down
4 changes: 0 additions & 4 deletions package.json
Expand Up @@ -8,9 +8,6 @@
"generate-docs": "npx typedoc --excludePrivate --hideGenerator --includeVersion --readme ./README.md --out docs index.ts"
},
"license": "MIT",
"engines": {
"node": ">=8"
},
"files": [
"build/lib",
"build/index*"
Expand All @@ -28,7 +25,6 @@
"hilbert r-tree",
"hilbert curve"
],
"engineStrict": true,
"devDependencies": {
"@commitlint/cli": "16.0.1",
"@commitlint/config-conventional": "16.0.0",
Expand Down

0 comments on commit 5f6e6f5

Please sign in to comment.