Skip to content

Commit

Permalink
Converted turf-unkink-polygon to Typescript (#2654)
Browse files Browse the repository at this point in the history
Converted to Typescript. Synced up JSDoc types with parameter types. Minor error in on of the JSDoc params type.
  • Loading branch information
smallsaucepan authored Jul 23, 2024
1 parent e2edb44 commit 584acbc
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 36 deletions.
20 changes: 11 additions & 9 deletions packages/turf-unkink-polygon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,37 @@

## unkinkPolygon

Takes a kinked polygon and returns a feature collection of polygons that have no kinks.
Takes a kinked polygon and returns a feature collection of polygons that have
no kinks.

Uses [simplepolygon][1] internally.

### Parameters

* `geojson` **([FeatureCollection][2] | [Feature][3]<([Polygon][4] | [MultiPolygon][5])>)** GeoJSON Polygon or MultiPolygon
* `geojson` **([FeatureCollection][2]<([Polygon][3] | [MultiPolygon][4])> | [Feature][5]<([Polygon][3] | [MultiPolygon][4])> | [Polygon][3] | [MultiPolygon][4])** polygons to unkink

### Examples

```javascript
var poly = turf.polygon([[[0, 0], [2, 0], [0, 2], [2, 2], [0, 0]]]);
const poly = turf.polygon([[[0, 0], [2, 0], [0, 2], [2, 2], [0, 0]]]);

var result = turf.unkinkPolygon(poly);
const result = turf.unkinkPolygon(poly);

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

Returns **[FeatureCollection][2]<[Polygon][4]>** Unkinked polygons
Returns **[FeatureCollection][2]<[Polygon][3]>** Unkinked polygons

[1]: https://github.com/mclaeysb/simplepolygon

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

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

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

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

<!-- 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: 6 additions & 3 deletions packages/turf-unkink-polygon/bench.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Feature, FeatureCollection, Polygon } from "geojson";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import Benchmark from "benchmark";
import Benchmark, { Event } from "benchmark";
import { unkinkPolygon as unkink } from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
Expand All @@ -12,7 +13,9 @@ const directories = {
};

const fixtures = fs.readdirSync(directories.in).map((filename) => {
return { filename, geojson: loadJsonFileSync(directories.in + filename) };
const geojson: FeatureCollection<Polygon> | Feature<Polygon> =
loadJsonFileSync(directories.in + filename);
return { filename, geojson };
});

const suite = new Benchmark.Suite("unkink-polygon");
Expand All @@ -23,7 +26,7 @@ for (const fixture of fixtures) {
}

suite
.on("cycle", (event) => {
.on("cycle", (event: Event) => {
console.log(String(event.target));
})
.run();
11 changes: 0 additions & 11 deletions packages/turf-unkink-polygon/index.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import { Polygon, MultiPolygon, Feature, FeatureCollection } from "geojson";
import { flattenEach, featureEach } from "@turf/meta";
import { polygon, featureCollection } from "@turf/helpers";
import { simplepolygon } from "./lib/simplepolygon.js";

/**
* Takes a kinked polygon and returns a feature collection of polygons that have no kinks.
* Takes a kinked polygon and returns a feature collection of polygons that have
* no kinks.
*
* Uses [simplepolygon](https://github.com/mclaeysb/simplepolygon) internally.
*
* @name unkinkPolygon
* @param {FeatureCollection|Feature<Polygon|MultiPolygon>} geojson GeoJSON Polygon or MultiPolygon
* @param {FeatureCollection<Polygon|MultiPolygon>|Feature<Polygon|MultiPolygon>|Polygon|MultiPolygon} geojson polygons to unkink
* @returns {FeatureCollection<Polygon>} Unkinked polygons
* @example
* var poly = turf.polygon([[[0, 0], [2, 0], [0, 2], [2, 2], [0, 0]]]);
* const poly = turf.polygon([[[0, 0], [2, 0], [0, 2], [2, 2], [0, 0]]]);
*
* var result = turf.unkinkPolygon(poly);
* const result = turf.unkinkPolygon(poly);
*
* //addToMap
* var addToMap = [poly, result]
* const addToMap = [poly, result]
*/
function unkinkPolygon(geojson) {
var features = [];
function unkinkPolygon<T extends Polygon | MultiPolygon>(
geojson: Feature<T> | FeatureCollection<T> | T
): FeatureCollection<Polygon> {
var features: Feature<Polygon>[] = [];
flattenEach(geojson, function (feature) {
if (feature.geometry.type !== "Polygon") return;
featureEach(simplepolygon(feature), function (poly) {
// Safe to treat feature as Feature<Polygon>
featureEach(simplepolygon(feature as Feature<Polygon>), function (poly) {
features.push(polygon(poly.geometry.coordinates, feature.properties));
});
});
Expand Down
8 changes: 8 additions & 0 deletions packages/turf-unkink-polygon/lib/simplepolygon.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Polygon, Feature, FeatureCollection } from "geojson";

declare function simplepolygon(
feature: Feature<Polygon>
): FeatureCollection<Polygon>;

export { simplepolygon };
export default simplepolygon;
5 changes: 4 additions & 1 deletion packages/turf-unkink-polygon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,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/area": "workspace:^",
"@turf/boolean-point-in-polygon": "workspace:^",
"@turf/helpers": "workspace:^",
"@turf/meta": "workspace:^",
"rbush": "^3.0.1"
"@types/geojson": "7946.0.8",
"rbush": "^3.0.1",
"tslib": "^2.6.2"
}
}
13 changes: 9 additions & 4 deletions packages/turf-unkink-polygon/test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Feature, FeatureCollection, Polygon } from "geojson";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
Expand All @@ -17,7 +18,9 @@ const directories = {
};

const fixtures = fs.readdirSync(directories.in).map((filename) => {
return { filename, geojson: loadJsonFileSync(directories.in + filename) };
const geojson: FeatureCollection<Polygon> | Feature<Polygon> =
loadJsonFileSync(directories.in + filename);
return { filename, geojson };
});

test("unkink-polygon", (t) => {
Expand Down Expand Up @@ -55,7 +58,7 @@ test("issue #2504", (t) => {
t.pass(
"large number of coordinates in a single ring should not cause an error"
);
} catch (e) {
} catch (e: any) {
t.fail(e);
}

Expand All @@ -68,17 +71,19 @@ test("unkink-polygon -- throws", (t) => {
t.true(value !== "isUnique", "isUnique");
t.true(value !== "getUnique", "getUnique");
}
// @ts-expect-error intentional non-existent function
t.throws(() => Array.isUnique(), "isUnique()");
// @ts-expect-error intentional non-existent function
t.throws(() => Array.getUnique(), "getUnique()");
t.end();
});

function colorize(
features,
features: FeatureCollection,
colors = ["#F00", "#00F", "#0F0", "#F0F", "#FFF"],
width = 6
) {
const results = [];
const results: Feature[] = [];
featureEach(features, (feature, index) => {
const color = colors[index % colors.length];
feature.properties = Object.assign(
Expand Down
9 changes: 9 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 584acbc

Please sign in to comment.