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