| 1 | import {Adder} from "d3-array";
|
|---|
| 2 | import {abs, atan2, cos, radians, sin, sqrt} from "./math.js";
|
|---|
| 3 | import noop from "./noop.js";
|
|---|
| 4 | import stream from "./stream.js";
|
|---|
| 5 |
|
|---|
| 6 | var lengthSum,
|
|---|
| 7 | lambda0,
|
|---|
| 8 | sinPhi0,
|
|---|
| 9 | cosPhi0;
|
|---|
| 10 |
|
|---|
| 11 | var lengthStream = {
|
|---|
| 12 | sphere: noop,
|
|---|
| 13 | point: noop,
|
|---|
| 14 | lineStart: lengthLineStart,
|
|---|
| 15 | lineEnd: noop,
|
|---|
| 16 | polygonStart: noop,
|
|---|
| 17 | polygonEnd: noop
|
|---|
| 18 | };
|
|---|
| 19 |
|
|---|
| 20 | function lengthLineStart() {
|
|---|
| 21 | lengthStream.point = lengthPointFirst;
|
|---|
| 22 | lengthStream.lineEnd = lengthLineEnd;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | function lengthLineEnd() {
|
|---|
| 26 | lengthStream.point = lengthStream.lineEnd = noop;
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | function lengthPointFirst(lambda, phi) {
|
|---|
| 30 | lambda *= radians, phi *= radians;
|
|---|
| 31 | lambda0 = lambda, sinPhi0 = sin(phi), cosPhi0 = cos(phi);
|
|---|
| 32 | lengthStream.point = lengthPoint;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | function lengthPoint(lambda, phi) {
|
|---|
| 36 | lambda *= radians, phi *= radians;
|
|---|
| 37 | var sinPhi = sin(phi),
|
|---|
| 38 | cosPhi = cos(phi),
|
|---|
| 39 | delta = abs(lambda - lambda0),
|
|---|
| 40 | cosDelta = cos(delta),
|
|---|
| 41 | sinDelta = sin(delta),
|
|---|
| 42 | x = cosPhi * sinDelta,
|
|---|
| 43 | y = cosPhi0 * sinPhi - sinPhi0 * cosPhi * cosDelta,
|
|---|
| 44 | z = sinPhi0 * sinPhi + cosPhi0 * cosPhi * cosDelta;
|
|---|
| 45 | lengthSum.add(atan2(sqrt(x * x + y * y), z));
|
|---|
| 46 | lambda0 = lambda, sinPhi0 = sinPhi, cosPhi0 = cosPhi;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | export default function(object) {
|
|---|
| 50 | lengthSum = new Adder();
|
|---|
| 51 | stream(object, lengthStream);
|
|---|
| 52 | return +lengthSum;
|
|---|
| 53 | }
|
|---|