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

Convert voronoi package to Typescript #2637

Closed
wants to merge 3 commits into from
Closed
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
22 changes: 10 additions & 12 deletions packages/turf-voronoi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,42 @@

## voronoi

Takes a FeatureCollection of points, and a bounding box, and returns a FeatureCollection
Takes a collection of points and a bounding box, and returns a collection
of Voronoi polygons.

The Voronoi algorithim used comes from the d3-voronoi package.

### Parameters

* `points` **[FeatureCollection][1]<[Point][2]>** to find the Voronoi polygons around.
* `points` **[FeatureCollection][1]<[Point][2]>** points around which to calculate the Voronoi polygons
* `options` **[Object][3]** Optional parameters (optional, default `{}`)

* `options.bbox` **[Array][4]<[number][5]>** clipping rectangle, in \[minX, minY, maxX, MaxY] order. (optional, default `[-180,-85,180,-85]`)
* `options.bbox` **[BBox][4]** clipping rectangle, in \[minX, minY, maxX, MaxY] order (optional, default `[-180,-85,180,-85]`)

### Examples

```javascript
var options = {
const options = {
bbox: [-70, 40, -60, 60]
};
var points = turf.randomPoint(100, options);
var voronoiPolygons = turf.voronoi(points, options);
const points = turf.randomPoint(100, options);
const voronoiPolygons = turf.voronoi(points, options);

//addToMap
var addToMap = [voronoiPolygons, points];
const addToMap = [voronoiPolygons, points];
```

Returns **[FeatureCollection][1]<[Polygon][6]>** a set of polygons, one per input point.
Returns **[FeatureCollection][1]<[Polygon][5]>** a set of polygons, one per input point

[1]: https://tools.ietf.org/html/rfc7946#section-3.3

[2]: https://tools.ietf.org/html/rfc7946#section-3.1.2

[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[4]: https://tools.ietf.org/html/rfc7946#section-5

[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number

[6]: https://tools.ietf.org/html/rfc7946#section-3.1.6
[5]: https://tools.ietf.org/html/rfc7946#section-3.1.6

<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->

Expand Down
9 changes: 5 additions & 4 deletions packages/turf-voronoi/bench.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Benchmark from "benchmark";
import Benchmark, { Event } from "benchmark";
import { FeatureCollection, Point } from "geojson";
import path from "path";
import { fileURLToPath } from "url";
import fs from "fs";
Expand All @@ -12,7 +13,7 @@ const fixtures = fs.readdirSync(directory).map((filename) => {
return {
filename,
name: path.parse(filename).name,
geojson: loadJsonFileSync(directory + filename),
geojson: loadJsonFileSync(directory + filename) as FeatureCollection<Point>,
};
});

Expand All @@ -23,7 +24,7 @@ const fixtures = fs.readdirSync(directory).map((filename) => {
*/
const suite = new Benchmark.Suite("turf-voronoi");
for (const { name, geojson } of fixtures) {
suite.add(name, () => voronoi(geojson, geojson.features[0].properties.bbox));
suite.add(name, () => voronoi(geojson, { bbox: geojson.bbox }));
}

suite.on("cycle", (e) => console.log(String(e.target))).run();
suite.on("cycle", (e: Event) => console.log(String(e.target))).run();
12 changes: 0 additions & 12 deletions packages/turf-voronoi/index.d.ts

This file was deleted.

47 changes: 28 additions & 19 deletions packages/turf-voronoi/index.js → packages/turf-voronoi/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,58 @@
import {
BBox,
Feature,
FeatureCollection,
Point,
Polygon,
Position,
} from "geojson";
import { polygon, featureCollection, isObject } from "@turf/helpers";
import { collectionOf } from "@turf/invariant";
import { cloneProperties } from "@turf/clone";
import * as d3voronoi from "d3-voronoi";

/**
* Creates a polygon from a list of coordinates. Ensures the polygon is closed.
*
* @private
* @param {Array<Array<number>>} coords representing a polygon
* @param {Position[]} coords representing a polygon
* @returns {Feature<Polygon>} polygon
*/
function coordsToPolygon(coords) {
function coordsToPolygon(coords: Position[]) {
coords = coords.slice();
coords.push(coords[0]);
return polygon([coords]);
}

/**
* Takes a FeatureCollection of points, and a bounding box, and returns a FeatureCollection
* Takes a collection of points and a bounding box, and returns a collection
* of Voronoi polygons.
*
* The Voronoi algorithim used comes from the d3-voronoi package.
*
* @name voronoi
* @param {FeatureCollection<Point>} points to find the Voronoi polygons around.
* @param {FeatureCollection<Point>} points points around which to calculate the Voronoi polygons
* @param {Object} [options={}] Optional parameters
* @param {number[]} [options.bbox=[-180, -85, 180, -85]] clipping rectangle, in [minX, minY, maxX, MaxY] order.
* @returns {FeatureCollection<Polygon>} a set of polygons, one per input point.
* @param {BBox} [options.bbox=[-180, -85, 180, -85]] clipping rectangle, in [minX, minY, maxX, MaxY] order
* @returns {FeatureCollection<Polygon>} a set of polygons, one per input point
* @example
* var options = {
* const options = {
* bbox: [-70, 40, -60, 60]
* };
* var points = turf.randomPoint(100, options);
* var voronoiPolygons = turf.voronoi(points, options);
* const points = turf.randomPoint(100, options);
* const voronoiPolygons = turf.voronoi(points, options);
*
* //addToMap
* var addToMap = [voronoiPolygons, points];
* const addToMap = [voronoiPolygons, points];
*/
function voronoi(points, options) {
function voronoi(
points: FeatureCollection<Point>,
options?: { bbox?: BBox }
): FeatureCollection<Polygon> {
// Optional params
options = options || {};
if (!isObject(options)) throw new Error("options is invalid");
var bbox = options.bbox || [-180, -85, 180, 85];
const bbox = options.bbox || [-180, -85, 180, 85];

// Input Validation
if (!points) throw new Error("points is required");
Expand All @@ -49,13 +62,9 @@ function voronoi(points, options) {
// Main
return featureCollection(
d3voronoi
.voronoi()
.x(function (feature) {
return feature.geometry.coordinates[0];
})
.y(function (feature) {
return feature.geometry.coordinates[1];
})
.voronoi<Feature<Point>>()
.x((feature) => feature.geometry.coordinates[0])
.y((feature) => feature.geometry.coordinates[1])
.extent([
[bbox[0], bbox[1]],
[bbox[2], bbox[3]],
Expand Down
6 changes: 5 additions & 1 deletion packages/turf-voronoi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@
"tape": "^5.7.2",
"tsup": "^8.0.1",
"tsx": "^4.6.2",
"typescript": "^5.2.2",
"write-json-file": "^5.0.0"
},
"dependencies": {
"@turf/clone": "workspace:^",
"@turf/helpers": "workspace:^",
"@turf/invariant": "workspace:^",
"d3-voronoi": "1.1.2"
"@types/d3-voronoi": "^1.1.12",
"@types/geojson": "7946.0.8",
"d3-voronoi": "1.1.2",
"tslib": "^2.6.2"
}
}
3 changes: 2 additions & 1 deletion packages/turf-voronoi/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
import { voronoi } from "./index.js";
import { FeatureCollection, Point } from "geojson";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

Expand All @@ -13,7 +14,7 @@ test("turf-voronoi", (t) => {
.sync(path.join(__dirname, "test", "in", "*.json"))
.forEach((filepath) => {
const { name } = path.parse(filepath);
const geojson = loadJsonFileSync(filepath);
const geojson = loadJsonFileSync(filepath) as FeatureCollection<Point>;
const results = voronoi(geojson, { bbox: geojson.bbox });

const out = filepath.replace(
Expand Down
73 changes: 72 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading