From c9c0a4c91c629f42a47d7768c0e4b652db324f5e Mon Sep 17 00:00:00 2001 From: James Beard Date: Tue, 9 Jul 2024 11:03:43 +1000 Subject: [PATCH] Converted turf-planepoint to Typescript (#2646) * Converted turf-planepoint to Typescript. --- packages/turf-planepoint/README.md | 33 +++++----- packages/turf-planepoint/bench.ts | 4 +- packages/turf-planepoint/index.d.ts | 13 ---- .../turf-planepoint/{index.js => index.ts} | 62 ++++++++++--------- packages/turf-planepoint/package.json | 9 ++- pnpm-lock.yaml | 15 +++++ 6 files changed, 73 insertions(+), 63 deletions(-) delete mode 100644 packages/turf-planepoint/index.d.ts rename packages/turf-planepoint/{index.js => index.ts} (53%) diff --git a/packages/turf-planepoint/README.md b/packages/turf-planepoint/README.md index 31dddf4324..d3044007b1 100644 --- a/packages/turf-planepoint/README.md +++ b/packages/turf-planepoint/README.md @@ -4,24 +4,25 @@ ## planepoint -Takes a triangular plane as a [Polygon][1] -and a [Point][2] within that triangle and returns the z-value -at that point. The Polygon should have properties `a`, `b`, and `c` +Takes a triangular plane as a polygon and a point within that triangle, and +returns the z-value at that point. + +The Polygon should have properties `a`, `b`, and `c` that define the values at its three corners. Alternatively, the z-values of each triangle point can be provided by their respective 3rd coordinate if their values are not provided as properties. ### Parameters -* `point` **[Coord][3]** the Point for which a z-value will be calculated -* `triangle` **[Feature][4]<[Polygon][5]>** a Polygon feature with three vertices +* `point` **[Coord][1]** the Point for which a z-value will be calculated +* `triangle` **[Feature][2]<[Polygon][3]>** a Polygon feature with three vertices ### Examples ```javascript -var point = turf.point([-75.3221, 39.529]); +const point = turf.point([-75.3221, 39.529]); // "a", "b", and "c" values represent the values of the coordinates in order. -var triangle = turf.polygon([[ +const triangle = turf.polygon([[ [-75.1221, 39.57], [-75.58, 39.18], [-75.97, 39.86], @@ -32,26 +33,22 @@ var triangle = turf.polygon([[ "c": 44 }); -var zValue = turf.planepoint(point, triangle); +const zValue = turf.planepoint(point, triangle); point.properties.zValue = zValue; //addToMap -var addToMap = [triangle, point]; +const addToMap = [triangle, point]; ``` -Returns **[number][6]** the z-value for `interpolatedPoint` - -[1]: https://tools.ietf.org/html/rfc7946#section-3.1.6 - -[2]: https://tools.ietf.org/html/rfc7946#section-3.1.2 +Returns **[number][4]** the z-value for `interpolatedPoint` -[3]: https://tools.ietf.org/html/rfc7946#section-3.1.1 +[1]: https://tools.ietf.org/html/rfc7946#section-3.1.1 -[4]: https://tools.ietf.org/html/rfc7946#section-3.2 +[2]: https://tools.ietf.org/html/rfc7946#section-3.2 -[5]: https://tools.ietf.org/html/rfc7946#section-3.1.6 +[3]: https://tools.ietf.org/html/rfc7946#section-3.1.6 -[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number +[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number diff --git a/packages/turf-planepoint/bench.ts b/packages/turf-planepoint/bench.ts index 345ad33e67..f54449e439 100644 --- a/packages/turf-planepoint/bench.ts +++ b/packages/turf-planepoint/bench.ts @@ -1,4 +1,4 @@ -import Benchmark from "benchmark"; +import Benchmark, { Event } from "benchmark"; import { polygon } from "@turf/helpers"; import { planepoint } from "./index.js"; @@ -23,5 +23,5 @@ const triangle = polygon( const suite = new Benchmark.Suite("turf-planepoint"); suite .add("turf-planepoint", () => planepoint(point, triangle)) - .on("cycle", (e) => console.log(String(e.target))) + .on("cycle", (e: Event) => console.log(String(e.target))) .run(); diff --git a/packages/turf-planepoint/index.d.ts b/packages/turf-planepoint/index.d.ts deleted file mode 100644 index bbb3e58973..0000000000 --- a/packages/turf-planepoint/index.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Feature, Polygon } from "geojson"; -import { Coord } from "@turf/helpers"; - -/** - * http://turfjs.org/docs/#planepoint - */ -declare function planepoint( - point: Coord, - triangle: Feature | Polygon -): number; - -export { planepoint }; -export default planepoint; diff --git a/packages/turf-planepoint/index.js b/packages/turf-planepoint/index.ts similarity index 53% rename from packages/turf-planepoint/index.js rename to packages/turf-planepoint/index.ts index 0b6823b39a..9e6e531727 100644 --- a/packages/turf-planepoint/index.js +++ b/packages/turf-planepoint/index.ts @@ -1,9 +1,12 @@ +import { Feature, Polygon } from "geojson"; import { getCoord, getGeom } from "@turf/invariant"; +import { Coord } from "@turf/helpers"; /** - * Takes a triangular plane as a {@link Polygon} - * and a {@link Point} within that triangle and returns the z-value - * at that point. The Polygon should have properties `a`, `b`, and `c` + * Takes a triangular plane as a polygon and a point within that triangle, and + * returns the z-value at that point. + * + * The Polygon should have properties `a`, `b`, and `c` * that define the values at its three corners. Alternatively, the z-values * of each triangle point can be provided by their respective 3rd coordinate * if their values are not provided as properties. @@ -13,9 +16,9 @@ import { getCoord, getGeom } from "@turf/invariant"; * @param {Feature} triangle a Polygon feature with three vertices * @returns {number} the z-value for `interpolatedPoint` * @example - * var point = turf.point([-75.3221, 39.529]); + * const point = turf.point([-75.3221, 39.529]); * // "a", "b", and "c" values represent the values of the coordinates in order. - * var triangle = turf.polygon([[ + * const triangle = turf.polygon([[ * [-75.1221, 39.57], * [-75.58, 39.18], * [-75.97, 39.86], @@ -26,38 +29,41 @@ import { getCoord, getGeom } from "@turf/invariant"; * "c": 44 * }); * - * var zValue = turf.planepoint(point, triangle); + * const zValue = turf.planepoint(point, triangle); * point.properties.zValue = zValue; * * //addToMap - * var addToMap = [triangle, point]; + * const addToMap = [triangle, point]; */ -function planepoint(point, triangle) { +function planepoint( + point: Coord, + triangle: Feature | Polygon +): number { // Normalize input - var coord = getCoord(point); - var geom = getGeom(triangle); - var coords = geom.coordinates; - var outer = coords[0]; + const coord = getCoord(point); + const geom = getGeom(triangle); + const coords = geom.coordinates; + const outer = coords[0]; if (outer.length < 4) throw new Error("OuterRing of a Polygon must have 4 or more Positions."); - var properties = triangle.properties || {}; - var a = properties.a; - var b = properties.b; - var c = properties.c; + const properties = (triangle.type === "Feature" && triangle.properties) || {}; + const a = properties.a; + const b = properties.b; + const c = properties.c; // Planepoint - var x = coord[0]; - var y = coord[1]; - var x1 = outer[0][0]; - var y1 = outer[0][1]; - var z1 = a !== undefined ? a : outer[0][2]; - var x2 = outer[1][0]; - var y2 = outer[1][1]; - var z2 = b !== undefined ? b : outer[1][2]; - var x3 = outer[2][0]; - var y3 = outer[2][1]; - var z3 = c !== undefined ? c : outer[2][2]; - var z = + const x = coord[0]; + const y = coord[1]; + const x1 = outer[0][0]; + const y1 = outer[0][1]; + const z1 = a !== undefined ? a : outer[0][2]; + const x2 = outer[1][0]; + const y2 = outer[1][1]; + const z2 = b !== undefined ? b : outer[1][2]; + const x3 = outer[2][0]; + const y3 = outer[2][1]; + const z3 = c !== undefined ? c : outer[2][2]; + const z = (z3 * (x - x1) * (y - y2) + z1 * (x - x2) * (y - y3) + z2 * (x - x3) * (y - y1) - diff --git a/packages/turf-planepoint/package.json b/packages/turf-planepoint/package.json index fbf4297dde..d86e15c167 100644 --- a/packages/turf-planepoint/package.json +++ b/packages/turf-planepoint/package.json @@ -53,14 +53,19 @@ "test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts" }, "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:^", - "@turf/invariant": "workspace:^" + "@turf/invariant": "workspace:^", + "@types/geojson": "7946.0.8", + "tslib": "^2.6.2" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 758883e459..b25018b3d4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4443,7 +4443,19 @@ importers: '@turf/invariant': specifier: workspace:^ version: link:../turf-invariant + '@types/geojson': + specifier: 7946.0.8 + version: 7946.0.8 + tslib: + specifier: ^2.6.2 + version: 2.6.2 devDependencies: + '@types/benchmark': + specifier: ^2.1.5 + version: 2.1.5 + '@types/tape': + specifier: ^4.2.32 + version: 4.13.4 benchmark: specifier: ^2.1.4 version: 2.1.4 @@ -4459,6 +4471,9 @@ importers: tsx: specifier: ^4.6.2 version: 4.6.2 + typescript: + specifier: ^5.2.2 + version: 5.3.3 packages/turf-point-grid: dependencies: