graticule.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {range} from "d3-array";
  2. import {abs, ceil, epsilon} from "./math.js";
  3. function graticuleX(y0, y1, dy) {
  4. var y = range(y0, y1 - epsilon, dy).concat(y1);
  5. return function(x) { return y.map(function(y) { return [x, y]; }); };
  6. }
  7. function graticuleY(x0, x1, dx) {
  8. var x = range(x0, x1 - epsilon, dx).concat(x1);
  9. return function(y) { return x.map(function(x) { return [x, y]; }); };
  10. }
  11. export default function graticule() {
  12. var x1, x0, X1, X0,
  13. y1, y0, Y1, Y0,
  14. dx = 10, dy = dx, DX = 90, DY = 360,
  15. x, y, X, Y,
  16. precision = 2.5;
  17. function graticule() {
  18. return {type: "MultiLineString", coordinates: lines()};
  19. }
  20. function lines() {
  21. return range(ceil(X0 / DX) * DX, X1, DX).map(X)
  22. .concat(range(ceil(Y0 / DY) * DY, Y1, DY).map(Y))
  23. .concat(range(ceil(x0 / dx) * dx, x1, dx).filter(function(x) { return abs(x % DX) > epsilon; }).map(x))
  24. .concat(range(ceil(y0 / dy) * dy, y1, dy).filter(function(y) { return abs(y % DY) > epsilon; }).map(y));
  25. }
  26. graticule.lines = function() {
  27. return lines().map(function(coordinates) { return {type: "LineString", coordinates: coordinates}; });
  28. };
  29. graticule.outline = function() {
  30. return {
  31. type: "Polygon",
  32. coordinates: [
  33. X(X0).concat(
  34. Y(Y1).slice(1),
  35. X(X1).reverse().slice(1),
  36. Y(Y0).reverse().slice(1))
  37. ]
  38. };
  39. };
  40. graticule.extent = function(_) {
  41. if (!arguments.length) return graticule.extentMinor();
  42. return graticule.extentMajor(_).extentMinor(_);
  43. };
  44. graticule.extentMajor = function(_) {
  45. if (!arguments.length) return [[X0, Y0], [X1, Y1]];
  46. X0 = +_[0][0], X1 = +_[1][0];
  47. Y0 = +_[0][1], Y1 = +_[1][1];
  48. if (X0 > X1) _ = X0, X0 = X1, X1 = _;
  49. if (Y0 > Y1) _ = Y0, Y0 = Y1, Y1 = _;
  50. return graticule.precision(precision);
  51. };
  52. graticule.extentMinor = function(_) {
  53. if (!arguments.length) return [[x0, y0], [x1, y1]];
  54. x0 = +_[0][0], x1 = +_[1][0];
  55. y0 = +_[0][1], y1 = +_[1][1];
  56. if (x0 > x1) _ = x0, x0 = x1, x1 = _;
  57. if (y0 > y1) _ = y0, y0 = y1, y1 = _;
  58. return graticule.precision(precision);
  59. };
  60. graticule.step = function(_) {
  61. if (!arguments.length) return graticule.stepMinor();
  62. return graticule.stepMajor(_).stepMinor(_);
  63. };
  64. graticule.stepMajor = function(_) {
  65. if (!arguments.length) return [DX, DY];
  66. DX = +_[0], DY = +_[1];
  67. return graticule;
  68. };
  69. graticule.stepMinor = function(_) {
  70. if (!arguments.length) return [dx, dy];
  71. dx = +_[0], dy = +_[1];
  72. return graticule;
  73. };
  74. graticule.precision = function(_) {
  75. if (!arguments.length) return precision;
  76. precision = +_;
  77. x = graticuleX(y0, y1, 90);
  78. y = graticuleY(x0, x1, precision);
  79. X = graticuleX(Y0, Y1, 90);
  80. Y = graticuleY(X0, X1, precision);
  81. return graticule;
  82. };
  83. return graticule
  84. .extentMajor([[-180, -90 + epsilon], [180, 90 - epsilon]])
  85. .extentMinor([[-180, -80 - epsilon], [180, 80 + epsilon]]);
  86. }
  87. export function graticule10() {
  88. return graticule()();
  89. }