You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3103 lines
86 KiB

1 year ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // https://d3js.org/d3-geo/ v1.11.1 Copyright 2018 Mike Bostock
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-array')) :
  4. typeof define === 'function' && define.amd ? define(['exports', 'd3-array'], factory) :
  5. (factory((global.d3 = global.d3 || {}),global.d3));
  6. }(this, (function (exports,d3Array) { 'use strict';
  7. // Adds floating point numbers with twice the normal precision.
  8. // Reference: J. R. Shewchuk, Adaptive Precision Floating-Point Arithmetic and
  9. // Fast Robust Geometric Predicates, Discrete & Computational Geometry 18(3)
  10. // 305–363 (1997).
  11. // Code adapted from GeographicLib by Charles F. F. Karney,
  12. // http://geographiclib.sourceforge.net/
  13. function adder() {
  14. return new Adder;
  15. }
  16. function Adder() {
  17. this.reset();
  18. }
  19. Adder.prototype = {
  20. constructor: Adder,
  21. reset: function() {
  22. this.s = // rounded value
  23. this.t = 0; // exact error
  24. },
  25. add: function(y) {
  26. add(temp, y, this.t);
  27. add(this, temp.s, this.s);
  28. if (this.s) this.t += temp.t;
  29. else this.s = temp.t;
  30. },
  31. valueOf: function() {
  32. return this.s;
  33. }
  34. };
  35. var temp = new Adder;
  36. function add(adder, a, b) {
  37. var x = adder.s = a + b,
  38. bv = x - a,
  39. av = x - bv;
  40. adder.t = (a - av) + (b - bv);
  41. }
  42. var epsilon = 1e-6;
  43. var epsilon2 = 1e-12;
  44. var pi = Math.PI;
  45. var halfPi = pi / 2;
  46. var quarterPi = pi / 4;
  47. var tau = pi * 2;
  48. var degrees = 180 / pi;
  49. var radians = pi / 180;
  50. var abs = Math.abs;
  51. var atan = Math.atan;
  52. var atan2 = Math.atan2;
  53. var cos = Math.cos;
  54. var ceil = Math.ceil;
  55. var exp = Math.exp;
  56. var log = Math.log;
  57. var pow = Math.pow;
  58. var sin = Math.sin;
  59. var sign = Math.sign || function(x) { return x > 0 ? 1 : x < 0 ? -1 : 0; };
  60. var sqrt = Math.sqrt;
  61. var tan = Math.tan;
  62. function acos(x) {
  63. return x > 1 ? 0 : x < -1 ? pi : Math.acos(x);
  64. }
  65. function asin(x) {
  66. return x > 1 ? halfPi : x < -1 ? -halfPi : Math.asin(x);
  67. }
  68. function haversin(x) {
  69. return (x = sin(x / 2)) * x;
  70. }
  71. function noop() {}
  72. function streamGeometry(geometry, stream) {
  73. if (geometry && streamGeometryType.hasOwnProperty(geometry.type)) {
  74. streamGeometryType[geometry.type](geometry, stream);
  75. }
  76. }
  77. var streamObjectType = {
  78. Feature: function(object, stream) {
  79. streamGeometry(object.geometry, stream);
  80. },
  81. FeatureCollection: function(object, stream) {
  82. var features = object.features, i = -1, n = features.length;
  83. while (++i < n) streamGeometry(features[i].geometry, stream);
  84. }
  85. };
  86. var streamGeometryType = {
  87. Sphere: function(object, stream) {
  88. stream.sphere();
  89. },
  90. Point: function(object, stream) {
  91. object = object.coordinates;
  92. stream.point(object[0], object[1], object[2]);
  93. },
  94. MultiPoint: function(object, stream) {
  95. var coordinates = object.coordinates, i = -1, n = coordinates.length;
  96. while (++i < n) object = coordinates[i], stream.point(object[0], object[1], object[2]);
  97. },
  98. LineString: function(object, stream) {
  99. streamLine(object.coordinates, stream, 0);
  100. },
  101. MultiLineString: function(object, stream) {
  102. var coordinates = object.coordinates, i = -1, n = coordinates.length;
  103. while (++i < n) streamLine(coordinates[i], stream, 0);
  104. },
  105. Polygon: function(object, stream) {
  106. streamPolygon(object.coordinates, stream);
  107. },
  108. MultiPolygon: function(object, stream) {
  109. var coordinates = object.coordinates, i = -1, n = coordinates.length;
  110. while (++i < n) streamPolygon(coordinates[i], stream);
  111. },
  112. GeometryCollection: function(object, stream) {
  113. var geometries = object.geometries, i = -1, n = geometries.length;
  114. while (++i < n) streamGeometry(geometries[i], stream);
  115. }
  116. };
  117. function streamLine(coordinates, stream, closed) {
  118. var i = -1, n = coordinates.length - closed, coordinate;
  119. stream.lineStart();
  120. while (++i < n) coordinate = coordinates[i], stream.point(coordinate[0], coordinate[1], coordinate[2]);
  121. stream.lineEnd();
  122. }
  123. function streamPolygon(coordinates, stream) {
  124. var i = -1, n = coordinates.length;
  125. stream.polygonStart();
  126. while (++i < n) streamLine(coordinates[i], stream, 1);
  127. stream.polygonEnd();
  128. }
  129. function geoStream(object, stream) {
  130. if (object && streamObjectType.hasOwnProperty(object.type)) {
  131. streamObjectType[object.type](object, stream);
  132. } else {
  133. streamGeometry(object, stream);
  134. }
  135. }
  136. var areaRingSum = adder();
  137. var areaSum = adder(),
  138. lambda00,
  139. phi00,
  140. lambda0,
  141. cosPhi0,
  142. sinPhi0;
  143. var areaStream = {
  144. point: noop,
  145. lineStart: noop,
  146. lineEnd: noop,
  147. polygonStart: function() {
  148. areaRingSum.reset();
  149. areaStream.lineStart = areaRingStart;
  150. areaStream.lineEnd = areaRingEnd;
  151. },
  152. polygonEnd: function() {
  153. var areaRing = +areaRingSum;
  154. areaSum.add(areaRing < 0 ? tau + areaRing : areaRing);
  155. this.lineStart = this.lineEnd = this.point = noop;
  156. },
  157. sphere: function() {
  158. areaSum.add(tau);
  159. }
  160. };
  161. function areaRingStart() {
  162. areaStream.point = areaPointFirst;
  163. }
  164. function areaRingEnd() {
  165. areaPoint(lambda00, phi00);
  166. }
  167. function areaPointFirst(lambda, phi) {
  168. areaStream.point = areaPoint;
  169. lambda00 = lambda, phi00 = phi;
  170. lambda *= radians, phi *= radians;
  171. lambda0 = lambda, cosPhi0 = cos(phi = phi / 2 + quarterPi), sinPhi0 = sin(phi);
  172. }
  173. function areaPoint(lambda, phi) {
  174. lambda *= radians, phi *= radians;
  175. phi = phi / 2 + quarterPi; // half the angular distance from south pole
  176. // Spherical excess E for a spherical triangle with vertices: south pole,
  177. // previous point, current point. Uses a formula derived from Cagnoli’s
  178. // theorem. See Todhunter, Spherical Trig. (1871), Sec. 103, Eq. (2).
  179. var dLambda = lambda - lambda0,
  180. sdLambda = dLambda >= 0 ? 1 : -1,
  181. adLambda = sdLambda * dLambda,
  182. cosPhi = cos(phi),
  183. sinPhi = sin(phi),
  184. k = sinPhi0 * sinPhi,
  185. u = cosPhi0 * cosPhi + k * cos(adLambda),
  186. v = k * sdLambda * sin(adLambda);
  187. areaRingSum.add(atan2(v, u));
  188. // Advance the previous points.
  189. lambda0 = lambda, cosPhi0 = cosPhi, sinPhi0 = sinPhi;
  190. }
  191. function area(object) {
  192. areaSum.reset();
  193. geoStream(object, areaStream);
  194. return areaSum * 2;
  195. }
  196. function spherical(cartesian) {
  197. return [atan2(cartesian[1], cartesian[0]), asin(cartesian[2])];
  198. }
  199. function cartesian(spherical) {
  200. var lambda = spherical[0], phi = spherical[1], cosPhi = cos(phi);
  201. return [cosPhi * cos(lambda), cosPhi * sin(lambda), sin(phi)];
  202. }
  203. function cartesianDot(a, b) {
  204. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  205. }
  206. function cartesianCross(a, b) {
  207. return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
  208. }
  209. // TODO return a
  210. function cartesianAddInPlace(a, b) {
  211. a[0] += b[0], a[1] += b[1], a[2] += b[2];
  212. }
  213. function cartesianScale(vector, k) {
  214. return [vector[0] * k, vector[1] * k, vector[2] * k];
  215. }
  216. // TODO return d
  217. function cartesianNormalizeInPlace(d) {
  218. var l = sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
  219. d[0] /= l, d[1] /= l, d[2] /= l;
  220. }
  221. var lambda0$1, phi0, lambda1, phi1, // bounds
  222. lambda2, // previous lambda-coordinate
  223. lambda00$1, phi00$1, // first point
  224. p0, // previous 3D point
  225. deltaSum = adder(),
  226. ranges,
  227. range;
  228. var boundsStream = {
  229. point: boundsPoint,
  230. lineStart: boundsLineStart,
  231. lineEnd: boundsLineEnd,
  232. polygonStart: function() {
  233. boundsStream.point = boundsRingPoint;
  234. boundsStream.lineStart = boundsRingStart;
  235. boundsStream.lineEnd = boundsRingEnd;
  236. deltaSum.reset();
  237. areaStream.polygonStart();
  238. },
  239. polygonEnd: function() {
  240. areaStream.polygonEnd();
  241. boundsStream.point = boundsPoint;
  242. boundsStream.lineStart = boundsLineStart;
  243. boundsStream.lineEnd = boundsLineEnd;
  244. if (areaRingSum < 0) lambda0$1 = -(lambda1 = 180), phi0 = -(phi1 = 90);
  245. else if (deltaSum > epsilon) phi1 = 90;
  246. else if (deltaSum < -epsilon) phi0 = -90;
  247. range[0] = lambda0$1, range[1] = lambda1;
  248. }
  249. };
  250. function boundsPoint(lambda, phi) {
  251. ranges.push(range = [lambda0$1 = lambda, lambda1 = lambda]);
  252. if (phi < phi0) phi0 = phi;
  253. if (phi > phi1) phi1 = phi;
  254. }
  255. function linePoint(lambda, phi) {
  256. var p = cartesian([lambda * radians, phi * radians]);
  257. if (p0) {
  258. var normal = cartesianCross(p0, p),
  259. equatorial = [normal[1], -normal[0], 0],
  260. inflection = cartesianCross(equatorial, normal);
  261. cartesianNormalizeInPlace(inflection);
  262. inflection = spherical(inflection);
  263. var delta = lambda - lambda2,
  264. sign$$1 = delta > 0 ? 1 : -1,
  265. lambdai = inflection[0] * degrees * sign$$1,
  266. phii,
  267. antimeridian = abs(delta) > 180;
  268. if (antimeridian ^ (sign$$1 * lambda2 < lambdai && lambdai < sign$$1 * lambda)) {
  269. phii = inflection[1] * degrees;
  270. if (phii > phi1) phi1 = phii;
  271. } else if (lambdai = (lambdai + 360) % 360 - 180, antimeridian ^ (sign$$1 * lambda2 < lambdai && lambdai < sign$$1 * lambda)) {
  272. phii = -inflection[1] * degrees;
  273. if (phii < phi0) phi0 = phii;
  274. } else {
  275. if (phi < phi0) phi0 = phi;
  276. if (phi > phi1) phi1 = phi;
  277. }
  278. if (antimeridian) {
  279. if (lambda < lambda2) {
  280. if (angle(lambda0$1, lambda) > angle(lambda0$1, lambda1)) lambda1 = lambda;
  281. } else {
  282. if (angle(lambda, lambda1) > angle(lambda0$1, lambda1)) lambda0$1 = lambda;
  283. }
  284. } else {
  285. if (lambda1 >= lambda0$1) {
  286. if (lambda < lambda0$1) lambda0$1 = lambda;
  287. if (lambda > lambda1) lambda1 = lambda;
  288. } else {
  289. if (lambda > lambda2) {
  290. if (angle(lambda0$1, lambda) > angle(lambda0$1, lambda1)) lambda1 = lambda;
  291. } else {
  292. if (angle(lambda, lambda1) > angle(lambda0$1, lambda1)) lambda0$1 = lambda;
  293. }
  294. }
  295. }
  296. } else {
  297. ranges.push(range = [lambda0$1 = lambda, lambda1 = lambda]);
  298. }
  299. if (phi < phi0) phi0 = phi;
  300. if (phi > phi1) phi1 = phi;
  301. p0 = p, lambda2 = lambda;
  302. }
  303. function boundsLineStart() {
  304. boundsStream.point = linePoint;
  305. }
  306. function boundsLineEnd() {
  307. range[0] = lambda0$1, range[1] = lambda1;
  308. boundsStream.point = boundsPoint;
  309. p0 = null;
  310. }
  311. function boundsRingPoint(lambda, phi) {
  312. if (p0) {
  313. var delta = lambda - lambda2;
  314. deltaSum.add(abs(delta) > 180 ? delta + (delta > 0 ? 360 : -360) : delta);
  315. } else {
  316. lambda00$1 = lambda, phi00$1 = phi;
  317. }
  318. areaStream.point(lambda, phi);
  319. linePoint(lambda, phi);
  320. }
  321. function boundsRingStart() {
  322. areaStream.lineStart();
  323. }
  324. function boundsRingEnd() {
  325. boundsRingPoint(lambda00$1, phi00$1);
  326. areaStream.lineEnd();
  327. if (abs(deltaSum) > epsilon) lambda0$1 = -(lambda1 = 180);
  328. range[0] = lambda0$1, range[1] = lambda1;
  329. p0 = null;
  330. }
  331. // Finds the left-right distance between two longitudes.
  332. // This is almost the same as (lambda1 - lambda0 + 360°) % 360°, except that we want
  333. // the distance between ±180° to be 360°.
  334. function angle(lambda0, lambda1) {
  335. return (lambda1 -= lambda0) < 0 ? lambda1 + 360 : lambda1;
  336. }
  337. function rangeCompare(a, b) {
  338. return a[0] - b[0];
  339. }
  340. function rangeContains(range, x) {
  341. return range[0] <= range[1] ? range[0] <= x && x <= range[1] : x < range[0] || range[1] < x;
  342. }
  343. function bounds(feature) {
  344. var i, n, a, b, merged, deltaMax, delta;
  345. phi1 = lambda1 = -(lambda0$1 = phi0 = Infinity);
  346. ranges = [];
  347. geoStream(feature, boundsStream);
  348. // First, sort ranges by their minimum longitudes.
  349. if (n = ranges.length) {
  350. ranges.sort(rangeCompare);
  351. // Then, merge any ranges that overlap.
  352. for (i = 1, a = ranges[0], merged = [a]; i < n; ++i) {
  353. b = ranges[i];
  354. if (rangeContains(a, b[0]) || rangeContains(a, b[1])) {
  355. if (angle(a[0], b[1]) > angle(a[0], a[1])) a[1] = b[1];
  356. if (angle(b[0], a[1]) > angle(a[0], a[1])) a[0] = b[0];
  357. } else {
  358. merged.push(a = b);
  359. }
  360. }
  361. // Finally, find the largest gap between the merged ranges.
  362. // The final bounding box will be the inverse of this gap.
  363. for (deltaMax = -Infinity, n = merged.length - 1, i = 0, a = merged[n]; i <= n; a = b, ++i) {
  364. b = merged[i];
  365. if ((delta = angle(a[1], b[0])) > deltaMax) deltaMax = delta, lambda0$1 = b[0], lambda1 = a[1];
  366. }
  367. }
  368. ranges = range = null;
  369. return lambda0$1 === Infinity || phi0 === Infinity
  370. ? [[NaN, NaN], [NaN, NaN]]
  371. : [[lambda0$1, phi0], [lambda1, phi1]];
  372. }
  373. var W0, W1,
  374. X0, Y0, Z0,
  375. X1, Y1, Z1,
  376. X2, Y2, Z2,
  377. lambda00$2, phi00$2, // first point
  378. x0, y0, z0; // previous point
  379. var centroidStream = {
  380. sphere: noop,
  381. point: centroidPoint,
  382. lineStart: centroidLineStart,
  383. lineEnd: centroidLineEnd,
  384. polygonStart: function() {
  385. centroidStream.lineStart = centroidRingStart;
  386. centroidStream.lineEnd = centroidRingEnd;
  387. },
  388. polygonEnd: function() {
  389. centroidStream.lineStart = centroidLineStart;
  390. centroidStream.lineEnd = centroidLineEnd;
  391. }
  392. };
  393. // Arithmetic mean of Cartesian vectors.
  394. function centroidPoint(lambda, phi) {
  395. lambda *= radians, phi *= radians;
  396. var cosPhi = cos(phi);
  397. centroidPointCartesian(cosPhi * cos(lambda), cosPhi * sin(lambda), sin(phi));
  398. }
  399. function centroidPointCartesian(x, y, z) {
  400. ++W0;
  401. X0 += (x - X0) / W0;
  402. Y0 += (y - Y0) / W0;
  403. Z0 += (z - Z0) / W0;
  404. }
  405. function centroidLineStart() {
  406. centroidStream.point = centroidLinePointFirst;
  407. }
  408. function centroidLinePointFirst(lambda, phi) {
  409. lambda *= radians, phi *= radians;
  410. var cosPhi = cos(phi);
  411. x0 = cosPhi * cos(lambda);
  412. y0 = cosPhi * sin(lambda);
  413. z0 = sin(phi);
  414. centroidStream.point = centroidLinePoint;
  415. centroidPointCartesian(x0, y0, z0);
  416. }
  417. function centroidLinePoint(lambda, phi) {
  418. lambda *= radians, phi *= radians;
  419. var cosPhi = cos(phi),
  420. x = cosPhi * cos(lambda),
  421. y = cosPhi * sin(lambda),
  422. z = sin(phi),
  423. w = atan2(sqrt((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z);
  424. W1 += w;
  425. X1 += w * (x0 + (x0 = x));
  426. Y1 += w * (y0 + (y0 = y));
  427. Z1 += w * (z0 + (z0 = z));
  428. centroidPointCartesian(x0, y0, z0);
  429. }
  430. function centroidLineEnd() {
  431. centroidStream.point = centroidPoint;
  432. }
  433. // See J. E. Brock, The Inertia Tensor for a Spherical Triangle,
  434. // J. Applied Mechanics 42, 239 (1975).
  435. function centroidRingStart() {
  436. centroidStream.point = centroidRingPointFirst;
  437. }
  438. function centroidRingEnd() {
  439. centroidRingPoint(lambda00$2, phi00$2);
  440. centroidStream.point = centroidPoint;
  441. }
  442. function centroidRingPointFirst(lambda, phi) {
  443. lambda00$2 = lambda, phi00$2 = phi;
  444. lambda *= radians, phi *= radians;
  445. centroidStream.point = centroidRingPoint;
  446. var cosPhi = cos(phi);
  447. x0 = cosPhi * cos(lambda);
  448. y0 = cosPhi * sin(lambda);
  449. z0 = sin(phi);
  450. centroidPointCartesian(x0, y0, z0);
  451. }
  452. function centroidRingPoint(lambda, phi) {
  453. lambda *= radians, phi *= radians;
  454. var cosPhi = cos(phi),
  455. x = cosPhi * cos(lambda),
  456. y = cosPhi * sin(lambda),
  457. z = sin(phi),
  458. cx = y0 * z - z0 * y,
  459. cy = z0 * x - x0 * z,
  460. cz = x0 * y - y0 * x,
  461. m = sqrt(cx * cx + cy * cy + cz * cz),
  462. w = asin(m), // line weight = angle
  463. v = m && -w / m; // area weight multiplier
  464. X2 += v * cx;
  465. Y2 += v * cy;
  466. Z2 += v * cz;
  467. W1 += w;
  468. X1 += w * (x0 + (x0 = x));
  469. Y1 += w * (y0 + (y0 = y));
  470. Z1 += w * (z0 + (z0 = z));
  471. centroidPointCartesian(x0, y0, z0);
  472. }
  473. function centroid(object) {
  474. W0 = W1 =
  475. X0 = Y0 = Z0 =
  476. X1 = Y1 = Z1 =
  477. X2 = Y2 = Z2 = 0;
  478. geoStream(object, centroidStream);
  479. var x = X2,
  480. y = Y2,
  481. z = Z2,
  482. m = x * x + y * y + z * z;
  483. // If the area-weighted ccentroid is undefined, fall back to length-weighted ccentroid.
  484. if (m < epsilon2) {
  485. x = X1, y = Y1, z = Z1;
  486. // If the feature has zero length, fall back to arithmetic mean of point vectors.
  487. if (W1 < epsilon) x = X0, y = Y0, z = Z0;
  488. m = x * x + y * y + z * z;
  489. // If the feature still has an undefined ccentroid, then return.
  490. if (m < epsilon2) return [NaN, NaN];
  491. }
  492. return [atan2(y, x) * degrees, asin(z / sqrt(m)) * degrees];
  493. }
  494. function constant(x) {
  495. return function() {
  496. return x;
  497. };
  498. }
  499. function compose(a, b) {
  500. function compose(x, y) {
  501. return x = a(x, y), b(x[0], x[1]);
  502. }
  503. if (a.invert && b.invert) compose.invert = function(x, y) {
  504. return x = b.invert(x, y), x && a.invert(x[0], x[1]);
  505. };
  506. return compose;
  507. }
  508. function rotationIdentity(lambda, phi) {
  509. return [lambda > pi ? lambda - tau : lambda < -pi ? lambda + tau : lambda, phi];
  510. }
  511. rotationIdentity.invert = rotationIdentity;
  512. function rotateRadians(deltaLambda, deltaPhi, deltaGamma) {
  513. return (deltaLambda %= tau) ? (deltaPhi || deltaGamma ? compose(rotationLambda(deltaLambda), rotationPhiGamma(deltaPhi, deltaGamma))
  514. : rotationLambda(deltaLambda))
  515. : (deltaPhi || deltaGamma ? rotationPhiGamma(deltaPhi, deltaGamma)
  516. : rotationIdentity);
  517. }
  518. function forwardRotationLambda(deltaLambda) {
  519. return function(lambda, phi) {
  520. return lambda += deltaLambda, [lambda > pi ? lambda - tau : lambda < -pi ? lambda + tau : lambda, phi];
  521. };
  522. }
  523. function rotationLambda(deltaLambda) {
  524. var rotation = forwardRotationLambda(deltaLambda);
  525. rotation.invert = forwardRotationLambda(-deltaLambda);
  526. return rotation;
  527. }
  528. function rotationPhiGamma(deltaPhi, deltaGamma) {
  529. var cosDeltaPhi = cos(deltaPhi),
  530. sinDeltaPhi = sin(deltaPhi),
  531. cosDeltaGamma = cos(deltaGamma),
  532. sinDeltaGamma = sin(deltaGamma);
  533. function rotation(lambda, phi) {
  534. var cosPhi = cos(phi),
  535. x = cos(lambda) * cosPhi,
  536. y = sin(lambda) * cosPhi,
  537. z = sin(phi),
  538. k = z * cosDeltaPhi + x * sinDeltaPhi;
  539. return [
  540. atan2(y * cosDeltaGamma - k * sinDeltaGamma, x * cosDeltaPhi - z * sinDeltaPhi),
  541. asin(k * cosDeltaGamma + y * sinDeltaGamma)
  542. ];
  543. }
  544. rotation.invert = function(lambda, phi) {
  545. var cosPhi = cos(phi),
  546. x = cos(lambda) * cosPhi,
  547. y = sin(lambda) * cosPhi,
  548. z = sin(phi),
  549. k = z * cosDeltaGamma - y * sinDeltaGamma;
  550. return [
  551. atan2(y * cosDeltaGamma + z * sinDeltaGamma, x * cosDeltaPhi + k * sinDeltaPhi),
  552. asin(k * cosDeltaPhi - x * sinDeltaPhi)
  553. ];
  554. };
  555. return rotation;
  556. }
  557. function rotation(rotate) {
  558. rotate = rotateRadians(rotate[0] * radians, rotate[1] * radians, rotate.length > 2 ? rotate[2] * radians : 0);
  559. function forward(coordinates) {
  560. coordinates = rotate(coordinates[0] * radians, coordinates[1] * radians);
  561. return coordinates[0] *= degrees, coordinates[1] *= degrees, coordinates;
  562. }
  563. forward.invert = function(coordinates) {
  564. coordinates = rotate.invert(coordinates[0] * radians, coordinates[1] * radians);
  565. return coordinates[0] *= degrees, coordinates[1] *= degrees, coordinates;
  566. };
  567. return forward;
  568. }
  569. // Generates a circle centered at [0°, 0°], with a given radius and precision.
  570. function circleStream(stream, radius, delta, direction, t0, t1) {
  571. if (!delta) return;
  572. var cosRadius = cos(radius),
  573. sinRadius = sin(radius),
  574. step = direction * delta;
  575. if (t0 == null) {
  576. t0 = radius + direction * tau;
  577. t1 = radius - step / 2;
  578. } else {
  579. t0 = circleRadius(cosRadius, t0);
  580. t1 = circleRadius(cosRadius, t1);
  581. if (direction > 0 ? t0 < t1 : t0 > t1) t0 += direction * tau;
  582. }
  583. for (var point, t = t0; direction > 0 ? t > t1 : t < t1; t -= step) {
  584. point = spherical([cosRadius, -sinRadius * cos(t), -sinRadius * sin(t)]);
  585. stream.point(point[0], point[1]);
  586. }
  587. }
  588. // Returns the signed angle of a cartesian point relative to [cosRadius, 0, 0].
  589. function circleRadius(cosRadius, point) {
  590. point = cartesian(point), point[0] -= cosRadius;
  591. cartesianNormalizeInPlace(point);
  592. var radius = acos(-point[1]);
  593. return ((-point[2] < 0 ? -radius : radius) + tau - epsilon) % tau;
  594. }
  595. function circle() {
  596. var center = constant([0, 0]),
  597. radius = constant(90),
  598. precision = constant(6),
  599. ring,
  600. rotate,
  601. stream = {point: point};
  602. function point(x, y) {
  603. ring.push(x = rotate(x, y));
  604. x[0] *= degrees, x[1] *= degrees;
  605. }
  606. function circle() {
  607. var c = center.apply(this, arguments),
  608. r = radius.apply(this, arguments) * radians,
  609. p = precision.apply(this, arguments) * radians;
  610. ring = [];
  611. rotate = rotateRadians(-c[0] * radians, -c[1] * radians, 0).invert;
  612. circleStream(stream, r, p, 1);
  613. c = {type: "Polygon", coordinates: [ring]};
  614. ring = rotate = null;
  615. return c;
  616. }
  617. circle.center = function(_) {
  618. return arguments.length ? (center = typeof _ === "function" ? _ : constant([+_[0], +_[1]]), circle) : center;
  619. };
  620. circle.radius = function(_) {
  621. return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), circle) : radius;
  622. };
  623. circle.precision = function(_) {
  624. return arguments.length ? (precision = typeof _ === "function" ? _ : constant(+_), circle) : precision;
  625. };
  626. return circle;
  627. }
  628. function clipBuffer() {
  629. var lines = [],
  630. line;
  631. return {
  632. point: function(x, y) {
  633. line.push([x, y]);
  634. },
  635. lineStart: function() {
  636. lines.push(line = []);
  637. },
  638. lineEnd: noop,
  639. rejoin: function() {
  640. if (lines.length > 1) lines.push(lines.pop().concat(lines.shift()));
  641. },
  642. result: function() {
  643. var result = lines;
  644. lines = [];
  645. line = null;
  646. return result;
  647. }
  648. };
  649. }
  650. function pointEqual(a, b) {
  651. return abs(a[0] - b[0]) < epsilon && abs(a[1] - b[1]) < epsilon;
  652. }
  653. function Intersection(point, points, other, entry) {
  654. this.x = point;
  655. this.z = points;
  656. this.o = other; // another intersection
  657. this.e = entry; // is an entry?
  658. this.v = false; // visited
  659. this.n = this.p = null; // next & previous
  660. }
  661. // A generalized polygon clipping algorithm: given a polygon that has been cut
  662. // into its visible line segments, and rejoins the segments by interpolating
  663. // along the clip edge.
  664. function clipRejoin(segments, compareIntersection, startInside, interpolate, stream) {
  665. var subject = [],
  666. clip = [],
  667. i,
  668. n;
  669. segments.forEach(function(segment) {
  670. if ((n = segment.length - 1) <= 0) return;
  671. var n, p0 = segment[0], p1 = segment[n], x;
  672. // If the first and last points of a segment are coincident, then treat as a
  673. // closed ring. TODO if all rings are closed, then the winding order of the
  674. // exterior ring should be checked.
  675. if (pointEqual(p0, p1)) {
  676. stream.lineStart();
  677. for (i = 0; i < n; ++i) stream.point((p0 = segment[i])[0], p0[1]);
  678. stream.lineEnd();
  679. return;
  680. }
  681. subject.push(x = new Intersection(p0, segment, null, true));
  682. clip.push(x.o = new Intersection(p0, null, x, false));
  683. subject.push(x = new Intersection(p1, segment, null, false));
  684. clip.push(x.o = new Intersection(p1, null, x, true));
  685. });
  686. if (!subject.length) return;
  687. clip.sort(compareIntersection);
  688. link(subject);
  689. link(clip);
  690. for (i = 0, n = clip.length; i < n; ++i) {
  691. clip[i].e = startInside = !startInside;
  692. }
  693. var start = subject[0],
  694. points,
  695. point;
  696. while (1) {
  697. // Find first unvisited intersection.
  698. var current = start,
  699. isSubject = true;
  700. while (current.v) if ((current = current.n) === start) return;
  701. points = current.z;
  702. stream.lineStart();
  703. do {
  704. current.v = current.o.v = true;
  705. if (current.e) {
  706. if (isSubject) {
  707. for (i = 0, n = points.length; i < n; ++i) stream.point((point = points[i])[0], point[1]);
  708. } else {
  709. interpolate(current.x, current.n.x, 1, stream);
  710. }
  711. current = current.n;
  712. } else {
  713. if (isSubject) {
  714. points = current.p.z;
  715. for (i = points.length - 1; i >= 0; --i) stream.point((point = points[i])[0], point[1]);
  716. } else {
  717. interpolate(current.x, current.p.x, -1, stream);
  718. }
  719. current = current.p;
  720. }
  721. current = current.o;
  722. points = current.z;
  723. isSubject = !isSubject;
  724. } while (!current.v);
  725. stream.lineEnd();
  726. }
  727. }
  728. function link(array) {
  729. if (!(n = array.length)) return;
  730. var n,
  731. i = 0,
  732. a = array[0],
  733. b;
  734. while (++i < n) {
  735. a.n = b = array[i];
  736. b.p = a;
  737. a = b;
  738. }
  739. a.n = b = array[0];
  740. b.p = a;
  741. }
  742. var sum = adder();
  743. function polygonContains(polygon, point) {
  744. var lambda = point[0],
  745. phi = point[1],
  746. sinPhi = sin(phi),
  747. normal = [sin(lambda), -cos(lambda), 0],
  748. angle = 0,
  749. winding = 0;
  750. sum.reset();
  751. if (sinPhi === 1) phi = halfPi + epsilon;
  752. else if (sinPhi === -1) phi = -halfPi - epsilon;
  753. for (var i = 0, n = polygon.length; i < n; ++i) {
  754. if (!(m = (ring = polygon[i]).length)) continue;
  755. var ring,
  756. m,
  757. point0 = ring[m - 1],
  758. lambda0 = point0[0],
  759. phi0 = point0[1] / 2 + quarterPi,
  760. sinPhi0 = sin(phi0),
  761. cosPhi0 = cos(phi0);
  762. for (var j = 0; j < m; ++j, lambda0 = lambda1, sinPhi0 = sinPhi1, cosPhi0 = cosPhi1, point0 = point1) {
  763. var point1 = ring[j],
  764. lambda1 = point1[0],
  765. phi1 = point1[1] / 2 + quarterPi,
  766. sinPhi1 = sin(phi1),
  767. cosPhi1 = cos(phi1),
  768. delta = lambda1 - lambda0,
  769. sign$$1 = delta >= 0 ? 1 : -1,
  770. absDelta = sign$$1 * delta,
  771. antimeridian = absDelta > pi,
  772. k = sinPhi0 * sinPhi1;
  773. sum.add(atan2(k * sign$$1 * sin(absDelta), cosPhi0 * cosPhi1 + k * cos(absDelta)));
  774. angle += antimeridian ? delta + sign$$1 * tau : delta;
  775. // Are the longitudes either side of the point’s meridian (lambda),
  776. // and are the latitudes smaller than the parallel (phi)?
  777. if (antimeridian ^ lambda0 >= lambda ^ lambda1 >= lambda) {
  778. var arc = cartesianCross(cartesian(point0), cartesian(point1));
  779. cartesianNormalizeInPlace(arc);
  780. var intersection = cartesianCross(normal, arc);
  781. cartesianNormalizeInPlace(intersection);
  782. var phiArc = (antimeridian ^ delta >= 0 ? -1 : 1) * asin(intersection[2]);
  783. if (phi > phiArc || phi === phiArc && (arc[0] || arc[1])) {
  784. winding += antimeridian ^ delta >= 0 ? 1 : -1;
  785. }
  786. }
  787. }
  788. }
  789. // First, determine whether the South pole is inside or outside:
  790. //
  791. // It is inside if:
  792. // * the polygon winds around it in a clockwise direction.
  793. // * the polygon does not (cumulatively) wind around it, but has a negative
  794. // (counter-clockwise) area.
  795. //
  796. // Second, count the (signed) number of times a segment crosses a lambda
  797. // from the point to the South pole. If it is zero, then the point is the
  798. // same side as the South pole.
  799. return (angle < -epsilon || angle < epsilon && sum < -epsilon) ^ (winding & 1);
  800. }
  801. function clip(pointVisible, clipLine, interpolate, start) {
  802. return function(sink) {
  803. var line = clipLine(sink),
  804. ringBuffer = clipBuffer(),
  805. ringSink = clipLine(ringBuffer),
  806. polygonStarted = false,
  807. polygon,
  808. segments,
  809. ring;
  810. var clip = {
  811. point: point,
  812. lineStart: lineStart,
  813. lineEnd: lineEnd,
  814. polygonStart: function() {
  815. clip.point = pointRing;
  816. clip.lineStart = ringStart;
  817. clip.lineEnd = ringEnd;
  818. segments = [];
  819. polygon = [];
  820. },
  821. polygonEnd: function() {
  822. clip.point = point;
  823. clip.lineStart = lineStart;
  824. clip.lineEnd = lineEnd;
  825. segments = d3Array.merge(segments);
  826. var startInside = polygonContains(polygon, start);
  827. if (segments.length) {
  828. if (!polygonStarted) sink.polygonStart(), polygonStarted = true;
  829. clipRejoin(segments, compareIntersection, startInside, interpolate, sink);
  830. } else if (startInside) {
  831. if (!polygonStarted) sink.polygonStart(), polygonStarted = true;
  832. sink.lineStart();
  833. interpolate(null, null, 1, sink);
  834. sink.lineEnd();
  835. }
  836. if (polygonStarted) sink.polygonEnd(), polygonStarted = false;
  837. segments = polygon = null;
  838. },
  839. sphere: function() {
  840. sink.polygonStart();
  841. sink.lineStart();
  842. interpolate(null, null, 1, sink);
  843. sink.lineEnd();
  844. sink.polygonEnd();
  845. }
  846. };
  847. function point(lambda, phi) {
  848. if (pointVisible(lambda, phi)) sink.point(lambda, phi);
  849. }
  850. function pointLine(lambda, phi) {
  851. line.point(lambda, phi);
  852. }
  853. function lineStart() {
  854. clip.point = pointLine;
  855. line.lineStart();
  856. }
  857. function lineEnd() {
  858. clip.point = point;
  859. line.lineEnd();
  860. }
  861. function pointRing(lambda, phi) {
  862. ring.push([lambda, phi]);
  863. ringSink.point(lambda, phi);
  864. }
  865. function ringStart() {
  866. ringSink.lineStart();
  867. ring = [];
  868. }
  869. function ringEnd() {
  870. pointRing(ring[0][0], ring[0][1]);
  871. ringSink.lineEnd();
  872. var clean = ringSink.clean(),
  873. ringSegments = ringBuffer.result(),
  874. i, n = ringSegments.length, m,
  875. segment,
  876. point;
  877. ring.pop();
  878. polygon.push(ring);
  879. ring = null;
  880. if (!n) return;
  881. // No intersections.
  882. if (clean & 1) {
  883. segment = ringSegments[0];
  884. if ((m = segment.length - 1) > 0) {
  885. if (!polygonStarted) sink.polygonStart(), polygonStarted = true;
  886. sink.lineStart();
  887. for (i = 0; i < m; ++i) sink.point((point = segment[i])[0], point[1]);
  888. sink.lineEnd();
  889. }
  890. return;
  891. }
  892. // Rejoin connected segments.
  893. // TODO reuse ringBuffer.rejoin()?
  894. if (n > 1 && clean & 2) ringSegments.push(ringSegments.pop().concat(ringSegments.shift()));
  895. segments.push(ringSegments.filter(validSegment));
  896. }
  897. return clip;
  898. };
  899. }
  900. function validSegment(segment) {
  901. return segment.length > 1;
  902. }
  903. // Intersections are sorted along the clip edge. For both antimeridian cutting
  904. // and circle clipping, the same comparison is used.
  905. function compareIntersection(a, b) {
  906. return ((a = a.x)[0] < 0 ? a[1] - halfPi - epsilon : halfPi - a[1])
  907. - ((b = b.x)[0] < 0 ? b[1] - halfPi - epsilon : halfPi - b[1]);
  908. }
  909. var clipAntimeridian = clip(
  910. function() { return true; },
  911. clipAntimeridianLine,
  912. clipAntimeridianInterpolate,
  913. [-pi, -halfPi]
  914. );
  915. // Takes a line and cuts into visible segments. Return values: 0 - there were
  916. // intersections or the line was empty; 1 - no intersections; 2 - there were
  917. // intersections, and the first and last segments should be rejoined.
  918. function clipAntimeridianLine(stream) {
  919. var lambda0 = NaN,
  920. phi0 = NaN,
  921. sign0 = NaN,
  922. clean; // no intersections
  923. return {
  924. lineStart: function() {
  925. stream.lineStart();
  926. clean = 1;
  927. },
  928. point: function(lambda1, phi1) {
  929. var sign1 = lambda1 > 0 ? pi : -pi,
  930. delta = abs(lambda1 - lambda0);
  931. if (abs(delta - pi) < epsilon) { // line crosses a pole
  932. stream.point(lambda0, phi0 = (phi0 + phi1) / 2 > 0 ? halfPi : -halfPi);
  933. stream.point(sign0, phi0);
  934. stream.lineEnd();
  935. stream.lineStart();
  936. stream.point(sign1, phi0);
  937. stream.point(lambda1, phi0);
  938. clean = 0;
  939. } else if (sign0 !== sign1 && delta >= pi) { // line crosses antimeridian
  940. if (abs(lambda0 - sign0) < epsilon) lambda0 -= sign0 * epsilon; // handle degeneracies
  941. if (abs(lambda1 - sign1) < epsilon) lambda1 -= sign1 * epsilon;
  942. phi0 = clipAntimeridianIntersect(lambda0, phi0, lambda1, phi1);
  943. stream.point(sign0, phi0);
  944. stream.lineEnd();
  945. stream.lineStart();
  946. stream.point(sign1, phi0);
  947. clean = 0;
  948. }
  949. stream.point(lambda0 = lambda1, phi0 = phi1);
  950. sign0 = sign1;
  951. },
  952. lineEnd: function() {
  953. stream.lineEnd();
  954. lambda0 = phi0 = NaN;
  955. },
  956. clean: function() {
  957. return 2 - clean; // if intersections, rejoin first and last segments
  958. }
  959. };
  960. }
  961. function clipAntimeridianIntersect(lambda0, phi0, lambda1, phi1) {
  962. var cosPhi0,
  963. cosPhi1,
  964. sinLambda0Lambda1 = sin(lambda0 - lambda1);
  965. return abs(sinLambda0Lambda1) > epsilon
  966. ? atan((sin(phi0) * (cosPhi1 = cos(phi1)) * sin(lambda1)
  967. - sin(phi1) * (cosPhi0 = cos(phi0)) * sin(lambda0))
  968. / (cosPhi0 * cosPhi1 * sinLambda0Lambda1))
  969. : (phi0 + phi1) / 2;
  970. }
  971. function clipAntimeridianInterpolate(from, to, direction, stream) {
  972. var phi;
  973. if (from == null) {
  974. phi = direction * halfPi;
  975. stream.point(-pi, phi);
  976. stream.point(0, phi);
  977. stream.point(pi, phi);
  978. stream.point(pi, 0);
  979. stream.point(pi, -phi);
  980. stream.point(0, -phi);
  981. stream.point(-pi, -phi);
  982. stream.point(-pi, 0);
  983. stream.point(-pi, phi);
  984. } else if (abs(from[0] - to[0]) > epsilon) {
  985. var lambda = from[0] < to[0] ? pi : -pi;
  986. phi = direction * lambda / 2;
  987. stream.point(-lambda, phi);
  988. stream.point(0, phi);
  989. stream.point(lambda, phi);
  990. } else {
  991. stream.point(to[0], to[1]);
  992. }
  993. }
  994. function clipCircle(radius) {
  995. var cr = cos(radius),
  996. delta = 6 * radians,
  997. smallRadius = cr > 0,
  998. notHemisphere = abs(cr) > epsilon; // TODO optimise for this common case
  999. function interpolate(from, to, direction, stream) {
  1000. circleStream(stream, radius, delta, direction, from, to);
  1001. }
  1002. function visible(lambda, phi) {
  1003. return cos(lambda) * cos(phi) > cr;
  1004. }
  1005. // Takes a line and cuts into visible segments. Return values used for polygon
  1006. // clipping: 0 - there were intersections or the line was empty; 1 - no
  1007. // intersections 2 - there were intersections, and the first and last segments
  1008. // should be rejoined.
  1009. function clipLine(stream) {
  1010. var point0, // previous point
  1011. c0, // code for previous point
  1012. v0, // visibility of previous point
  1013. v00, // visibility of first point
  1014. clean; // no intersections
  1015. return {
  1016. lineStart: function() {
  1017. v00 = v0 = false;
  1018. clean = 1;
  1019. },
  1020. point: function(lambda, phi) {
  1021. var point1 = [lambda, phi],
  1022. point2,
  1023. v = visible(lambda, phi),
  1024. c = smallRadius
  1025. ? v ? 0 : code(lambda, phi)
  1026. : v ? code(lambda + (lambda < 0 ? pi : -pi), phi) : 0;
  1027. if (!point0 && (v00 = v0 = v)) stream.lineStart();
  1028. // Handle degeneracies.
  1029. // TODO ignore if not clipping polygons.
  1030. if (v !== v0) {
  1031. point2 = intersect(point0, point1);
  1032. if (!point2 || pointEqual(point0, point2) || pointEqual(point1, point2)) {
  1033. point1[0] += epsilon;
  1034. point1[1] += epsilon;
  1035. v = visible(point1[0], point1[1]);
  1036. }
  1037. }
  1038. if (v !== v0) {
  1039. clean = 0;
  1040. if (v) {
  1041. // outside going in
  1042. stream.lineStart();
  1043. point2 = intersect(point1, point0);
  1044. stream.point(point2[0], point2[1]);
  1045. } else {
  1046. // inside going out
  1047. point2 = intersect(point0, point1);
  1048. stream.point(point2[0], point2[1]);
  1049. stream.lineEnd();
  1050. }
  1051. point0 = point2;
  1052. } else if (notHemisphere && point0 && smallRadius ^ v) {
  1053. var t;
  1054. // If the codes for two points are different, or are both zero,
  1055. // and there this segment intersects with the small circle.
  1056. if (!(c & c0) && (t = intersect(point1, point0, true))) {
  1057. clean = 0;
  1058. if (smallRadius) {
  1059. stream.lineStart();
  1060. stream.point(t[0][0], t[0][1]);
  1061. stream.point(t[1][0], t[1][1]);
  1062. stream.lineEnd();
  1063. } else {
  1064. stream.point(t[1][0], t[1][1]);
  1065. stream.lineEnd();
  1066. stream.lineStart();
  1067. stream.point(t[0][0], t[0][1]);
  1068. }
  1069. }
  1070. }
  1071. if (v && (!point0 || !pointEqual(point0, point1))) {
  1072. stream.point(point1[0], point1[1]);
  1073. }
  1074. point0 = point1, v0 = v, c0 = c;
  1075. },
  1076. lineEnd: function() {
  1077. if (v0) stream.lineEnd();
  1078. point0 = null;
  1079. },
  1080. // Rejoin first and last segments if there were intersections and the first
  1081. // and last points were visible.
  1082. clean: function() {
  1083. return clean | ((v00 && v0) << 1);
  1084. }
  1085. };
  1086. }
  1087. // Intersects the great circle between a and b with the clip circle.
  1088. function intersect(a, b, two) {
  1089. var pa = cartesian(a),
  1090. pb = cartesian(b);
  1091. // We have two planes, n1.p = d1 and n2.p = d2.
  1092. // Find intersection line p(t) = c1 n1 + c2 n2 + t (n1 ⨯ n2).
  1093. var n1 = [1, 0, 0], // normal
  1094. n2 = cartesianCross(pa, pb),
  1095. n2n2 = cartesianDot(n2, n2),
  1096. n1n2 = n2[0], // cartesianDot(n1, n2),
  1097. determinant = n2n2 - n1n2 * n1n2;
  1098. // Two polar points.
  1099. if (!determinant) return !two && a;
  1100. var c1 = cr * n2n2 / determinant,
  1101. c2 = -cr * n1n2 / determinant,
  1102. n1xn2 = cartesianCross(n1, n2),
  1103. A = cartesianScale(n1, c1),
  1104. B = cartesianScale(n2, c2);
  1105. cartesianAddInPlace(A, B);
  1106. // Solve |p(t)|^2 = 1.
  1107. var u = n1xn2,
  1108. w = cartesianDot(A, u),
  1109. uu = cartesianDot(u, u),
  1110. t2 = w * w - uu * (cartesianDot(A, A) - 1);
  1111. if (t2 < 0) return;
  1112. var t = sqrt(t2),
  1113. q = cartesianScale(u, (-w - t) / uu);
  1114. cartesianAddInPlace(q, A);
  1115. q = spherical(q);
  1116. if (!two) return q;
  1117. // Two intersection points.
  1118. var lambda0 = a[0],
  1119. lambda1 = b[0],
  1120. phi0 = a[1],
  1121. phi1 = b[1],
  1122. z;
  1123. if (lambda1 < lambda0) z = lambda0, lambda0 = lambda1, lambda1 = z;
  1124. var delta = lambda1 - lambda0,
  1125. polar = abs(delta - pi) < epsilon,
  1126. meridian = polar || delta < epsilon;
  1127. if (!polar && phi1 < phi0) z = phi0, phi0 = phi1, phi1 = z;
  1128. // Check that the first point is between a and b.
  1129. if (meridian
  1130. ? polar
  1131. ? phi0 + phi1 > 0 ^ q[1] < (abs(q[0] - lambda0) < epsilon ? phi0 : phi1)
  1132. : phi0 <= q[1] && q[1] <= phi1
  1133. : delta > pi ^ (lambda0 <= q[0] && q[0] <= lambda1)) {
  1134. var q1 = cartesianScale(u, (-w + t) / uu);
  1135. cartesianAddInPlace(q1, A);
  1136. return [q, spherical(q1)];
  1137. }
  1138. }
  1139. // Generates a 4-bit vector representing the location of a point relative to
  1140. // the small circle's bounding box.
  1141. function code(lambda, phi) {
  1142. var r = smallRadius ? radius : pi - radius,
  1143. code = 0;
  1144. if (lambda < -r) code |= 1; // left
  1145. else if (lambda > r) code |= 2; // right
  1146. if (phi < -r) code |= 4; // below
  1147. else if (phi > r) code |= 8; // above
  1148. return code;
  1149. }
  1150. return clip(visible, clipLine, interpolate, smallRadius ? [0, -radius] : [-pi, radius - pi]);
  1151. }
  1152. function clipLine(a, b, x0, y0, x1, y1) {
  1153. var ax = a[0],
  1154. ay = a[1],
  1155. bx = b[0],
  1156. by = b[1],
  1157. t0 = 0,
  1158. t1 = 1,
  1159. dx = bx - ax,
  1160. dy = by - ay,
  1161. r;
  1162. r = x0 - ax;
  1163. if (!dx && r > 0) return;
  1164. r /= dx;
  1165. if (dx < 0) {
  1166. if (r < t0) return;
  1167. if (r < t1) t1 = r;
  1168. } else if (dx > 0) {
  1169. if (r > t1) return;
  1170. if (r > t0) t0 = r;
  1171. }
  1172. r = x1 - ax;
  1173. if (!dx && r < 0) return;
  1174. r /= dx;
  1175. if (dx < 0) {
  1176. if (r > t1) return;
  1177. if (r > t0) t0 = r;
  1178. } else if (dx > 0) {
  1179. if (r < t0) return;
  1180. if (r < t1) t1 = r;
  1181. }
  1182. r = y0 - ay;
  1183. if (!dy && r > 0) return;
  1184. r /= dy;
  1185. if (dy < 0) {
  1186. if (r < t0) return;
  1187. if (r < t1) t1 = r;
  1188. } else if (dy > 0) {
  1189. if (r > t1) return;
  1190. if (r > t0) t0 = r;
  1191. }
  1192. r = y1 - ay;
  1193. if (!dy && r < 0) return;
  1194. r /= dy;
  1195. if (dy < 0) {
  1196. if (r > t1) return;
  1197. if (r > t0) t0 = r;
  1198. } else if (dy > 0) {
  1199. if (r < t0) return;
  1200. if (r < t1) t1 = r;
  1201. }
  1202. if (t0 > 0) a[0] = ax + t0 * dx, a[1] = ay + t0 * dy;
  1203. if (t1 < 1) b[0] = ax + t1 * dx, b[1] = ay + t1 * dy;
  1204. return true;
  1205. }
  1206. var clipMax = 1e9, clipMin = -clipMax;
  1207. // TODO Use d3-polygon’s polygonContains here for the ring check?
  1208. // TODO Eliminate duplicate buffering in clipBuffer and polygon.push?
  1209. function clipRectangle(x0, y0, x1, y1) {
  1210. function visible(x, y) {
  1211. return x0 <= x && x <= x1 && y0 <= y && y <= y1;
  1212. }
  1213. function interpolate(from, to, direction, stream) {
  1214. var a = 0, a1 = 0;
  1215. if (from == null
  1216. || (a = corner(from, direction)) !== (a1 = corner(to, direction))
  1217. || comparePoint(from, to) < 0 ^ direction > 0) {
  1218. do stream.point(a === 0 || a === 3 ? x0 : x1, a > 1 ? y1 : y0);
  1219. while ((a = (a + direction + 4) % 4) !== a1);
  1220. } else {
  1221. stream.point(to[0], to[1]);
  1222. }
  1223. }
  1224. function corner(p, direction) {
  1225. return abs(p[0] - x0) < epsilon ? direction > 0 ? 0 : 3
  1226. : abs(p[0] - x1) < epsilon ? direction > 0 ? 2 : 1
  1227. : abs(p[1] - y0) < epsilon ? direction > 0 ? 1 : 0
  1228. : direction > 0 ? 3 : 2; // abs(p[1] - y1) < epsilon
  1229. }
  1230. function compareIntersection(a, b) {
  1231. return comparePoint(a.x, b.x);
  1232. }
  1233. function comparePoint(a, b) {
  1234. var ca = corner(a, 1),
  1235. cb = corner(b, 1);
  1236. return ca !== cb ? ca - cb
  1237. : ca === 0 ? b[1] - a[1]
  1238. : ca === 1 ? a[0] - b[0]
  1239. : ca === 2 ? a[1] - b[1]
  1240. : b[0] - a[0];
  1241. }
  1242. return function(stream) {
  1243. var activeStream = stream,
  1244. bufferStream = clipBuffer(),
  1245. segments,
  1246. polygon,
  1247. ring,
  1248. x__, y__, v__, // first point
  1249. x_, y_, v_, // previous point
  1250. first,
  1251. clean;
  1252. var clipStream = {
  1253. point: point,
  1254. lineStart: lineStart,
  1255. lineEnd: lineEnd,
  1256. polygonStart: polygonStart,
  1257. polygonEnd: polygonEnd
  1258. };
  1259. function point(x, y) {
  1260. if (visible(x, y)) activeStream.point(x, y);
  1261. }
  1262. function polygonInside() {
  1263. var winding = 0;
  1264. for (var i = 0, n = polygon.length; i < n; ++i) {
  1265. for (var ring = polygon[i], j = 1, m = ring.length, point = ring[0], a0, a1, b0 = point[0], b1 = point[1]; j < m; ++j) {
  1266. a0 = b0, a1 = b1, point = ring[j], b0 = point[0], b1 = point[1];
  1267. if (a1 <= y1) { if (b1 > y1 && (b0 - a0) * (y1 - a1) > (b1 - a1) * (x0 - a0)) ++winding; }
  1268. else { if (b1 <= y1 && (b0 - a0) * (y1 - a1) < (b1 - a1) * (x0 - a0)) --winding; }
  1269. }
  1270. }
  1271. return winding;
  1272. }
  1273. // Buffer geometry within a polygon and then clip it en masse.
  1274. function polygonStart() {
  1275. activeStream = bufferStream, segments = [], polygon = [], clean = true;
  1276. }
  1277. function polygonEnd() {
  1278. var startInside = polygonInside(),
  1279. cleanInside = clean && startInside,
  1280. visible = (segments = d3Array.merge(segments)).length;
  1281. if (cleanInside || visible) {
  1282. stream.polygonStart();
  1283. if (cleanInside) {
  1284. stream.lineStart();
  1285. interpolate(null, null, 1, stream);
  1286. stream.lineEnd();
  1287. }
  1288. if (visible) {
  1289. clipRejoin(segments, compareIntersection, startInside, interpolate, stream);
  1290. }
  1291. stream.polygonEnd();
  1292. }
  1293. activeStream = stream, segments = polygon = ring = null;
  1294. }
  1295. function lineStart() {
  1296. clipStream.point = linePoint;
  1297. if (polygon) polygon.push(ring = []);
  1298. first = true;
  1299. v_ = false;
  1300. x_ = y_ = NaN;
  1301. }
  1302. // TODO rather than special-case polygons, simply handle them separately.
  1303. // Ideally, coincident intersection points should be jittered to avoid
  1304. // clipping issues.
  1305. function lineEnd() {
  1306. if (segments) {
  1307. linePoint(x__, y__);
  1308. if (v__ && v_) bufferStream.rejoin();
  1309. segments.push(bufferStream.result());
  1310. }
  1311. clipStream.point = point;
  1312. if (v_) activeStream.lineEnd();
  1313. }
  1314. function linePoint(x, y) {
  1315. var v = visible(x, y);
  1316. if (polygon) ring.push([x, y]);
  1317. if (first) {
  1318. x__ = x, y__ = y, v__ = v;
  1319. first = false;
  1320. if (v) {
  1321. activeStream.lineStart();
  1322. activeStream.point(x, y);
  1323. }
  1324. } else {
  1325. if (v && v_) activeStream.point(x, y);
  1326. else {
  1327. var a = [x_ = Math.max(clipMin, Math.min(clipMax, x_)), y_ = Math.max(clipMin, Math.min(clipMax, y_))],
  1328. b = [x = Math.max(clipMin, Math.min(clipMax, x)), y = Math.max(clipMin, Math.min(clipMax, y))];
  1329. if (clipLine(a, b, x0, y0, x1, y1)) {
  1330. if (!v_) {
  1331. activeStream.lineStart();
  1332. activeStream.point(a[0], a[1]);
  1333. }
  1334. activeStream.point(b[0], b[1]);
  1335. if (!v) activeStream.lineEnd();
  1336. clean = false;
  1337. } else if (v) {
  1338. activeStream.lineStart();
  1339. activeStream.point(x, y);
  1340. clean = false;
  1341. }
  1342. }
  1343. }
  1344. x_ = x, y_ = y, v_ = v;
  1345. }
  1346. return clipStream;
  1347. };
  1348. }
  1349. function extent() {
  1350. var x0 = 0,
  1351. y0 = 0,
  1352. x1 = 960,
  1353. y1 = 500,
  1354. cache,
  1355. cacheStream,
  1356. clip;
  1357. return clip = {
  1358. stream: function(stream) {
  1359. return cache && cacheStream === stream ? cache : cache = clipRectangle(x0, y0, x1, y1)(cacheStream = stream);
  1360. },
  1361. extent: function(_) {
  1362. return arguments.length ? (x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1], cache = cacheStream = null, clip) : [[x0, y0], [x1, y1]];
  1363. }
  1364. };
  1365. }
  1366. var lengthSum = adder(),
  1367. lambda0$2,
  1368. sinPhi0$1,
  1369. cosPhi0$1;
  1370. var lengthStream = {
  1371. sphere: noop,
  1372. point: noop,
  1373. lineStart: lengthLineStart,
  1374. lineEnd: noop,
  1375. polygonStart: noop,
  1376. polygonEnd: noop
  1377. };
  1378. function lengthLineStart() {
  1379. lengthStream.point = lengthPointFirst;
  1380. lengthStream.lineEnd = lengthLineEnd;
  1381. }
  1382. function lengthLineEnd() {
  1383. lengthStream.point = lengthStream.lineEnd = noop;
  1384. }
  1385. function lengthPointFirst(lambda, phi) {
  1386. lambda *= radians, phi *= radians;
  1387. lambda0$2 = lambda, sinPhi0$1 = sin(phi), cosPhi0$1 = cos(phi);
  1388. lengthStream.point = lengthPoint;
  1389. }
  1390. function lengthPoint(lambda, phi) {
  1391. lambda *= radians, phi *= radians;
  1392. var sinPhi = sin(phi),
  1393. cosPhi = cos(phi),
  1394. delta = abs(lambda - lambda0$2),
  1395. cosDelta = cos(delta),
  1396. sinDelta = sin(delta),
  1397. x = cosPhi * sinDelta,
  1398. y = cosPhi0$1 * sinPhi - sinPhi0$1 * cosPhi * cosDelta,
  1399. z = sinPhi0$1 * sinPhi + cosPhi0$1 * cosPhi * cosDelta;
  1400. lengthSum.add(atan2(sqrt(x * x + y * y), z));
  1401. lambda0$2 = lambda, sinPhi0$1 = sinPhi, cosPhi0$1 = cosPhi;
  1402. }
  1403. function length(object) {
  1404. lengthSum.reset();
  1405. geoStream(object, lengthStream);
  1406. return +lengthSum;
  1407. }
  1408. var coordinates = [null, null],
  1409. object = {type: "LineString", coordinates: coordinates};
  1410. function distance(a, b) {
  1411. coordinates[0] = a;
  1412. coordinates[1] = b;
  1413. return length(object);
  1414. }
  1415. var containsObjectType = {
  1416. Feature: function(object, point) {
  1417. return containsGeometry(object.geometry, point);
  1418. },
  1419. FeatureCollection: function(object, point) {
  1420. var features = object.features, i = -1, n = features.length;
  1421. while (++i < n) if (containsGeometry(features[i].geometry, point)) return true;
  1422. return false;
  1423. }
  1424. };
  1425. var containsGeometryType = {
  1426. Sphere: function() {
  1427. return true;
  1428. },
  1429. Point: function(object, point) {
  1430. return containsPoint(object.coordinates, point);
  1431. },
  1432. MultiPoint: function(object, point) {
  1433. var coordinates = object.coordinates, i = -1, n = coordinates.length;
  1434. while (++i < n) if (containsPoint(coordinates[i], point)) return true;
  1435. return false;
  1436. },
  1437. LineString: function(object, point) {
  1438. return containsLine(object.coordinates, point);
  1439. },
  1440. MultiLineString: function(object, point) {
  1441. var coordinates = object.coordinates, i = -1, n = coordinates.length;
  1442. while (++i < n) if (containsLine(coordinates[i], point)) return true;
  1443. return false;
  1444. },
  1445. Polygon: function(object, point) {
  1446. return containsPolygon(object.coordinates, point);
  1447. },
  1448. MultiPolygon: function(object, point) {
  1449. var coordinates = object.coordinates, i = -1, n = coordinates.length;
  1450. while (++i < n) if (containsPolygon(coordinates[i], point)) return true;
  1451. return false;
  1452. },
  1453. GeometryCollection: function(object, point) {
  1454. var geometries = object.geometries, i = -1, n = geometries.length;
  1455. while (++i < n) if (containsGeometry(geometries[i], point)) return true;
  1456. return false;
  1457. }
  1458. };
  1459. function containsGeometry(geometry, point) {
  1460. return geometry && containsGeometryType.hasOwnProperty(geometry.type)
  1461. ? containsGeometryType[geometry.type](geometry, point)
  1462. : false;
  1463. }
  1464. function containsPoint(coordinates, point) {
  1465. return distance(coordinates, point) === 0;
  1466. }
  1467. function containsLine(coordinates, point) {
  1468. var ab = distance(coordinates[0], coordinates[1]),
  1469. ao = distance(coordinates[0], point),
  1470. ob = distance(point, coordinates[1]);
  1471. return ao + ob <= ab + epsilon;
  1472. }
  1473. function containsPolygon(coordinates, point) {
  1474. return !!polygonContains(coordinates.map(ringRadians), pointRadians(point));
  1475. }
  1476. function ringRadians(ring) {
  1477. return ring = ring.map(pointRadians), ring.pop(), ring;
  1478. }
  1479. function pointRadians(point) {
  1480. return [point[0] * radians, point[1] * radians];
  1481. }
  1482. function contains(object, point) {
  1483. return (object && containsObjectType.hasOwnProperty(object.type)
  1484. ? containsObjectType[object.type]
  1485. : containsGeometry)(object, point);
  1486. }
  1487. function graticuleX(y0, y1, dy) {
  1488. var y = d3Array.range(y0, y1 - epsilon, dy).concat(y1);
  1489. return function(x) { return y.map(function(y) { return [x, y]; }); };
  1490. }
  1491. function graticuleY(x0, x1, dx) {
  1492. var x = d3Array.range(x0, x1 - epsilon, dx).concat(x1);
  1493. return function(y) { return x.map(function(x) { return [x, y]; }); };
  1494. }
  1495. function graticule() {
  1496. var x1, x0, X1, X0,
  1497. y1, y0, Y1, Y0,
  1498. dx = 10, dy = dx, DX = 90, DY = 360,
  1499. x, y, X, Y,
  1500. precision = 2.5;
  1501. function graticule() {
  1502. return {type: "MultiLineString", coordinates: lines()};
  1503. }
  1504. function lines() {
  1505. return d3Array.range(ceil(X0 / DX) * DX, X1, DX).map(X)
  1506. .concat(d3Array.range(ceil(Y0 / DY) * DY, Y1, DY).map(Y))
  1507. .concat(d3Array.range(ceil(x0 / dx) * dx, x1, dx).filter(function(x) { return abs(x % DX) > epsilon; }).map(x))
  1508. .concat(d3Array.range(ceil(y0 / dy) * dy, y1, dy).filter(function(y) { return abs(y % DY) > epsilon; }).map(y));
  1509. }
  1510. graticule.lines = function() {
  1511. return lines().map(function(coordinates) { return {type: "LineString", coordinates: coordinates}; });
  1512. };
  1513. graticule.outline = function() {
  1514. return {
  1515. type: "Polygon",
  1516. coordinates: [
  1517. X(X0).concat(
  1518. Y(Y1).slice(1),
  1519. X(X1).reverse().slice(1),
  1520. Y(Y0).reverse().slice(1))
  1521. ]
  1522. };
  1523. };
  1524. graticule.extent = function(_) {
  1525. if (!arguments.length) return graticule.extentMinor();
  1526. return graticule.extentMajor(_).extentMinor(_);
  1527. };
  1528. graticule.extentMajor = function(_) {
  1529. if (!arguments.length) return [[X0, Y0], [X1, Y1]];
  1530. X0 = +_[0][0], X1 = +_[1][0];
  1531. Y0 = +_[0][1], Y1 = +_[1][1];
  1532. if (X0 > X1) _ = X0, X0 = X1, X1 = _;
  1533. if (Y0 > Y1) _ = Y0, Y0 = Y1, Y1 = _;
  1534. return graticule.precision(precision);
  1535. };
  1536. graticule.extentMinor = function(_) {
  1537. if (!arguments.length) return [[x0, y0], [x1, y1]];
  1538. x0 = +_[0][0], x1 = +_[1][0];
  1539. y0 = +_[0][1], y1 = +_[1][1];
  1540. if (x0 > x1) _ = x0, x0 = x1, x1 = _;
  1541. if (y0 > y1) _ = y0, y0 = y1, y1 = _;
  1542. return graticule.precision(precision);
  1543. };
  1544. graticule.step = function(_) {
  1545. if (!arguments.length) return graticule.stepMinor();
  1546. return graticule.stepMajor(_).stepMinor(_);
  1547. };
  1548. graticule.stepMajor = function(_) {
  1549. if (!arguments.length) return [DX, DY];
  1550. DX = +_[0], DY = +_[1];
  1551. return graticule;
  1552. };
  1553. graticule.stepMinor = function(_) {
  1554. if (!arguments.length) return [dx, dy];
  1555. dx = +_[0], dy = +_[1];
  1556. return graticule;
  1557. };
  1558. graticule.precision = function(_) {
  1559. if (!arguments.length) return precision;
  1560. precision = +_;
  1561. x = graticuleX(y0, y1, 90);
  1562. y = graticuleY(x0, x1, precision);
  1563. X = graticuleX(Y0, Y1, 90);
  1564. Y = graticuleY(X0, X1, precision);
  1565. return graticule;
  1566. };
  1567. return graticule
  1568. .extentMajor([[-180, -90 + epsilon], [180, 90 - epsilon]])
  1569. .extentMinor([[-180, -80 - epsilon], [180, 80 + epsilon]]);
  1570. }
  1571. function graticule10() {
  1572. return graticule()();
  1573. }
  1574. function interpolate(a, b) {
  1575. var x0 = a[0] * radians,
  1576. y0 = a[1] * radians,
  1577. x1 = b[0] * radians,
  1578. y1 = b[1] * radians,
  1579. cy0 = cos(y0),
  1580. sy0 = sin(y0),
  1581. cy1 = cos(y1),
  1582. sy1 = sin(y1),
  1583. kx0 = cy0 * cos(x0),
  1584. ky0 = cy0 * sin(x0),
  1585. kx1 = cy1 * cos(x1),
  1586. ky1 = cy1 * sin(x1),
  1587. d = 2 * asin(sqrt(haversin(y1 - y0) + cy0 * cy1 * haversin(x1 - x0))),
  1588. k = sin(d);
  1589. var interpolate = d ? function(t) {
  1590. var B = sin(t *= d) / k,
  1591. A = sin(d - t) / k,
  1592. x = A * kx0 + B * kx1,
  1593. y = A * ky0 + B * ky1,
  1594. z = A * sy0 + B * sy1;
  1595. return [
  1596. atan2(y, x) * degrees,
  1597. atan2(z, sqrt(x * x + y * y)) * degrees
  1598. ];
  1599. } : function() {
  1600. return [x0 * degrees, y0 * degrees];
  1601. };
  1602. interpolate.distance = d;
  1603. return interpolate;
  1604. }
  1605. function identity(x) {
  1606. return x;
  1607. }
  1608. var areaSum$1 = adder(),
  1609. areaRingSum$1 = adder(),
  1610. x00,
  1611. y00,
  1612. x0$1,
  1613. y0$1;
  1614. var areaStream$1 = {
  1615. point: noop,
  1616. lineStart: noop,
  1617. lineEnd: noop,
  1618. polygonStart: function() {
  1619. areaStream$1.lineStart = areaRingStart$1;
  1620. areaStream$1.lineEnd = areaRingEnd$1;
  1621. },
  1622. polygonEnd: function() {
  1623. areaStream$1.lineStart = areaStream$1.lineEnd = areaStream$1.point = noop;
  1624. areaSum$1.add(abs(areaRingSum$1));
  1625. areaRingSum$1.reset();
  1626. },
  1627. result: function() {
  1628. var area = areaSum$1 / 2;
  1629. areaSum$1.reset();
  1630. return area;
  1631. }
  1632. };
  1633. function areaRingStart$1() {
  1634. areaStream$1.point = areaPointFirst$1;
  1635. }
  1636. function areaPointFirst$1(x, y) {
  1637. areaStream$1.point = areaPoint$1;
  1638. x00 = x0$1 = x, y00 = y0$1 = y;
  1639. }
  1640. function areaPoint$1(x, y) {
  1641. areaRingSum$1.add(y0$1 * x - x0$1 * y);
  1642. x0$1 = x, y0$1 = y;
  1643. }
  1644. function areaRingEnd$1() {
  1645. areaPoint$1(x00, y00);
  1646. }
  1647. var x0$2 = Infinity,
  1648. y0$2 = x0$2,
  1649. x1 = -x0$2,
  1650. y1 = x1;
  1651. var boundsStream$1 = {
  1652. point: boundsPoint$1,
  1653. lineStart: noop,
  1654. lineEnd: noop,
  1655. polygonStart: noop,
  1656. polygonEnd: noop,
  1657. result: function() {
  1658. var bounds = [[x0$2, y0$2], [x1, y1]];
  1659. x1 = y1 = -(y0$2 = x0$2 = Infinity);
  1660. return bounds;
  1661. }
  1662. };
  1663. function boundsPoint$1(x, y) {
  1664. if (x < x0$2) x0$2 = x;
  1665. if (x > x1) x1 = x;
  1666. if (y < y0$2) y0$2 = y;
  1667. if (y > y1) y1 = y;
  1668. }
  1669. // TODO Enforce positive area for exterior, negative area for interior?
  1670. var X0$1 = 0,
  1671. Y0$1 = 0,
  1672. Z0$1 = 0,
  1673. X1$1 = 0,
  1674. Y1$1 = 0,
  1675. Z1$1 = 0,
  1676. X2$1 = 0,
  1677. Y2$1 = 0,
  1678. Z2$1 = 0,
  1679. x00$1,
  1680. y00$1,
  1681. x0$3,
  1682. y0$3;
  1683. var centroidStream$1 = {
  1684. point: centroidPoint$1,
  1685. lineStart: centroidLineStart$1,
  1686. lineEnd: centroidLineEnd$1,
  1687. polygonStart: function() {
  1688. centroidStream$1.lineStart = centroidRingStart$1;
  1689. centroidStream$1.lineEnd = centroidRingEnd$1;
  1690. },
  1691. polygonEnd: function() {
  1692. centroidStream$1.point = centroidPoint$1;
  1693. centroidStream$1.lineStart = centroidLineStart$1;
  1694. centroidStream$1.lineEnd = centroidLineEnd$1;
  1695. },
  1696. result: function() {
  1697. var centroid = Z2$1 ? [X2$1 / Z2$1, Y2$1 / Z2$1]
  1698. : Z1$1 ? [X1$1 / Z1$1, Y1$1 / Z1$1]
  1699. : Z0$1 ? [X0$1 / Z0$1, Y0$1 / Z0$1]
  1700. : [NaN, NaN];
  1701. X0$1 = Y0$1 = Z0$1 =
  1702. X1$1 = Y1$1 = Z1$1 =
  1703. X2$1 = Y2$1 = Z2$1 = 0;
  1704. return centroid;
  1705. }
  1706. };
  1707. function centroidPoint$1(x, y) {
  1708. X0$1 += x;
  1709. Y0$1 += y;
  1710. ++Z0$1;
  1711. }
  1712. function centroidLineStart$1() {
  1713. centroidStream$1.point = centroidPointFirstLine;
  1714. }
  1715. function centroidPointFirstLine(x, y) {
  1716. centroidStream$1.point = centroidPointLine;
  1717. centroidPoint$1(x0$3 = x, y0$3 = y);
  1718. }
  1719. function centroidPointLine(x, y) {
  1720. var dx = x - x0$3, dy = y - y0$3, z = sqrt(dx * dx + dy * dy);
  1721. X1$1 += z * (x0$3 + x) / 2;
  1722. Y1$1 += z * (y0$3 + y) / 2;
  1723. Z1$1 += z;
  1724. centroidPoint$1(x0$3 = x, y0$3 = y);
  1725. }
  1726. function centroidLineEnd$1() {
  1727. centroidStream$1.point = centroidPoint$1;
  1728. }
  1729. function centroidRingStart$1() {
  1730. centroidStream$1.point = centroidPointFirstRing;
  1731. }
  1732. function centroidRingEnd$1() {
  1733. centroidPointRing(x00$1, y00$1);
  1734. }
  1735. function centroidPointFirstRing(x, y) {
  1736. centroidStream$1.point = centroidPointRing;
  1737. centroidPoint$1(x00$1 = x0$3 = x, y00$1 = y0$3 = y);
  1738. }
  1739. function centroidPointRing(x, y) {
  1740. var dx = x - x0$3,
  1741. dy = y - y0$3,
  1742. z = sqrt(dx * dx + dy * dy);
  1743. X1$1 += z * (x0$3 + x) / 2;
  1744. Y1$1 += z * (y0$3 + y) / 2;
  1745. Z1$1 += z;
  1746. z = y0$3 * x - x0$3 * y;
  1747. X2$1 += z * (x0$3 + x);
  1748. Y2$1 += z * (y0$3 + y);
  1749. Z2$1 += z * 3;
  1750. centroidPoint$1(x0$3 = x, y0$3 = y);
  1751. }
  1752. function PathContext(context) {
  1753. this._context = context;
  1754. }
  1755. PathContext.prototype = {
  1756. _radius: 4.5,
  1757. pointRadius: function(_) {
  1758. return this._radius = _, this;
  1759. },
  1760. polygonStart: function() {
  1761. this._line = 0;
  1762. },
  1763. polygonEnd: function() {
  1764. this._line = NaN;
  1765. },
  1766. lineStart: function() {
  1767. this._point = 0;
  1768. },
  1769. lineEnd: function() {
  1770. if (this._line === 0) this._context.closePath();
  1771. this._point = NaN;
  1772. },
  1773. point: function(x, y) {
  1774. switch (this._point) {
  1775. case 0: {
  1776. this._context.moveTo(x, y);
  1777. this._point = 1;
  1778. break;
  1779. }
  1780. case 1: {
  1781. this._context.lineTo(x, y);
  1782. break;
  1783. }
  1784. default: {
  1785. this._context.moveTo(x + this._radius, y);
  1786. this._context.arc(x, y, this._radius, 0, tau);
  1787. break;
  1788. }
  1789. }
  1790. },
  1791. result: noop
  1792. };
  1793. var lengthSum$1 = adder(),
  1794. lengthRing,
  1795. x00$2,
  1796. y00$2,
  1797. x0$4,
  1798. y0$4;
  1799. var lengthStream$1 = {
  1800. point: noop,
  1801. lineStart: function() {
  1802. lengthStream$1.point = lengthPointFirst$1;
  1803. },
  1804. lineEnd: function() {
  1805. if (lengthRing) lengthPoint$1(x00$2, y00$2);
  1806. lengthStream$1.point = noop;
  1807. },
  1808. polygonStart: function() {
  1809. lengthRing = true;
  1810. },
  1811. polygonEnd: function() {
  1812. lengthRing = null;
  1813. },
  1814. result: function() {
  1815. var length = +lengthSum$1;
  1816. lengthSum$1.reset();
  1817. return length;
  1818. }
  1819. };
  1820. function lengthPointFirst$1(x, y) {
  1821. lengthStream$1.point = lengthPoint$1;
  1822. x00$2 = x0$4 = x, y00$2 = y0$4 = y;
  1823. }
  1824. function lengthPoint$1(x, y) {
  1825. x0$4 -= x, y0$4 -= y;
  1826. lengthSum$1.add(sqrt(x0$4 * x0$4 + y0$4 * y0$4));
  1827. x0$4 = x, y0$4 = y;
  1828. }
  1829. function PathString() {
  1830. this._string = [];
  1831. }
  1832. PathString.prototype = {
  1833. _radius: 4.5,
  1834. _circle: circle$1(4.5),
  1835. pointRadius: function(_) {
  1836. if ((_ = +_) !== this._radius) this._radius = _, this._circle = null;
  1837. return this;
  1838. },
  1839. polygonStart: function() {
  1840. this._line = 0;
  1841. },
  1842. polygonEnd: function() {
  1843. this._line = NaN;
  1844. },
  1845. lineStart: function() {
  1846. this._point = 0;
  1847. },
  1848. lineEnd: function() {
  1849. if (this._line === 0) this._string.push("Z");
  1850. this._point = NaN;
  1851. },
  1852. point: function(x, y) {
  1853. switch (this._point) {
  1854. case 0: {
  1855. this._string.push("M", x, ",", y);
  1856. this._point = 1;
  1857. break;
  1858. }
  1859. case 1: {
  1860. this._string.push("L", x, ",", y);
  1861. break;
  1862. }
  1863. default: {
  1864. if (this._circle == null) this._circle = circle$1(this._radius);
  1865. this._string.push("M", x, ",", y, this._circle);
  1866. break;
  1867. }
  1868. }
  1869. },
  1870. result: function() {
  1871. if (this._string.length) {
  1872. var result = this._string.join("");
  1873. this._string = [];
  1874. return result;
  1875. } else {
  1876. return null;
  1877. }
  1878. }
  1879. };
  1880. function circle$1(radius) {
  1881. return "m0," + radius
  1882. + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius
  1883. + "a" + radius + "," + radius + " 0 1,1 0," + 2 * radius
  1884. + "z";
  1885. }
  1886. function index(projection, context) {
  1887. var pointRadius = 4.5,
  1888. projectionStream,
  1889. contextStream;
  1890. function path(object) {
  1891. if (object) {
  1892. if (typeof pointRadius === "function") contextStream.pointRadius(+pointRadius.apply(this, arguments));
  1893. geoStream(object, projectionStream(contextStream));
  1894. }
  1895. return contextStream.result();
  1896. }
  1897. path.area = function(object) {
  1898. geoStream(object, projectionStream(areaStream$1));
  1899. return areaStream$1.result();
  1900. };
  1901. path.measure = function(object) {
  1902. geoStream(object, projectionStream(lengthStream$1));
  1903. return lengthStream$1.result();
  1904. };
  1905. path.bounds = function(object) {
  1906. geoStream(object, projectionStream(boundsStream$1));
  1907. return boundsStream$1.result();
  1908. };
  1909. path.centroid = function(object) {
  1910. geoStream(object, projectionStream(centroidStream$1));
  1911. return centroidStream$1.result();
  1912. };
  1913. path.projection = function(_) {
  1914. return arguments.length ? (projectionStream = _ == null ? (projection = null, identity) : (projection = _).stream, path) : projection;
  1915. };
  1916. path.context = function(_) {
  1917. if (!arguments.length) return context;
  1918. contextStream = _ == null ? (context = null, new PathString) : new PathContext(context = _);
  1919. if (typeof pointRadius !== "function") contextStream.pointRadius(pointRadius);
  1920. return path;
  1921. };
  1922. path.pointRadius = function(_) {
  1923. if (!arguments.length) return pointRadius;
  1924. pointRadius = typeof _ === "function" ? _ : (contextStream.pointRadius(+_), +_);
  1925. return path;
  1926. };
  1927. return path.projection(projection).context(context);
  1928. }
  1929. function transform(methods) {
  1930. return {
  1931. stream: transformer(methods)
  1932. };
  1933. }
  1934. function transformer(methods) {
  1935. return function(stream) {
  1936. var s = new TransformStream;
  1937. for (var key in methods) s[key] = methods[key];
  1938. s.stream = stream;
  1939. return s;
  1940. };
  1941. }
  1942. function TransformStream() {}
  1943. TransformStream.prototype = {
  1944. constructor: TransformStream,
  1945. point: function(x, y) { this.stream.point(x, y); },
  1946. sphere: function() { this.stream.sphere(); },
  1947. lineStart: function() { this.stream.lineStart(); },
  1948. lineEnd: function() { this.stream.lineEnd(); },
  1949. polygonStart: function() { this.stream.polygonStart(); },
  1950. polygonEnd: function() { this.stream.polygonEnd(); }
  1951. };
  1952. function fit(projection, fitBounds, object) {
  1953. var clip = projection.clipExtent && projection.clipExtent();
  1954. projection.scale(150).translate([0, 0]);
  1955. if (clip != null) projection.clipExtent(null);
  1956. geoStream(object, projection.stream(boundsStream$1));
  1957. fitBounds(boundsStream$1.result());
  1958. if (clip != null) projection.clipExtent(clip);
  1959. return projection;
  1960. }
  1961. function fitExtent(projection, extent, object) {
  1962. return fit(projection, function(b) {
  1963. var w = extent[1][0] - extent[0][0],
  1964. h = extent[1][1] - extent[0][1],
  1965. k = Math.min(w / (b[1][0] - b[0][0]), h / (b[1][1] - b[0][1])),
  1966. x = +extent[0][0] + (w - k * (b[1][0] + b[0][0])) / 2,
  1967. y = +extent[0][1] + (h - k * (b[1][1] + b[0][1])) / 2;
  1968. projection.scale(150 * k).translate([x, y]);
  1969. }, object);
  1970. }
  1971. function fitSize(projection, size, object) {
  1972. return fitExtent(projection, [[0, 0], size], object);
  1973. }
  1974. function fitWidth(projection, width, object) {
  1975. return fit(projection, function(b) {
  1976. var w = +width,
  1977. k = w / (b[1][0] - b[0][0]),
  1978. x = (w - k * (b[1][0] + b[0][0])) / 2,
  1979. y = -k * b[0][1];
  1980. projection.scale(150 * k).translate([x, y]);
  1981. }, object);
  1982. }
  1983. function fitHeight(projection, height, object) {
  1984. return fit(projection, function(b) {
  1985. var h = +height,
  1986. k = h / (b[1][1] - b[0][1]),
  1987. x = -k * b[0][0],
  1988. y = (h - k * (b[1][1] + b[0][1])) / 2;
  1989. projection.scale(150 * k).translate([x, y]);
  1990. }, object);
  1991. }
  1992. var maxDepth = 16, // maximum depth of subdivision
  1993. cosMinDistance = cos(30 * radians); // cos(minimum angular distance)
  1994. function resample(project, delta2) {
  1995. return +delta2 ? resample$1(project, delta2) : resampleNone(project);
  1996. }
  1997. function resampleNone(project) {
  1998. return transformer({
  1999. point: function(x, y) {
  2000. x = project(x, y);
  2001. this.stream.point(x[0], x[1]);
  2002. }
  2003. });
  2004. }
  2005. function resample$1(project, delta2) {
  2006. function resampleLineTo(x0, y0, lambda0, a0, b0, c0, x1, y1, lambda1, a1, b1, c1, depth, stream) {
  2007. var dx = x1 - x0,
  2008. dy = y1 - y0,
  2009. d2 = dx * dx + dy * dy;
  2010. if (d2 > 4 * delta2 && depth--) {
  2011. var a = a0 + a1,
  2012. b = b0 + b1,
  2013. c = c0 + c1,
  2014. m = sqrt(a * a + b * b + c * c),
  2015. phi2 = asin(c /= m),
  2016. lambda2 = abs(abs(c) - 1) < epsilon || abs(lambda0 - lambda1) < epsilon ? (lambda0 + lambda1) / 2 : atan2(b, a),
  2017. p = project(lambda2, phi2),
  2018. x2 = p[0],
  2019. y2 = p[1],
  2020. dx2 = x2 - x0,
  2021. dy2 = y2 - y0,
  2022. dz = dy * dx2 - dx * dy2;
  2023. if (dz * dz / d2 > delta2 // perpendicular projected distance
  2024. || abs((dx * dx2 + dy * dy2) / d2 - 0.5) > 0.3 // midpoint close to an end
  2025. || a0 * a1 + b0 * b1 + c0 * c1 < cosMinDistance) { // angular distance
  2026. resampleLineTo(x0, y0, lambda0, a0, b0, c0, x2, y2, lambda2, a /= m, b /= m, c, depth, stream);
  2027. stream.point(x2, y2);
  2028. resampleLineTo(x2, y2, lambda2, a, b, c, x1, y1, lambda1, a1, b1, c1, depth, stream);
  2029. }
  2030. }
  2031. }
  2032. return function(stream) {
  2033. var lambda00, x00, y00, a00, b00, c00, // first point
  2034. lambda0, x0, y0, a0, b0, c0; // previous point
  2035. var resampleStream = {
  2036. point: point,
  2037. lineStart: lineStart,
  2038. lineEnd: lineEnd,
  2039. polygonStart: function() { stream.polygonStart(); resampleStream.lineStart = ringStart; },
  2040. polygonEnd: function() { stream.polygonEnd(); resampleStream.lineStart = lineStart; }
  2041. };
  2042. function point(x, y) {
  2043. x = project(x, y);
  2044. stream.point(x[0], x[1]);
  2045. }
  2046. function lineStart() {
  2047. x0 = NaN;
  2048. resampleStream.point = linePoint;
  2049. stream.lineStart();
  2050. }
  2051. function linePoint(lambda, phi) {
  2052. var c = cartesian([lambda, phi]), p = project(lambda, phi);
  2053. resampleLineTo(x0, y0, lambda0, a0, b0, c0, x0 = p[0], y0 = p[1], lambda0 = lambda, a0 = c[0], b0 = c[1], c0 = c[2], maxDepth, stream);
  2054. stream.point(x0, y0);
  2055. }
  2056. function lineEnd() {
  2057. resampleStream.point = point;
  2058. stream.lineEnd();
  2059. }
  2060. function ringStart() {
  2061. lineStart();
  2062. resampleStream.point = ringPoint;
  2063. resampleStream.lineEnd = ringEnd;
  2064. }
  2065. function ringPoint(lambda, phi) {
  2066. linePoint(lambda00 = lambda, phi), x00 = x0, y00 = y0, a00 = a0, b00 = b0, c00 = c0;
  2067. resampleStream.point = linePoint;
  2068. }
  2069. function ringEnd() {
  2070. resampleLineTo(x0, y0, lambda0, a0, b0, c0, x00, y00, lambda00, a00, b00, c00, maxDepth, stream);
  2071. resampleStream.lineEnd = lineEnd;
  2072. lineEnd();
  2073. }
  2074. return resampleStream;
  2075. };
  2076. }
  2077. var transformRadians = transformer({
  2078. point: function(x, y) {
  2079. this.stream.point(x * radians, y * radians);
  2080. }
  2081. });
  2082. function transformRotate(rotate) {
  2083. return transformer({
  2084. point: function(x, y) {
  2085. var r = rotate(x, y);
  2086. return this.stream.point(r[0], r[1]);
  2087. }
  2088. });
  2089. }
  2090. function scaleTranslate(k, dx, dy) {
  2091. function transform$$1(x, y) {
  2092. return [dx + k * x, dy - k * y];
  2093. }
  2094. transform$$1.invert = function(x, y) {
  2095. return [(x - dx) / k, (dy - y) / k];
  2096. };
  2097. return transform$$1;
  2098. }
  2099. function scaleTranslateRotate(k, dx, dy, alpha) {
  2100. var cosAlpha = cos(alpha),
  2101. sinAlpha = sin(alpha),
  2102. a = cosAlpha * k,
  2103. b = sinAlpha * k,
  2104. ai = cosAlpha / k,
  2105. bi = sinAlpha / k,
  2106. ci = (sinAlpha * dy - cosAlpha * dx) / k,
  2107. fi = (sinAlpha * dx + cosAlpha * dy) / k;
  2108. function transform$$1(x, y) {
  2109. return [a * x - b * y + dx, dy - b * x - a * y];
  2110. }
  2111. transform$$1.invert = function(x, y) {
  2112. return [ai * x - bi * y + ci, fi - bi * x - ai * y];
  2113. };
  2114. return transform$$1;
  2115. }
  2116. function projection(project) {
  2117. return projectionMutator(function() { return project; })();
  2118. }
  2119. function projectionMutator(projectAt) {
  2120. var project,
  2121. k = 150, // scale
  2122. x = 480, y = 250, // translate
  2123. lambda = 0, phi = 0, // center
  2124. deltaLambda = 0, deltaPhi = 0, deltaGamma = 0, rotate, // pre-rotate
  2125. alpha = 0, // post-rotate
  2126. theta = null, preclip = clipAntimeridian, // pre-clip angle
  2127. x0 = null, y0, x1, y1, postclip = identity, // post-clip extent
  2128. delta2 = 0.5, // precision
  2129. projectResample,
  2130. projectTransform,
  2131. projectRotateTransform,
  2132. cache,
  2133. cacheStream;
  2134. function projection(point) {
  2135. return projectRotateTransform(point[0] * radians, point[1] * radians);
  2136. }
  2137. function invert(point) {
  2138. point = projectRotateTransform.invert(point[0], point[1]);
  2139. return point && [point[0] * degrees, point[1] * degrees];
  2140. }
  2141. projection.stream = function(stream) {
  2142. return cache && cacheStream === stream ? cache : cache = transformRadians(transformRotate(rotate)(preclip(projectResample(postclip(cacheStream = stream)))));
  2143. };
  2144. projection.preclip = function(_) {
  2145. return arguments.length ? (preclip = _, theta = undefined, reset()) : preclip;
  2146. };
  2147. projection.postclip = function(_) {
  2148. return arguments.length ? (postclip = _, x0 = y0 = x1 = y1 = null, reset()) : postclip;
  2149. };
  2150. projection.clipAngle = function(_) {
  2151. return arguments.length ? (preclip = +_ ? clipCircle(theta = _ * radians) : (theta = null, clipAntimeridian), reset()) : theta * degrees;
  2152. };
  2153. projection.clipExtent = function(_) {
  2154. return arguments.length ? (postclip = _ == null ? (x0 = y0 = x1 = y1 = null, identity) : clipRectangle(x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1]), reset()) : x0 == null ? null : [[x0, y0], [x1, y1]];
  2155. };
  2156. projection.scale = function(_) {
  2157. return arguments.length ? (k = +_, recenter()) : k;
  2158. };
  2159. projection.translate = function(_) {
  2160. return arguments.length ? (x = +_[0], y = +_[1], recenter()) : [x, y];
  2161. };
  2162. projection.center = function(_) {
  2163. return arguments.length ? (lambda = _[0] % 360 * radians, phi = _[1] % 360 * radians, recenter()) : [lambda * degrees, phi * degrees];
  2164. };
  2165. projection.rotate = function(_) {
  2166. return arguments.length ? (deltaLambda = _[0] % 360 * radians, deltaPhi = _[1] % 360 * radians, deltaGamma = _.length > 2 ? _[2] % 360 * radians : 0, recenter()) : [deltaLambda * degrees, deltaPhi * degrees, deltaGamma * degrees];
  2167. };
  2168. projection.angle = function(_) {
  2169. return arguments.length ? (alpha = _ % 360 * radians, recenter()) : alpha * degrees;
  2170. };
  2171. projection.precision = function(_) {
  2172. return arguments.length ? (projectResample = resample(projectTransform, delta2 = _ * _), reset()) : sqrt(delta2);
  2173. };
  2174. projection.fitExtent = function(extent, object) {
  2175. return fitExtent(projection, extent, object);
  2176. };
  2177. projection.fitSize = function(size, object) {
  2178. return fitSize(projection, size, object);
  2179. };
  2180. projection.fitWidth = function(width, object) {
  2181. return fitWidth(projection, width, object);
  2182. };
  2183. projection.fitHeight = function(height, object) {
  2184. return fitHeight(projection, height, object);
  2185. };
  2186. function recenter() {
  2187. var center = scaleTranslateRotate(k, 0, 0, alpha).apply(null, project(lambda, phi)),
  2188. transform$$1 = (alpha ? scaleTranslateRotate : scaleTranslate)(k, x - center[0], y - center[1], alpha);
  2189. rotate = rotateRadians(deltaLambda, deltaPhi, deltaGamma);
  2190. projectTransform = compose(project, transform$$1);
  2191. projectRotateTransform = compose(rotate, projectTransform);
  2192. projectResample = resample(projectTransform, delta2);
  2193. return reset();
  2194. }
  2195. function reset() {
  2196. cache = cacheStream = null;
  2197. return projection;
  2198. }
  2199. return function() {
  2200. project = projectAt.apply(this, arguments);
  2201. projection.invert = project.invert && invert;
  2202. return recenter();
  2203. };
  2204. }
  2205. function conicProjection(projectAt) {
  2206. var phi0 = 0,
  2207. phi1 = pi / 3,
  2208. m = projectionMutator(projectAt),
  2209. p = m(phi0, phi1);
  2210. p.parallels = function(_) {
  2211. return arguments.length ? m(phi0 = _[0] * radians, phi1 = _[1] * radians) : [phi0 * degrees, phi1 * degrees];
  2212. };
  2213. return p;
  2214. }
  2215. function cylindricalEqualAreaRaw(phi0) {
  2216. var cosPhi0 = cos(phi0);
  2217. function forward(lambda, phi) {
  2218. return [lambda * cosPhi0, sin(phi) / cosPhi0];
  2219. }
  2220. forward.invert = function(x, y) {
  2221. return [x / cosPhi0, asin(y * cosPhi0)];
  2222. };
  2223. return forward;
  2224. }
  2225. function conicEqualAreaRaw(y0, y1) {
  2226. var sy0 = sin(y0), n = (sy0 + sin(y1)) / 2;
  2227. // Are the parallels symmetrical around the Equator?
  2228. if (abs(n) < epsilon) return cylindricalEqualAreaRaw(y0);
  2229. var c = 1 + sy0 * (2 * n - sy0), r0 = sqrt(c) / n;
  2230. function project(x, y) {
  2231. var r = sqrt(c - 2 * n * sin(y)) / n;
  2232. return [r * sin(x *= n), r0 - r * cos(x)];
  2233. }
  2234. project.invert = function(x, y) {
  2235. var r0y = r0 - y;
  2236. return [atan2(x, abs(r0y)) / n * sign(r0y), asin((c - (x * x + r0y * r0y) * n * n) / (2 * n))];
  2237. };
  2238. return project;
  2239. }
  2240. function conicEqualArea() {
  2241. return conicProjection(conicEqualAreaRaw)
  2242. .scale(155.424)
  2243. .center([0, 33.6442]);
  2244. }
  2245. function albers() {
  2246. return conicEqualArea()
  2247. .parallels([29.5, 45.5])
  2248. .scale(1070)
  2249. .translate([480, 250])
  2250. .rotate([96, 0])
  2251. .center([-0.6, 38.7]);
  2252. }
  2253. // The projections must have mutually exclusive clip regions on the sphere,
  2254. // as this will avoid emitting interleaving lines and polygons.
  2255. function multiplex(streams) {
  2256. var n = streams.length;
  2257. return {
  2258. point: function(x, y) { var i = -1; while (++i < n) streams[i].point(x, y); },
  2259. sphere: function() { var i = -1; while (++i < n) streams[i].sphere(); },
  2260. lineStart: function() { var i = -1; while (++i < n) streams[i].lineStart(); },
  2261. lineEnd: function() { var i = -1; while (++i < n) streams[i].lineEnd(); },
  2262. polygonStart: function() { var i = -1; while (++i < n) streams[i].polygonStart(); },
  2263. polygonEnd: function() { var i = -1; while (++i < n) streams[i].polygonEnd(); }
  2264. };
  2265. }
  2266. // A composite projection for the United States, configured by default for
  2267. // 960×500. The projection also works quite well at 960×600 if you change the
  2268. // scale to 1285 and adjust the translate accordingly. The set of standard
  2269. // parallels for each region comes from USGS, which is published here:
  2270. // http://egsc.usgs.gov/isb/pubs/MapProjections/projections.html#albers
  2271. function albersUsa() {
  2272. var cache,
  2273. cacheStream,
  2274. lower48 = albers(), lower48Point,
  2275. alaska = conicEqualArea().rotate([154, 0]).center([-2, 58.5]).parallels([55, 65]), alaskaPoint, // EPSG:3338
  2276. hawaii = conicEqualArea().rotate([157, 0]).center([-3, 19.9]).parallels([8, 18]), hawaiiPoint, // ESRI:102007
  2277. point, pointStream = {point: function(x, y) { point = [x, y]; }};
  2278. function albersUsa(coordinates) {
  2279. var x = coordinates[0], y = coordinates[1];
  2280. return point = null,
  2281. (lower48Point.point(x, y), point)
  2282. || (alaskaPoint.point(x, y), point)
  2283. || (hawaiiPoint.point(x, y), point);
  2284. }
  2285. albersUsa.invert = function(coordinates) {
  2286. var k = lower48.scale(),
  2287. t = lower48.translate(),
  2288. x = (coordinates[0] - t[0]) / k,
  2289. y = (coordinates[1] - t[1]) / k;
  2290. return (y >= 0.120 && y < 0.234 && x >= -0.425 && x < -0.214 ? alaska
  2291. : y >= 0.166 && y < 0.234 && x >= -0.214 && x < -0.115 ? hawaii
  2292. : lower48).invert(coordinates);
  2293. };
  2294. albersUsa.stream = function(stream) {
  2295. return cache && cacheStream === stream ? cache : cache = multiplex([lower48.stream(cacheStream = stream), alaska.stream(stream), hawaii.stream(stream)]);
  2296. };
  2297. albersUsa.precision = function(_) {
  2298. if (!arguments.length) return lower48.precision();
  2299. lower48.precision(_), alaska.precision(_), hawaii.precision(_);
  2300. return reset();
  2301. };
  2302. albersUsa.scale = function(_) {
  2303. if (!arguments.length) return lower48.scale();
  2304. lower48.scale(_), alaska.scale(_ * 0.35), hawaii.scale(_);
  2305. return albersUsa.translate(lower48.translate());
  2306. };
  2307. albersUsa.translate = function(_) {
  2308. if (!arguments.length) return lower48.translate();
  2309. var k = lower48.scale(), x = +_[0], y = +_[1];
  2310. lower48Point = lower48
  2311. .translate(_)
  2312. .clipExtent([[x - 0.455 * k, y - 0.238 * k], [x + 0.455 * k, y + 0.238 * k]])
  2313. .stream(pointStream);
  2314. alaskaPoint = alaska
  2315. .translate([x - 0.307 * k, y + 0.201 * k])
  2316. .clipExtent([[x - 0.425 * k + epsilon, y + 0.120 * k + epsilon], [x - 0.214 * k - epsilon, y + 0.234 * k - epsilon]])
  2317. .stream(pointStream);
  2318. hawaiiPoint = hawaii
  2319. .translate([x - 0.205 * k, y + 0.212 * k])
  2320. .clipExtent([[x - 0.214 * k + epsilon, y + 0.166 * k + epsilon], [x - 0.115 * k - epsilon, y + 0.234 * k - epsilon]])
  2321. .stream(pointStream);
  2322. return reset();
  2323. };
  2324. albersUsa.fitExtent = function(extent, object) {
  2325. return fitExtent(albersUsa, extent, object);
  2326. };
  2327. albersUsa.fitSize = function(size, object) {
  2328. return fitSize(albersUsa, size, object);
  2329. };
  2330. albersUsa.fitWidth = function(width, object) {
  2331. return fitWidth(albersUsa, width, object);
  2332. };
  2333. albersUsa.fitHeight = function(height, object) {
  2334. return fitHeight(albersUsa, height, object);
  2335. };
  2336. function reset() {
  2337. cache = cacheStream = null;
  2338. return albersUsa;
  2339. }
  2340. return albersUsa.scale(1070);
  2341. }
  2342. function azimuthalRaw(scale) {
  2343. return function(x, y) {
  2344. var cx = cos(x),
  2345. cy = cos(y),
  2346. k = scale(cx * cy);
  2347. return [
  2348. k * cy * sin(x),
  2349. k * sin(y)
  2350. ];
  2351. }
  2352. }
  2353. function azimuthalInvert(angle) {
  2354. return function(x, y) {
  2355. var z = sqrt(x * x + y * y),
  2356. c = angle(z),
  2357. sc = sin(c),
  2358. cc = cos(c);
  2359. return [
  2360. atan2(x * sc, z * cc),
  2361. asin(z && y * sc / z)
  2362. ];
  2363. }
  2364. }
  2365. var azimuthalEqualAreaRaw = azimuthalRaw(function(cxcy) {
  2366. return sqrt(2 / (1 + cxcy));
  2367. });
  2368. azimuthalEqualAreaRaw.invert = azimuthalInvert(function(z) {
  2369. return 2 * asin(z / 2);
  2370. });
  2371. function azimuthalEqualArea() {
  2372. return projection(azimuthalEqualAreaRaw)
  2373. .scale(124.75)
  2374. .clipAngle(180 - 1e-3);
  2375. }
  2376. var azimuthalEquidistantRaw = azimuthalRaw(function(c) {
  2377. return (c = acos(c)) && c / sin(c);
  2378. });
  2379. azimuthalEquidistantRaw.invert = azimuthalInvert(function(z) {
  2380. return z;
  2381. });
  2382. function azimuthalEquidistant() {
  2383. return projection(azimuthalEquidistantRaw)
  2384. .scale(79.4188)
  2385. .clipAngle(180 - 1e-3);
  2386. }
  2387. function mercatorRaw(lambda, phi) {
  2388. return [lambda, log(tan((halfPi + phi) / 2))];
  2389. }
  2390. mercatorRaw.invert = function(x, y) {
  2391. return [x, 2 * atan(exp(y)) - halfPi];
  2392. };
  2393. function mercator() {
  2394. return mercatorProjection(mercatorRaw)
  2395. .scale(961 / tau);
  2396. }
  2397. function mercatorProjection(project) {
  2398. var m = projection(project),
  2399. center = m.center,
  2400. scale = m.scale,
  2401. translate = m.translate,
  2402. clipExtent = m.clipExtent,
  2403. x0 = null, y0, x1, y1; // clip extent
  2404. m.scale = function(_) {
  2405. return arguments.length ? (scale(_), reclip()) : scale();
  2406. };
  2407. m.translate = function(_) {
  2408. return arguments.length ? (translate(_), reclip()) : translate();
  2409. };
  2410. m.center = function(_) {
  2411. return arguments.length ? (center(_), reclip()) : center();
  2412. };
  2413. m.clipExtent = function(_) {
  2414. return arguments.length ? ((_ == null ? x0 = y0 = x1 = y1 = null : (x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1])), reclip()) : x0 == null ? null : [[x0, y0], [x1, y1]];
  2415. };
  2416. function reclip() {
  2417. var k = pi * scale(),
  2418. t = m(rotation(m.rotate()).invert([0, 0]));
  2419. return clipExtent(x0 == null
  2420. ? [[t[0] - k, t[1] - k], [t[0] + k, t[1] + k]] : project === mercatorRaw
  2421. ? [[Math.max(t[0] - k, x0), y0], [Math.min(t[0] + k, x1), y1]]
  2422. : [[x0, Math.max(t[1] - k, y0)], [x1, Math.min(t[1] + k, y1)]]);
  2423. }
  2424. return reclip();
  2425. }
  2426. function tany(y) {
  2427. return tan((halfPi + y) / 2);
  2428. }
  2429. function conicConformalRaw(y0, y1) {
  2430. var cy0 = cos(y0),
  2431. n = y0 === y1 ? sin(y0) : log(cy0 / cos(y1)) / log(tany(y1) / tany(y0)),
  2432. f = cy0 * pow(tany(y0), n) / n;
  2433. if (!n) return mercatorRaw;
  2434. function project(x, y) {
  2435. if (f > 0) { if (y < -halfPi + epsilon) y = -halfPi + epsilon; }
  2436. else { if (y > halfPi - epsilon) y = halfPi - epsilon; }
  2437. var r = f / pow(tany(y), n);
  2438. return [r * sin(n * x), f - r * cos(n * x)];
  2439. }
  2440. project.invert = function(x, y) {
  2441. var fy = f - y, r = sign(n) * sqrt(x * x + fy * fy);
  2442. return [atan2(x, abs(fy)) / n * sign(fy), 2 * atan(pow(f / r, 1 / n)) - halfPi];
  2443. };
  2444. return project;
  2445. }
  2446. function conicConformal() {
  2447. return conicProjection(conicConformalRaw)
  2448. .scale(109.5)
  2449. .parallels([30, 30]);
  2450. }
  2451. function equirectangularRaw(lambda, phi) {
  2452. return [lambda, phi];
  2453. }
  2454. equirectangularRaw.invert = equirectangularRaw;
  2455. function equirectangular() {
  2456. return projection(equirectangularRaw)
  2457. .scale(152.63);
  2458. }
  2459. function conicEquidistantRaw(y0, y1) {
  2460. var cy0 = cos(y0),
  2461. n = y0 === y1 ? sin(y0) : (cy0 - cos(y1)) / (y1 - y0),
  2462. g = cy0 / n + y0;
  2463. if (abs(n) < epsilon) return equirectangularRaw;
  2464. function project(x, y) {
  2465. var gy = g - y, nx = n * x;
  2466. return [gy * sin(nx), g - gy * cos(nx)];
  2467. }
  2468. project.invert = function(x, y) {
  2469. var gy = g - y;
  2470. return [atan2(x, abs(gy)) / n * sign(gy), g - sign(n) * sqrt(x * x + gy * gy)];
  2471. };
  2472. return project;
  2473. }
  2474. function conicEquidistant() {
  2475. return conicProjection(conicEquidistantRaw)
  2476. .scale(131.154)
  2477. .center([0, 13.9389]);
  2478. }
  2479. var A1 = 1.340264,
  2480. A2 = -0.081106,
  2481. A3 = 0.000893,
  2482. A4 = 0.003796,
  2483. M = sqrt(3) / 2,
  2484. iterations = 12;
  2485. function equalEarthRaw(lambda, phi) {
  2486. var l = asin(M * sin(phi)), l2 = l * l, l6 = l2 * l2 * l2;
  2487. return [
  2488. lambda * cos(l) / (M * (A1 + 3 * A2 * l2 + l6 * (7 * A3 + 9 * A4 * l2))),
  2489. l * (A1 + A2 * l2 + l6 * (A3 + A4 * l2))
  2490. ];
  2491. }
  2492. equalEarthRaw.invert = function(x, y) {
  2493. var l = y, l2 = l * l, l6 = l2 * l2 * l2;
  2494. for (var i = 0, delta, fy, fpy; i < iterations; ++i) {
  2495. fy = l * (A1 + A2 * l2 + l6 * (A3 + A4 * l2)) - y;
  2496. fpy = A1 + 3 * A2 * l2 + l6 * (7 * A3 + 9 * A4 * l2);
  2497. l -= delta = fy / fpy, l2 = l * l, l6 = l2 * l2 * l2;
  2498. if (abs(delta) < epsilon2) break;
  2499. }
  2500. return [
  2501. M * x * (A1 + 3 * A2 * l2 + l6 * (7 * A3 + 9 * A4 * l2)) / cos(l),
  2502. asin(sin(l) / M)
  2503. ];
  2504. };
  2505. function equalEarth() {
  2506. return projection(equalEarthRaw)
  2507. .scale(177.158);
  2508. }
  2509. function gnomonicRaw(x, y) {
  2510. var cy = cos(y), k = cos(x) * cy;
  2511. return [cy * sin(x) / k, sin(y) / k];
  2512. }
  2513. gnomonicRaw.invert = azimuthalInvert(atan);
  2514. function gnomonic() {
  2515. return projection(gnomonicRaw)
  2516. .scale(144.049)
  2517. .clipAngle(60);
  2518. }
  2519. function scaleTranslate$1(kx, ky, tx, ty) {
  2520. return kx === 1 && ky === 1 && tx === 0 && ty === 0 ? identity : transformer({
  2521. point: function(x, y) {
  2522. this.stream.point(x * kx + tx, y * ky + ty);
  2523. }
  2524. });
  2525. }
  2526. function identity$1() {
  2527. var k = 1, tx = 0, ty = 0, sx = 1, sy = 1, transform$$1 = identity, // scale, translate and reflect
  2528. x0 = null, y0, x1, y1, // clip extent
  2529. postclip = identity,
  2530. cache,
  2531. cacheStream,
  2532. projection;
  2533. function reset() {
  2534. cache = cacheStream = null;
  2535. return projection;
  2536. }
  2537. return projection = {
  2538. stream: function(stream) {
  2539. return cache && cacheStream === stream ? cache : cache = transform$$1(postclip(cacheStream = stream));
  2540. },
  2541. postclip: function(_) {
  2542. return arguments.length ? (postclip = _, x0 = y0 = x1 = y1 = null, reset()) : postclip;
  2543. },
  2544. clipExtent: function(_) {
  2545. return arguments.length ? (postclip = _ == null ? (x0 = y0 = x1 = y1 = null, identity) : clipRectangle(x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1]), reset()) : x0 == null ? null : [[x0, y0], [x1, y1]];
  2546. },
  2547. scale: function(_) {
  2548. return arguments.length ? (transform$$1 = scaleTranslate$1((k = +_) * sx, k * sy, tx, ty), reset()) : k;
  2549. },
  2550. translate: function(_) {
  2551. return arguments.length ? (transform$$1 = scaleTranslate$1(k * sx, k * sy, tx = +_[0], ty = +_[1]), reset()) : [tx, ty];
  2552. },
  2553. reflectX: function(_) {
  2554. return arguments.length ? (transform$$1 = scaleTranslate$1(k * (sx = _ ? -1 : 1), k * sy, tx, ty), reset()) : sx < 0;
  2555. },
  2556. reflectY: function(_) {
  2557. return arguments.length ? (transform$$1 = scaleTranslate$1(k * sx, k * (sy = _ ? -1 : 1), tx, ty), reset()) : sy < 0;
  2558. },
  2559. fitExtent: function(extent, object) {
  2560. return fitExtent(projection, extent, object);
  2561. },
  2562. fitSize: function(size, object) {
  2563. return fitSize(projection, size, object);
  2564. },
  2565. fitWidth: function(width, object) {
  2566. return fitWidth(projection, width, object);
  2567. },
  2568. fitHeight: function(height, object) {
  2569. return fitHeight(projection, height, object);
  2570. }
  2571. };
  2572. }
  2573. function naturalEarth1Raw(lambda, phi) {
  2574. var phi2 = phi * phi, phi4 = phi2 * phi2;
  2575. return [
  2576. lambda * (0.8707 - 0.131979 * phi2 + phi4 * (-0.013791 + phi4 * (0.003971 * phi2 - 0.001529 * phi4))),
  2577. phi * (1.007226 + phi2 * (0.015085 + phi4 * (-0.044475 + 0.028874 * phi2 - 0.005916 * phi4)))
  2578. ];
  2579. }
  2580. naturalEarth1Raw.invert = function(x, y) {
  2581. var phi = y, i = 25, delta;
  2582. do {
  2583. var phi2 = phi * phi, phi4 = phi2 * phi2;
  2584. phi -= delta = (phi * (1.007226 + phi2 * (0.015085 + phi4 * (-0.044475 + 0.028874 * phi2 - 0.005916 * phi4))) - y) /
  2585. (1.007226 + phi2 * (0.015085 * 3 + phi4 * (-0.044475 * 7 + 0.028874 * 9 * phi2 - 0.005916 * 11 * phi4)));
  2586. } while (abs(delta) > epsilon && --i > 0);
  2587. return [
  2588. x / (0.8707 + (phi2 = phi * phi) * (-0.131979 + phi2 * (-0.013791 + phi2 * phi2 * phi2 * (0.003971 - 0.001529 * phi2)))),
  2589. phi
  2590. ];
  2591. };
  2592. function naturalEarth1() {
  2593. return projection(naturalEarth1Raw)
  2594. .scale(175.295);
  2595. }
  2596. function orthographicRaw(x, y) {
  2597. return [cos(y) * sin(x), sin(y)];
  2598. }
  2599. orthographicRaw.invert = azimuthalInvert(asin);
  2600. function orthographic() {
  2601. return projection(orthographicRaw)
  2602. .scale(249.5)
  2603. .clipAngle(90 + epsilon);
  2604. }
  2605. function stereographicRaw(x, y) {
  2606. var cy = cos(y), k = 1 + cos(x) * cy;
  2607. return [cy * sin(x) / k, sin(y) / k];
  2608. }
  2609. stereographicRaw.invert = azimuthalInvert(function(z) {
  2610. return 2 * atan(z);
  2611. });
  2612. function stereographic() {
  2613. return projection(stereographicRaw)
  2614. .scale(250)
  2615. .clipAngle(142);
  2616. }
  2617. function transverseMercatorRaw(lambda, phi) {
  2618. return [log(tan((halfPi + phi) / 2)), -lambda];
  2619. }
  2620. transverseMercatorRaw.invert = function(x, y) {
  2621. return [-y, 2 * atan(exp(x)) - halfPi];
  2622. };
  2623. function transverseMercator() {
  2624. var m = mercatorProjection(transverseMercatorRaw),
  2625. center = m.center,
  2626. rotate = m.rotate;
  2627. m.center = function(_) {
  2628. return arguments.length ? center([-_[1], _[0]]) : (_ = center(), [_[1], -_[0]]);
  2629. };
  2630. m.rotate = function(_) {
  2631. return arguments.length ? rotate([_[0], _[1], _.length > 2 ? _[2] + 90 : 90]) : (_ = rotate(), [_[0], _[1], _[2] - 90]);
  2632. };
  2633. return rotate([0, 0, 90])
  2634. .scale(159.155);
  2635. }
  2636. exports.geoArea = area;
  2637. exports.geoBounds = bounds;
  2638. exports.geoCentroid = centroid;
  2639. exports.geoCircle = circle;
  2640. exports.geoClipAntimeridian = clipAntimeridian;
  2641. exports.geoClipCircle = clipCircle;
  2642. exports.geoClipExtent = extent;
  2643. exports.geoClipRectangle = clipRectangle;
  2644. exports.geoContains = contains;
  2645. exports.geoDistance = distance;
  2646. exports.geoGraticule = graticule;
  2647. exports.geoGraticule10 = graticule10;
  2648. exports.geoInterpolate = interpolate;
  2649. exports.geoLength = length;
  2650. exports.geoPath = index;
  2651. exports.geoAlbers = albers;
  2652. exports.geoAlbersUsa = albersUsa;
  2653. exports.geoAzimuthalEqualArea = azimuthalEqualArea;
  2654. exports.geoAzimuthalEqualAreaRaw = azimuthalEqualAreaRaw;
  2655. exports.geoAzimuthalEquidistant = azimuthalEquidistant;
  2656. exports.geoAzimuthalEquidistantRaw = azimuthalEquidistantRaw;
  2657. exports.geoConicConformal = conicConformal;
  2658. exports.geoConicConformalRaw = conicConformalRaw;
  2659. exports.geoConicEqualArea = conicEqualArea;
  2660. exports.geoConicEqualAreaRaw = conicEqualAreaRaw;
  2661. exports.geoConicEquidistant = conicEquidistant;
  2662. exports.geoConicEquidistantRaw = conicEquidistantRaw;
  2663. exports.geoEqualEarth = equalEarth;
  2664. exports.geoEqualEarthRaw = equalEarthRaw;
  2665. exports.geoEquirectangular = equirectangular;
  2666. exports.geoEquirectangularRaw = equirectangularRaw;
  2667. exports.geoGnomonic = gnomonic;
  2668. exports.geoGnomonicRaw = gnomonicRaw;
  2669. exports.geoIdentity = identity$1;
  2670. exports.geoProjection = projection;
  2671. exports.geoProjectionMutator = projectionMutator;
  2672. exports.geoMercator = mercator;
  2673. exports.geoMercatorRaw = mercatorRaw;
  2674. exports.geoNaturalEarth1 = naturalEarth1;
  2675. exports.geoNaturalEarth1Raw = naturalEarth1Raw;
  2676. exports.geoOrthographic = orthographic;
  2677. exports.geoOrthographicRaw = orthographicRaw;
  2678. exports.geoStereographic = stereographic;
  2679. exports.geoStereographicRaw = stereographicRaw;
  2680. exports.geoTransverseMercator = transverseMercator;
  2681. exports.geoTransverseMercatorRaw = transverseMercatorRaw;
  2682. exports.geoRotation = rotation;
  2683. exports.geoStream = geoStream;
  2684. exports.geoTransform = transform;
  2685. Object.defineProperty(exports, '__esModule', { value: true });
  2686. })));