Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math from 'mathjs';
- // convert degrees (lat/long) to radians
- const radians = (deg) => (deg / 360) * 2 * Math.PI;
- // create a set of points around the origin at the given radius
- const rotatedPointsOrigin = (radius, sides) => {
- const z = new math.complex({ phi: 0, r: radius });
- const angle = (2 * Math.PI) / sides;
- return map(range(sides), (x) => {
- const i = math.complex(0, 1);
- const point = math.multiply(z, math.exp(math.multiply(x, angle, math.complex(0, 1))));
- return [math.re(point), math.im(point)];
- });
- };
- // scale points to coordinate system
- // const latFactor = 69.172 // if miles
- const latFactor = 111321.543 // if meters
- const scalePoints = (points, lat) => {
- const lngFactor = math.cos(radians(lat)) * latFactor;
- return map(points, (x) => [x[0] / latFactor, x[1] / lngFactor]);
- };
- // move this circle to the correct place on the map
- const translatePoints = (points, center) => map(
- points,
- (point) => [point[0] + center[0], point[1] + center[1]],
- );
- // create GeoJSON feature from circle
- // (using a 24 sided approximation by default)
- export const generateCircleApprox = (radius, unit, center, sides = 24) => {
- console.log('stuff', radius, unit, center, sides);
- const points = rotatedPointsOrigin(radius, sides);
- const scaledPoints = scalePoints(points, center[0]);
- const translatedPoints = translatePoints(scaledPoints, center);
- const reversedPoints = map(translatedPoints, (x) => reverse(x));
- return {
- type: 'Feature',
- properties: {
- radius,
- unit,
- center,
- sides,
- },
- geometry: {
- type: 'MultiPolygon',
- coordinates: [[reversedPoints]],
- },
- };
- };
Add Comment
Please, Sign In to add comment