🤖 Merge PR #46380 [polygon-lookup] Add types for polygon-lookup by @ffflorian

This commit is contained in:
Florian Keller 2020-07-28 18:21:19 +02:00 committed by GitHub
parent 202a040f62
commit 92aab27ecd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 0 deletions

50
types/polygon-lookup/index.d.ts vendored Normal file
View File

@ -0,0 +1,50 @@
// Type definitions for polygon-lookup 2.6
// Project: https://github.com/pelias/polygon-lookup
// Definitions by: Florian Keller <https://github.com/ffflorian>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import { FeatureCollection, Polygon } from 'geojson';
import RBush from 'rbush';
declare namespace PolygonLookup {
interface BBox {
maxX: number;
maxY: number;
minX: number;
minY: number;
}
}
declare class PolygonLookup {
constructor(featureCollection?: FeatureCollection);
/** A spatial index for `this.polygons`. */
rtree: RBush<PolygonLookup.BBox[]>;
polygons: FeatureCollection;
/**
* Find polygon(s) that a point intersects. Execute a bounding-box search to
* narrow down the candidate polygons to a small subset, and then perform
* additional point-in-polygon intersections to resolve any ambiguities.
*
* @param x The x-coordinate of the point.
* @param y The y-coordinate of the point.
* @param limit Number of results to return (`-1` to return all the results).
* @return If one or more bounding box intersections are
* found and limit is `undefined`, return the first polygon that intersects
* (`x`, `y`); otherwise, `undefined`. If a limit is passed in, return
* intersecting polygons as a GeoJSON `FeatureCollection`.
*/
search(x: number, y: number): Polygon | undefined;
search(x: number, y: number, limit: number): FeatureCollection | undefined;
/**
* Build a spatial index for a set of polygons, and store both the polygons and
* the index in this `PolygonLookup`.
*
* @param collection A GeoJSON-formatted `FeatureCollection`.
*/
loadFeatureCollection(collection: FeatureCollection): void;
}
export = PolygonLookup;

View File

@ -0,0 +1,27 @@
import PolygonLookup = require('polygon-lookup');
import { FeatureCollection } from 'geojson';
const featureCollection: FeatureCollection = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: { id: 'bar' },
geometry: {
type: 'Polygon',
coordinates: [
[
[0, 1],
[2, 1],
[3, 4],
[1, 5],
],
],
},
},
],
};
const lookup = new PolygonLookup(featureCollection);
lookup.search(1, 2);
lookup.search(1, 2, 1);

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",
"polygon-lookup-tests.ts"
]
}

View File

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