Add types for flatbush and geoflatbush packages (#37113)

* Add types for flatbush and geoflatbush packages

* Cleanup

* cleanup

* fix
This commit is contained in:
mfedderly 2019-07-25 15:19:33 -04:00 committed by Wesley Wigham
parent bf3b32a1da
commit 9d0023e630
8 changed files with 185 additions and 0 deletions

View File

@ -0,0 +1,10 @@
import Flatbush from 'flatbush';
const from: Flatbush = Flatbush.from(new ArrayBuffer(0));
const index = new Flatbush(1);
index.add(0, 0, 1, 1);
index.finish();
const results = index.search(0.5, 0.5, 0.5, 0.5);
const neighbors = index.neighbors(0.5, 0.5);

100
types/flatbush/index.d.ts vendored Normal file
View File

@ -0,0 +1,100 @@
// Type definitions for flatbush 3.1
// Project: https://github.com/mourner/flatbush
// Definitions by: Matt Fedderly <https://github.com/mfedderly>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
type TypedArrayConstructor =
| Int8ArrayConstructor
| Uint8ArrayConstructor
| Uint8ClampedArrayConstructor
| Int16ArrayConstructor
| Uint16ArrayConstructor
| Int32ArrayConstructor
| Uint32ArrayConstructor
| Float32ArrayConstructor
| Float64ArrayConstructor;
declare class FlatbushClass {
/**
* @param numItems total number of items to be indexed
* @param nodeSize size of the tree node, experiment with different values for best performance. Default 16.
* @param arrayType The array type used for coordinates storage. Other types may be faster in certain cases. Default Float64Array.
*/
constructor(numItems: number, nodeSize?: number, arrayType?: TypedArrayConstructor);
/**
* Adds a given rectangle to the index.
*/
add(minX: number, minY: number, maxX: number, maxY: number): void;
/**
* Performs indexing of the added rectangles. Their number must match the one provided when creating a Flatbush object.
*/
finish(): void;
/**
* Returns an array of indices of items in a given bounding box.
*/
search(minX: number, minY: number, maxX: number, maxY: number, filter?: (idx: number) => boolean): number[];
/**
* Returns an array of item indices in order of distance from the given x, y (known as K nearest neighbors, or KNN).
*/
neighbors(
x: number,
y: number,
maxResults?: number,
maxDistance?: number,
filter?: (idx: number) => boolean
): number[];
/**
* Recreates a Flatbush index from raw ArrayBuffer data (that's exposed as index.data on a previously indexed Flatbush instance).
* Very useful for transferring indices between threads or storing them in a file.
*/
static from(data: ArrayBuffer): Flatbush;
/**
* array buffer that holds the index
*/
readonly data: ArrayBuffer;
/**
* bounding box of the data.
*/
readonly minX: number;
/**
* bounding box of the data.
*/
readonly minY: number;
/**
* bounding box of the data.
*/
readonly maxX: number;
/**
* bounding box of the data.
*/
readonly maxY: number;
/**
* number of stored items.
*/
readonly numItems: number;
/**
* number of items in a node tree.
*/
readonly nodeSize: number;
/**
* array type used for internal coordinates storage.
*/
readonly ArrayType: TypedArrayConstructor;
/**
* array type used for internal item indices storage.
*/
readonly IndexArrayType: TypedArrayConstructor;
}
export type Flatbush = FlatbushClass;
// tslint:disable-next-line:npm-naming https://github.com/mourner/flatbush/blob/master/index.js#L11
export default FlatbushClass;

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"flatbush-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -0,0 +1,5 @@
import Flatbush from 'flatbush';
import { around } from 'geoflatbush';
const index = new Flatbush(1);
const results = around(index, 0, 0);

22
types/geoflatbush/index.d.ts vendored Normal file
View File

@ -0,0 +1,22 @@
// Type definitions for geoflatbush 1.0
// Project: https://github.com/mourner/geoflatbush
// Definitions by: Matt Fedderly <https://github.com/mfedderly>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
import { Flatbush } from 'flatbush';
/**
* Performs nearest neighbors queries for geographic bounding boxes, taking Earth curvature and date line wrapping into account.
*
* @param maxResults the maximum number of results, default Infinity
* @param maxDistance distance in kilometers, default Infinity
*/
export function around(
index: Flatbush,
longitude: number,
latitude: number,
maxResults?: number,
maxDistance?: number,
filter?: (index: number) => boolean
): number[];

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"geoflatbush-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }