Guest User

Untitled

a guest
Oct 17th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import math from 'mathjs';
  2.  
  3. // convert degrees (lat/long) to radians
  4. const radians = (deg) => (deg / 360) * 2 * Math.PI;
  5.  
  6. // create a set of points around the origin at the given radius
  7. const rotatedPointsOrigin = (radius, sides) => {
  8. const z = new math.complex({ phi: 0, r: radius });
  9. const angle = (2 * Math.PI) / sides;
  10. return map(range(sides), (x) => {
  11. const i = math.complex(0, 1);
  12. const point = math.multiply(z, math.exp(math.multiply(x, angle, math.complex(0, 1))));
  13. return [math.re(point), math.im(point)];
  14. });
  15. };
  16.  
  17. // scale points to coordinate system
  18. // const latFactor = 69.172 // if miles
  19. const latFactor = 111321.543 // if meters
  20. const scalePoints = (points, lat) => {
  21. const lngFactor = math.cos(radians(lat)) * latFactor;
  22. return map(points, (x) => [x[0] / latFactor, x[1] / lngFactor]);
  23. };
  24.  
  25. // move this circle to the correct place on the map
  26. const translatePoints = (points, center) => map(
  27. points,
  28. (point) => [point[0] + center[0], point[1] + center[1]],
  29. );
  30.  
  31. // create GeoJSON feature from circle
  32. // (using a 24 sided approximation by default)
  33. export const generateCircleApprox = (radius, unit, center, sides = 24) => {
  34. console.log('stuff', radius, unit, center, sides);
  35. const points = rotatedPointsOrigin(radius, sides);
  36. const scaledPoints = scalePoints(points, center[0]);
  37. const translatedPoints = translatePoints(scaledPoints, center);
  38. const reversedPoints = map(translatedPoints, (x) => reverse(x));
  39. return {
  40. type: 'Feature',
  41. properties: {
  42. radius,
  43. unit,
  44. center,
  45. sides,
  46. },
  47. geometry: {
  48. type: 'MultiPolygon',
  49. coordinates: [[reversedPoints]],
  50. },
  51. };
  52. };
Add Comment
Please, Sign In to add comment