hilbert-rtree
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -1,4 +0,4 @@ | ||
import { Point } from "./Point"; | ||
import type { BoundingBox } from "./BoundingBox.js"; | ||
export declare type Record<T = any> = Readonly<(BoundingBox | Point) & { | ||
import type { Point } from "./Point"; | ||
export declare type Record<T = unknown> = Readonly<(BoundingBox | Point) & { | ||
/** Data to be stored in the R-Tree */ | ||
@@ -5,0 +5,0 @@ data?: T; |
@@ -11,7 +11,8 @@ "use strict"; | ||
const { maxCoordinate, minCoordinate } = rectangles | ||
.map(rectangle => [ | ||
.map(rectangle => rectangle.getCenter()) | ||
.map(({ centerX, centerY }) => [ | ||
// X coordinate | ||
Math.ceil(rectangle.x + rectangle.width * 0.5), | ||
Math.floor(centerX), | ||
// Y coordinate | ||
Math.ceil(rectangle.y + rectangle.height * 0.5) | ||
Math.floor(centerY) | ||
]) | ||
@@ -25,5 +26,6 @@ .reduce(({ maxCoordinate: accumulatedMax, minCoordinate: accumulatedMin }, [x, y]) => { | ||
const weightedRectangles = rectangles | ||
.map(rectangle => ({ | ||
.map(rectangle => ({ ...rectangle.getCenter(), rectangle })) | ||
.map(({ rectangle, centerX, centerY }) => ({ | ||
rectangle, | ||
weight: (0, HilbertCurves_js_1.toHilbertCoordinates)(maxCoordinate - minCoordinate, Math.ceil(rectangle.x + rectangle.width * 0.5) - minCoordinate, Math.ceil(rectangle.y + rectangle.height * 0.5) - minCoordinate) | ||
weight: (0, HilbertCurves_js_1.toHilbertCoordinates)(maxCoordinate - minCoordinate, Math.floor(centerX) - minCoordinate, Math.floor(centerY) - minCoordinate) | ||
})); | ||
@@ -30,0 +32,0 @@ weightedRectangles.sort((A, B) => A.weight - B.weight); |
@@ -14,3 +14,7 @@ import type { BoundingBox } from "../@types/BoundingBox.js"; | ||
getArea(): number; | ||
getCenter(): { | ||
centerX: number; | ||
centerY: number; | ||
}; | ||
} | ||
//# sourceMappingURL=Rectangle.d.ts.map |
@@ -35,3 +35,9 @@ "use strict"; | ||
} | ||
getCenter() { | ||
return { | ||
centerX: this.x + (this.width / 2), | ||
centerY: this.y + (this.height / 2), | ||
}; | ||
} | ||
} | ||
exports.Rectangle = Rectangle; |
@@ -16,3 +16,3 @@ import type { BoundingBox } from "../@types/BoundingBox.js"; | ||
* @returns List of `Record["data"]` from overlapped Records. */ | ||
search(searchBoundary: BoundingBox): any[]; | ||
search(searchBoundary: BoundingBox): unknown[]; | ||
/** Insert a single record to the RTree and re-balance the tree if it violates `maxChildrenPerNode`. */ | ||
@@ -19,0 +19,0 @@ insert(record: Record): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RTree = void 0; | ||
const assert = require("assert"); | ||
const RTreeRectangle_js_1 = require("./RTreeRectangle.js"); | ||
@@ -19,3 +18,2 @@ const sortRectanglesByHilbertCoordinates_js_1 = require("../misc/sortRectanglesByHilbertCoordinates.js"); | ||
// rectangle should be returned by the search. | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return node.getSubtreeData(); | ||
@@ -25,3 +23,2 @@ } | ||
// If the query overlaps a leaf node, the leaf data shall be returned. | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return node.getSubtreeData(); | ||
@@ -40,12 +37,20 @@ } | ||
search(searchBoundary) { | ||
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_js_1.RTreeRectangle(searchBoundary); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return this | ||
.recursiveSearchForOverlappingNodes(searchRect, this.rootNode) | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
.map(node => node.data); | ||
@@ -55,6 +60,14 @@ } | ||
insert(record) { | ||
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 | ||
@@ -64,3 +77,3 @@ const insertRect = new RTreeRectangle_js_1.RTreeRectangle(record); | ||
if (!this.rootNode) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { data, ...boundingBox } = record; | ||
@@ -134,8 +147,18 @@ this.rootNode = new RTreeRectangle_js_1.RTreeRectangle(boundingBox); | ||
records) { | ||
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"); | ||
} | ||
} | ||
@@ -148,3 +171,5 @@ const rectangles = records | ||
balanceTreePath(leaf) { | ||
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]; | ||
@@ -151,0 +176,0 @@ // Traverse the ancestor path if the leaf node if the next parent node has too many children. |
{ | ||
"name": "hilbert-rtree", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"scripts": { | ||
@@ -11,5 +11,2 @@ "test": "./node_modules/.bin/tape --enable-source-maps --unhandled-rejections=strict build/test/integration/*.js", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"files": [ | ||
@@ -32,7 +29,6 @@ "build/lib", | ||
], | ||
"engineStrict": true, | ||
"devDependencies": { | ||
"@commitlint/cli": "16.0.1", | ||
"@commitlint/config-conventional": "16.0.0", | ||
"@types/node": "17.0.5", | ||
"@types/node": "17.0.6", | ||
"@types/tape": "4.13.2", | ||
@@ -39,0 +35,0 @@ "@typescript-eslint/eslint-plugin": "5.8.1", |
@@ -5,7 +5,13 @@ # Hilbert Packed R-Tree in Typescript | ||
## Install | ||
## Requirements | ||
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 | ||
``` | ||
npm install hilbert-rtree | ||
``` | ||
@@ -39,1 +45,6 @@ ## Usage | ||
``` | ||
## API | ||
#### [Documentation is available here](https://jorgenkg.github.io/hilbert-rtree/index.html) | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
30060
502
49