Skip to content

Commit

Permalink
Converted turf-planepoint to Typescript (#2646)
Browse files Browse the repository at this point in the history
* Converted turf-planepoint to Typescript.
  • Loading branch information
smallsaucepan authored Jul 9, 2024
1 parent 6012328 commit c9c0a4c
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 63 deletions.
33 changes: 15 additions & 18 deletions packages/turf-planepoint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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

<!-- 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-planepoint/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 { planepoint } from "./index.js";

Expand All @@ -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();
13 changes: 0 additions & 13 deletions packages/turf-planepoint/index.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -13,9 +16,9 @@ import { getCoord, getGeom } from "@turf/invariant";
* @param {Feature<Polygon>} 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],
Expand All @@ -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> | 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) -
Expand Down
9 changes: 7 additions & 2 deletions packages/turf-planepoint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
15 changes: 15 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 c9c0a4c

Please sign in to comment.