Skip to content

Commit

Permalink
Converted turf-tesselate to Typescript (#2650)
Browse files Browse the repository at this point in the history
* Renamed turf-tesselate/index.js to .ts in preparation for converting to Typescript.

* Converted turf-tesselate to Typescript.
  • Loading branch information
smallsaucepan authored Jul 23, 2024
1 parent 3a6a035 commit 19d2aa9
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 109 deletions.
26 changes: 12 additions & 14 deletions packages/turf-tesselate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,34 @@

## tesselate

Tesselates a [Feature\<Polygon>][1] into a [FeatureCollection\<Polygon>][2] of triangles
using [earcut][3].
Tesselates a polygon or multipolygon into a collection of triangle polygons
using [earcut][1].

### Parameters

* `poly` **[Feature][4]<[Polygon][5]>** the polygon to tesselate
* `poly` **[Feature][2]<([Polygon][3] | [MultiPolygon][4])>** the polygon to tesselate

### Examples

```javascript
var poly = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);
var triangles = turf.tesselate(poly);
const poly = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);
const triangles = turf.tesselate(poly);

//addToMap
var addToMap = [poly, triangles]
const addToMap = [poly, triangles]
```

Returns **[FeatureCollection][6]<[Polygon][5]>** a geometrycollection feature
Returns **[FeatureCollection][5]<[Polygon][3]>** collection of polygon tesselations

[1]: Feature<Polygon>
[1]: https://github.com/mapbox/earcut

[2]: FeatureCollection<Polygon>
[2]: https://tools.ietf.org/html/rfc7946#section-3.2

[3]: https://github.com/mapbox/earcut
[3]: https://tools.ietf.org/html/rfc7946#section-3.1.6

[4]: https://tools.ietf.org/html/rfc7946#section-3.2
[4]: https://tools.ietf.org/html/rfc7946#section-3.1.7

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

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

<!-- 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
4 changes: 2 additions & 2 deletions packages/turf-tesselate/bench.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Benchmark from "benchmark";
import Benchmark, { Event } from "benchmark";
import { polygon } from "@turf/helpers";
import { tesselate } from "./index.js";

Expand All @@ -20,5 +20,5 @@ var poly = polygon([
*/
new Benchmark.Suite("turf-tesselate")
.add("polygon", () => tesselate(poly))
.on("cycle", (e) => console.log(String(e.target)))
.on("cycle", (e: Event) => console.log(String(e.target)))
.run();
9 changes: 9 additions & 0 deletions packages/turf-tesselate/earcut.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module "earcut" {
declare function earcut(
vertices: number[],
holes: number[],
dimensions: number
);

export default earcut;
}
11 changes: 0 additions & 11 deletions packages/turf-tesselate/index.d.ts

This file was deleted.

80 changes: 0 additions & 80 deletions packages/turf-tesselate/index.js

This file was deleted.

96 changes: 96 additions & 0 deletions packages/turf-tesselate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import {
Feature,
FeatureCollection,
MultiPolygon,
Polygon,
Position,
} from "geojson";
import earcut from "earcut";
import { polygon } from "@turf/helpers";

/**
* Tesselates a polygon or multipolygon into a collection of triangle polygons
* using [earcut](https://github.com/mapbox/earcut).
*
* @name tesselate
* @param {Feature<Polygon|MultiPolygon>} poly the polygon to tesselate
* @returns {FeatureCollection<Polygon>} collection of polygon tesselations
* @example
* const poly = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);
* const triangles = turf.tesselate(poly);
*
* //addToMap
* const addToMap = [poly, triangles]
*/
function tesselate(
poly: Feature<Polygon | MultiPolygon>
): FeatureCollection<Polygon> {
if (
!poly.geometry ||
(poly.geometry.type !== "Polygon" && poly.geometry.type !== "MultiPolygon")
) {
throw new Error("input must be a Polygon or MultiPolygon");
}

const fc: FeatureCollection<Polygon> = {
type: "FeatureCollection",
features: [],
};

if (poly.geometry.type === "Polygon") {
fc.features = processPolygon(poly.geometry.coordinates);
} else {
poly.geometry.coordinates.forEach(function (coordinates) {
fc.features = fc.features.concat(processPolygon(coordinates));
});
}

return fc;
}

function processPolygon(coordinates: Position[][]) {
const data = flattenCoords(coordinates);
const dim = 2;
const result = earcut(data.vertices, data.holes, dim);

const features: Feature<Polygon>[] = [];
const vertices: Position[] = [];

result.forEach(function (vert: any, i: number) {
const index = result[i];
vertices.push([data.vertices[index * dim], data.vertices[index * dim + 1]]);
});

for (var i = 0; i < vertices.length; i += 3) {
const coords = vertices.slice(i, i + 3);
coords.push(vertices[i]);
features.push(polygon([coords]));
}

return features;
}

function flattenCoords(data: Position[][]) {
const dim: number = data[0][0].length,
result: { vertices: number[]; holes: number[]; dimensions: number } = {
vertices: [],
holes: [],
dimensions: dim,
};
let holeIndex = 0;

for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].length; j++) {
for (let d = 0; d < dim; d++) result.vertices.push(data[i][j][d]);
}
if (i > 0) {
holeIndex += data[i - 1].length;
result.holes.push(holeIndex);
}
}

return result;
}

export { tesselate };
export default tesselate;
8 changes: 6 additions & 2 deletions packages/turf-tesselate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@
},
"devDependencies": {
"@types/benchmark": "^2.1.5",
"@types/tape": "^4.2.32",
"benchmark": "^2.1.4",
"npm-run-all": "^4.1.5",
"tape": "^5.7.2",
"tsup": "^8.0.1",
"tsx": "^4.6.2"
"tsx": "^4.6.2",
"typescript": "^5.2.2"
},
"dependencies": {
"@turf/helpers": "workspace:^",
"earcut": "^2.2.4"
"@types/geojson": "7946.0.8",
"earcut": "^2.2.4",
"tslib": "^2.6.2"
}
}
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 19d2aa9

Please sign in to comment.