aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShinigami <[email protected]>2022-03-25 19:25:54 +0100
committerGitHub <[email protected]>2022-03-25 19:25:54 +0100
commit11ebb0b0c85d73647df587991e46827981042bfd (patch)
treededd5f1028b6cb406088aebce4182fa25e8ba2d9 /src
parenta299c22c375538112c52ee0ad69902c87496f78c (diff)
downloadfaker-11ebb0b0c85d73647df587991e46827981042bfd.tar.xz
faker-11ebb0b0c85d73647df587991e46827981042bfd.zip
refactor: use number tuple as input for coordinate (#668)
Diffstat (limited to 'src')
-rw-r--r--src/address.ts123
1 files changed, 73 insertions, 50 deletions
diff --git a/src/address.ts b/src/address.ts
index f60377ee..b1770003 100644
--- a/src/address.ts
+++ b/src/address.ts
@@ -5,6 +5,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.
*/
export class Address {
@@ -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.
+ // 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;