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

Converted turf-midpoint to Typescript #2645

Merged
merged 3 commits into from
Jul 9, 2024
Merged
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
27 changes: 13 additions & 14 deletions packages/turf-midpoint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,35 @@

## midpoint

Takes two [points][1] and returns a point midway between them.
The midpoint is calculated geodesically, meaning the curvature of the earth is taken into account.
Takes two points and returns a point midway between them. The midpoint is
calculated geodesically, meaning the curvature of the earth is taken into
account.

### Parameters

* `point1` **[Coord][2]** first point
* `point2` **[Coord][2]** second point
* `point1` **[Coord][1]** first point
* `point2` **[Coord][1]** second point

### Examples

```javascript
var point1 = turf.point([144.834823, -37.771257]);
var point2 = turf.point([145.14244, -37.830937]);
const point1 = turf.point([144.834823, -37.771257]);
const point2 = turf.point([145.14244, -37.830937]);

var midpoint = turf.midpoint(point1, point2);
const midpoint = turf.midpoint(point1, point2);

//addToMap
var addToMap = [point1, point2, midpoint];
const addToMap = [point1, point2, midpoint];
midpoint.properties['marker-color'] = '#f00';
```

Returns **[Feature][3]<[Point][4]>** a point midway between `pt1` and `pt2`
Returns **[Feature][2]<[Point][3]>** a point midway between `pt1` and `pt2`

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

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

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

[4]: https://tools.ietf.org/html/rfc7946#section-3.1.2
[3]: https://tools.ietf.org/html/rfc7946#section-3.1.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
4 changes: 2 additions & 2 deletions packages/turf-midpoint/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 { point } from "@turf/helpers";
import { midpoint } from "./index.js";

Expand All @@ -9,7 +9,7 @@ new Benchmark.Suite("turf-midpoint")
.add("turf-midpoint", function () {
midpoint(pt1, pt2);
})
.on("cycle", function (event) {
.on("cycle", function (event: Event) {
console.log(String(event.target));
})
.run();
10 changes: 0 additions & 10 deletions packages/turf-midpoint/index.d.ts

This file was deleted.

32 changes: 0 additions & 32 deletions packages/turf-midpoint/index.js

This file was deleted.

35 changes: 35 additions & 0 deletions packages/turf-midpoint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Feature, Point } from "geojson";
import { bearing } from "@turf/bearing";
import { destination } from "@turf/destination";
import { distance } from "@turf/distance";
import { Coord } from "@turf/helpers";

/**
* Takes two points and returns a point midway between them. The midpoint is
* calculated geodesically, meaning the curvature of the earth is taken into
* account.
*
* @name midpoint
* @param {Coord} point1 first point
* @param {Coord} point2 second point
* @returns {Feature<Point>} a point midway between `pt1` and `pt2`
* @example
* const point1 = turf.point([144.834823, -37.771257]);
* const point2 = turf.point([145.14244, -37.830937]);
*
* const midpoint = turf.midpoint(point1, point2);
*
* //addToMap
* const addToMap = [point1, point2, midpoint];
* midpoint.properties['marker-color'] = '#f00';
*/
function midpoint(point1: Coord, point2: Coord): Feature<Point> {
const dist = distance(point1, point2);
const heading = bearing(point1, point2);
const midpoint = destination(point1, dist / 2, heading);

return midpoint;
}

export { midpoint };
export default midpoint;
8 changes: 6 additions & 2 deletions packages/turf-midpoint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,20 @@
},
"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/bearing": "workspace:^",
"@turf/destination": "workspace:^",
"@turf/distance": "workspace:^",
"@turf/helpers": "workspace:^"
"@turf/helpers": "workspace:^",
"@types/geojson": "7946.0.8",
"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.

Loading