consolidation of various example repos/gists

This commit is contained in:
2021-07-15 11:36:18 +02:00
parent 58142d3157
commit 16953bd936
20 changed files with 978 additions and 1 deletions

View File

@@ -0,0 +1,52 @@
import { Point } from "./point";
/**
* Converts an angle in radians to an angle in degrees by multiplying with 180 and dividing by 180
* @param angleInRadians
*/
export const radiansToDegrees = (angleInRadians: number) => {
return (angleInRadians * 180) / Math.PI;
};
/**
* Convert an angle in degrees to an angle in radians by multiplying by PI and dividing the result by 180
* https://www.mathwarehouse.com/trigonometry/radians/convert-degee-to-radians.php
* @param angleInDegrees
*/
export const degreesToRadians = (angleInDegrees: number) => {
return (angleInDegrees * Math.PI) / 180;
};
/**
* Convert a point object to an array of longitude,latitude
* @param point
*/
export const pointToLongLatArray = (point: Point) => {
return [point.longitude, point.latitude];
};
/**
* Get a new point given a point, distance and bearing
* https://www.igismap.com/formula-to-find-bearing-or-heading-angle-between-two-points-latitude-longitude/#:~:text=Here%20is%20the%20formula%20to,%E2%80%93%20sin%20la1%20*%20sin%20la2)
*/
export const getRelativePointByDistance = (
point: Point,
distance: number,
bearing: number
): Point => {
const latitude = Math.asin(
Math.sin(point.latitude) * Math.cos(distance) +
Math.cos(point.latitude) * Math.sin(distance) * Math.cos(bearing)
);
const longitude =
point.longitude +
Math.atan2(
Math.sin(bearing) * Math.sin(distance) * Math.cos(point.latitude),
Math.cos(distance) - Math.sin(point.latitude) * Math.sin(latitude)
);
return {
latitude: radiansToDegrees(latitude),
longitude: radiansToDegrees(longitude),
};
};

View File

@@ -0,0 +1,55 @@
import {
degreesToRadians,
getRelativePointByDistance,
pointToLongLatArray,
} from "./helpers";
import { Point } from "./point";
/**
*
*/
const options = {
/**
* IERS Equatorial Radius of the earth
* https://en.wikipedia.org/wiki/Earth_ellipsoid
*/
equatorialRadiusInMeters: 6378136.6,
/**
* Number of edges to create in the polygon
* Use 360 to get a true circle
*/
numberOfEdges: 64,
};
const input = {
center: { latitude: 51.7543453, longitude: 5.5526647 } as Point,
radius: 2000,
};
const coordinates = [];
for (var i = 0; i < options.numberOfEdges; ++i) {
const newPoint = getRelativePointByDistance(
{
latitude: degreesToRadians(input.center.latitude),
longitude: degreesToRadians(input.center.longitude),
},
input.radius / options.equatorialRadiusInMeters,
// find bearing https://www.igismap.com/formula-to-find-bearing-or-heading-angle-between-two-points-latitude-longitude/
(2 * Math.PI * -i) / options.numberOfEdges
);
coordinates.push(pointToLongLatArray(newPoint));
}
// make sure the circle is closed by pushing a copy of the first element
coordinates.push(coordinates[0]);
console.log(
JSON.stringify(
{
type: "Polygon",
coordinates: [coordinates],
},
null,
2
)
);

View File

@@ -0,0 +1,4 @@
export interface Point {
latitude: number;
longitude: number;
}