d3-geo.js 82 KB

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