Skip to content

Commit

Permalink
added circle calculation around a GeoPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingeek committed Jun 8, 2020
1 parent 1ec2d8b commit c07430b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flyingeek/lidojs",
"version": "1.2.4",
"version": "1.2.5",
"description": "convert Lido OFP text files",
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
Expand Down
27 changes: 26 additions & 1 deletion src/modules/geopoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const nm_to_rad = (nm) => nm * NM / R;
const km_to_rad = (km) => km * 1000.0 / R;
const km_to_nm = (km) => km * 1000.0 / NM;

const fmod = (a, b) => Number((a - (Math.floor(a / b) * b)).toPrecision(8));
/**
* convert geo coordinates in degrees, minutes in signed fixed value
* N5500.0 => 55.00000000
Expand Down Expand Up @@ -273,7 +274,6 @@ class GeoPoint {
const phi1 = this.latphi.phi;
const rlat2 = other.latphi.rlat;
const phi2 = other.latphi.phi;
const fmod = (a, b) => Number((a - (Math.floor(a / b) * b)).toPrecision(8));
return fmod(
Math.atan2(
Math.sin(phi1 - phi2) * Math.cos(rlat2),
Expand Down Expand Up @@ -324,6 +324,31 @@ class GeoPoint {
return new GeoPoint(new LatPhi(rlat, phi).asLatLng);
}

/**
* Return points forming a circle around current points
* @param {int} radius distance in radians
* @param {int} steps number of points in the circle
* @param {function} converter converter to use for the radius
*/
circle(radius, steps=64, converter=nm_to_rad) {
if (converter) radius = converter(radius);
const destination = (d, tc) => {
const lat1 = this.latphi.rlat;
const lon1 = this.latphi.phi;
const rlat = Math.asin(Math.sin(lat1) * Math.cos(d) + Math.cos(lat1) *Math.sin(d) * Math.cos(tc));
let phi = lon1;
if (Math.cos(rlat) !== 0) {
phi = fmod(lon1 - Math.asin(Math.sin(tc) * Math.sin(d) / Math.cos(rlat)) +Math.PI, 2 * Math.PI) - Math.PI;
}
return new GeoPoint(new LatPhi(rlat, phi).asLatLng);
}
const points = [];
for (let i = 0; i < steps; i += 1) {
points.push(destination(radius, i * 2 * Math.PI / steps));
}
return points;
}

equals(other){
return (this.latitude.toFixed(6) === other.latitude.toFixed(6)
&& this.longitude.toFixed(6) === other.longitude.toFixed(6))
Expand Down
5 changes: 5 additions & 0 deletions test/geopoint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,8 @@ test("getCenter", () => {
center = GeoPoint.getCenter([g1, g2, g3]);
expect(center.equals(new GeoPoint([45.5, 0])));
});

test("circle", () => {
const g = new GeoPoint([0, 90]);
expect(g.circle(420).length).toEqual(64);
})

0 comments on commit c07430b

Please sign in to comment.