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

refactor: use number tuple as input for coordinate #668

Merged
merged 4 commits into from
Mar 25, 2022
Merged
Changes from 2 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
123 changes: 73 additions & 50 deletions src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,75 @@ import type { Helpers } from './helpers';

let f: Fake['fake'];

/**
* Converts degrees to radians.
*
* @param degrees Degrees.
*/
function degreesToRadians(degrees: number): number {
return degrees * (Math.PI / 180.0);
}

/**
* Converts radians to degrees.
*
* @param radians Radians.
*/
function radiansToDegrees(radians: number): number {
return radians * (180.0 / Math.PI);
}

/**
* Converts kilometers to miles.
*
* @param miles Miles.
*/
function kilometersToMiles(miles: number): number {
return miles * 0.621371;
}

/**
* Calculates coordinates with offset.
*
* @param coordinate Coordinate.
* @param bearing Bearing.
* @param distance Distance.
* @param isMetric Metric: true, Miles: false.
*/
function coordinateWithOffset(
coordinate: [number, number],
bearing: number,
distance: number,
isMetric: boolean
): number[] {
const R = 6378.137; // Radius of the Earth (http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html)
const d = isMetric ? distance : kilometersToMiles(distance); // Distance in km

const lat1 = degreesToRadians(coordinate[0]); //Current lat point converted to radians
const lon1 = degreesToRadians(coordinate[1]); //Current long point converted to radians

const lat2 = Math.asin(
Math.sin(lat1) * Math.cos(d / R) +
Math.cos(lat1) * Math.sin(d / R) * Math.cos(bearing)
);

let lon2 =
lon1 +
Math.atan2(
Math.sin(bearing) * Math.sin(d / R) * Math.cos(lat1),
Math.cos(d / R) - Math.sin(lat1) * Math.sin(lat2)
);

// Keep longitude in range [-180, 180]
if (lon2 > degreesToRadians(180)) {
lon2 = lon2 - degreesToRadians(360);
} else if (lon2 < degreesToRadians(-180)) {
lon2 = lon2 + degreesToRadians(360);
}

return [radiansToDegrees(lat2), radiansToDegrees(lon2)];
}

/**
* Module to generate addresses and locations.
*/
Expand Down Expand Up @@ -448,63 +517,17 @@ export class Address {
* faker.address.nearbyGPSCoordinate([33, -170]) // [ '33.0165', '-170.0636' ]
* faker.address.nearbyGPSCoordinate([33, -170], 1000, true) // [ '37.9163', '-179.2408' ]
*/
// TODO ST-DDT 2022-02-10: This should use either string or number coords.
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
// TODO ST-DDT 2022-02-10: Allow coordinate parameter to be [string, string].
nearbyGPSCoordinate(
coordinate?: number[],
coordinate?: [number, number],
radius?: number,
isMetric?: boolean
): string[] {
// TODO ST-DDT 2022-02-10: Remove unused code.
function randomFloat(min: number, max: number): number {
return Math.random() * (max - min) + min;
}
function degreesToRadians(degrees: number): number {
return degrees * (Math.PI / 180.0);
}
function radiansToDegrees(radians: number): number {
return radians * (180.0 / Math.PI);
}
function kilometersToMiles(miles: number): number {
return miles * 0.621371;
}
function coordinateWithOffset(
coordinate: number[],
bearing: number,
distance: number,
isMetric: boolean
): number[] {
const R = 6378.137; // Radius of the Earth (http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html)
const d = isMetric ? distance : kilometersToMiles(distance); // Distance in km

const lat1 = degreesToRadians(coordinate[0]); //Current lat point converted to radians
const lon1 = degreesToRadians(coordinate[1]); //Current long point converted to radians

const lat2 = Math.asin(
Math.sin(lat1) * Math.cos(d / R) +
Math.cos(lat1) * Math.sin(d / R) * Math.cos(bearing)
);

let lon2 =
lon1 +
Math.atan2(
Math.sin(bearing) * Math.sin(d / R) * Math.cos(lat1),
Math.cos(d / R) - Math.sin(lat1) * Math.sin(lat2)
);

// Keep longitude in range [-180, 180]
if (lon2 > degreesToRadians(180)) {
lon2 = lon2 - degreesToRadians(360);
} else if (lon2 < degreesToRadians(-180)) {
lon2 = lon2 + degreesToRadians(360);
}

return [radiansToDegrees(lat2), radiansToDegrees(lon2)];
}

): [string, string] {
// If there is no coordinate, the best we can do is return a random GPS coordinate.
if (coordinate === undefined) {
return [this.faker.address.latitude(), this.faker.address.longitude()];
}

radius = radius || 10.0;
isMetric = isMetric || false;

Expand Down