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

Typescript-ifying turf-great-circle #2733

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion packages/turf-great-circle/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ suite
.add("greatCircle", () => {
greatCircle(point1, point2);
})
.on("cycle", (e) => console.log(String(e.target)))
.on("cycle", (e: any) => console.log(String(e.target)))
.run();
23 changes: 0 additions & 23 deletions packages/turf-great-circle/index.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import type {
Feature,
GeoJsonProperties,
LineString,
MultiLineString,
Point,
Position,
} from "geojson";
import { lineString } from "@turf/helpers";
import { getCoord } from "@turf/invariant";
import { GreatCircle } from "./lib/arc.js";
Expand All @@ -8,7 +16,7 @@ import { GreatCircle } from "./lib/arc.js";
* be split into a `MultiLineString`. If the `start` and `end` positions are the same
* then a `LineString` will be returned with duplicate coordinates the length of the `npoints` option.
*
* @function
* @name greatCircle
* @param {Coord} start source point feature
* @param {Coord} end destination point feature
* @param {Object} [options={}] Optional parameters
Expand All @@ -26,35 +34,34 @@ import { GreatCircle } from "./lib/arc.js";
* //addToMap
* var addToMap = [start, end, greatCircle]
*/
function greatCircle(start, end, options) {
function greatCircle(
start: Feature<Point, GeoJsonProperties> | Point | Position,
end: Feature<Point, GeoJsonProperties> | Point | Position,
options: {
properties?: GeoJsonProperties;
npoints?: number;
offset?: number;
} = {}
): Feature<LineString | MultiLineString> {
// Optional parameters
options = options || {};
if (typeof options !== "object") throw new Error("options is invalid");
var properties = options.properties;
var npoints = options.npoints;
var offset = options.offset;
const { properties = {}, npoints = 100, offset = 10 } = options;

start = getCoord(start);
end = getCoord(end);
const startCoord = getCoord(start);
const endCoord = getCoord(end);

properties = properties || {};
npoints = npoints || 100;

if (start[0] === end[0] && start[1] === end[1]) {
const arr = Array(npoints);
arr.fill([start[0], start[1]]);
if (startCoord[0] === endCoord[0] && startCoord[1] === endCoord[1]) {
const arr = Array(npoints).fill([startCoord[0], startCoord[1]]);
return lineString(arr, properties);
}

offset = offset || 10;

var generator = new GreatCircle(
{ x: start[0], y: start[1] },
{ x: end[0], y: end[1] },
const generator = new GreatCircle(
{ x: startCoord[0], y: startCoord[1] },
{ x: endCoord[0], y: endCoord[1] },
properties
);

var line = generator.Arc(npoints, { offset: offset });
const line = generator.Arc(npoints, { offset: offset });

return line.json();
}
Expand Down
8 changes: 8 additions & 0 deletions packages/turf-great-circle/lib/arc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class GreatCircle {
constructor(
start: { x: number; y: number },
end: { x: number; y: number },
properties?: any
);
Arc(npoints: number, options: { offset: number }): { json: () => any };
}
7 changes: 5 additions & 2 deletions packages/turf-great-circle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"contributors": [
"Dane Springmeyer <@springmeyer>",
"Stepan Kuzmin <@stepankuzmin>",
"Denis Carriere <@DenisCarriere>"
"Denis Carriere <@DenisCarriere>",
"Thomas Hervey <@thomas-hervey>"
],
"license": "MIT",
"bugs": {
Expand Down Expand Up @@ -67,11 +68,13 @@
"tape": "^5.7.2",
"tsup": "^8.0.1",
"tsx": "^4.6.2",
"typescript": "^5.5.4",
"write-json-file": "^5.0.0"
},
"dependencies": {
"@turf/helpers": "workspace:^",
"@turf/invariant": "workspace:^",
"@types/geojson": "^7946.0.10"
"@types/geojson": "^7946.0.10",
"tslib": "^2.6.2"
}
}
92 changes: 85 additions & 7 deletions packages/turf-great-circle/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
import type {
FeatureCollection,
LineString,
Feature,
Geometry,
Point,
} from "geojson";
import { truncate } from "@turf/truncate";
import { featureCollection, point, lineString } from "@turf/helpers";
import { greatCircle } from "./index.js";
Expand All @@ -15,23 +22,32 @@ const directories = {
out: path.join(__dirname, "test", "out") + path.sep,
};

let fixtures = fs.readdirSync(directories.in).map((filename) => {
const fixtures = fs.readdirSync(directories.in).map((filename) => {
return {
filename,
name: path.parse(filename).name,
geojson: loadJsonFileSync(path.join(directories.in, filename)),
geojson: loadJsonFileSync(
path.join(directories.in, filename)
) as FeatureCollection,
};
});

// Function to get the start and end points from the fixture
function getStartEndPoints(fixture: (typeof fixtures)[0]) {
const geojson = fixture.geojson;
const start = geojson.features[0] as Feature<Point>;
const end = geojson.features[1] as Feature<Point>;
return { start, end };
}

test("turf-great-circle", (t) => {
fixtures.forEach((fixture) => {
const name = fixture.name;
const filename = fixture.filename;
const geojson = fixture.geojson;
const start = geojson.features[0];
const end = geojson.features[1];
const { start, end } = getStartEndPoints(fixture);

const line = truncate(greatCircle(start, end));
const results = featureCollection([line, start, end]);
const results = featureCollection<Geometry>([line, start, end]);

if (process.env.REGEN)
writeJsonFileSync(directories.out + filename, results);
Expand All @@ -54,8 +70,70 @@ test("turf-great-circle with same input and output", (t) => {
[0, 0],
[0, 0],
]),
line
line as Feature<LineString>
);

t.end();
});

test("turf-great-circle accepts Feature<Point> inputs", (t) => {
const { start, end } = getStartEndPoints(fixtures[0]);
t.doesNotThrow(
() => greatCircle(start, end),
"accepts Feature<Point> inputs"
);
t.end();
});

test("turf-great-circle accepts Point geometry inputs", (t) => {
const { start, end } = getStartEndPoints(fixtures[0]);
t.doesNotThrow(
() => greatCircle(start.geometry, end.geometry),
"accepts Point geometry inputs"
);
t.end();
});

test("turf-great-circle accepts Position inputs", (t) => {
const { start, end } = getStartEndPoints(fixtures[0]);
t.doesNotThrow(
() => greatCircle(start.geometry.coordinates, end.geometry.coordinates),
"accepts Position inputs"
);
t.end();
});

test("turf-great-circle applies custom properties", (t) => {
const { start, end } = getStartEndPoints(fixtures[0]);
const withProperties = greatCircle(start, end, {
properties: { name: "Test Route" },
});
t.equal(
withProperties.properties?.name,
"Test Route",
"applies custom properties"
);
t.end();
});

test("turf-great-circle respects npoints option", (t) => {
const { start, end } = getStartEndPoints(fixtures[0]);
const withCustomPoints = greatCircle(start, end, { npoints: 5 });
t.equal(
(withCustomPoints.geometry as LineString).coordinates.length,
5,
"respects npoints option"
);
t.end();
});

test("turf-great-circle respects offset and npoints options", (t) => {
const { start, end } = getStartEndPoints(fixtures[0]);
const withOffset = greatCircle(start, end, { offset: 100, npoints: 10 });
t.equal(
(withOffset.geometry as LineString).coordinates.length,
10,
"respects offset and npoints options"
);
t.end();
});
Loading