Guest User

Untitled

a guest
Aug 24th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. /**
  2. * Copyright 2015 Teralytics AG
  3. *
  4. * @author Kirill Zhuravlev <kirill.zhuravlev@teralytics.ch>
  5. *
  6. */
  7.  
  8. (function (factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. define(['leaflet', 'd3'], factory);
  11. } else if (typeof module === 'object' && module.exports) {
  12. module.exports = factory(require('leaflet', 'd3'));
  13. } else {
  14. factory(L, d3);
  15. }
  16. }(function (L, d3) {
  17.  
  18. // Check requirements
  19. if (typeof d3 == "undefined") {
  20. throw "D3 SVG Overlay for Leaflet requires D3 library loaded first";
  21. }
  22. if (typeof L == "undefined") {
  23. throw "D3 SVG Overlay for Leaflet requires Leaflet library loaded first";
  24. }
  25.  
  26. // Tiny stylesheet bundled here instead of a separate file
  27. if (L.version >= "1.0") {
  28. d3.select("head")
  29. .append("style").attr("type", "text/css")
  30. .text("g.d3-overlay *{pointer-events:visiblePainted;}");
  31. }
  32.  
  33. // Class definition
  34. L.D3SvgOverlay = (L.version < "1.0" ? L.Class : L.Layer).extend({
  35. includes: (L.version < "1.0" ? L.Mixin.Events : []),
  36.  
  37. _undef: function(a){ return typeof a == "undefined" },
  38.  
  39. _options: function (options) {
  40. if (this._undef(options)) {
  41. return this.options;
  42. }
  43. options.zoomHide = this._undef(options.zoomHide) ? false : options.zoomHide;
  44. options.zoomDraw = this._undef(options.zoomDraw) ? true : options.zoomDraw;
  45.  
  46. return this.options = options;
  47. },
  48.  
  49. _disableLeafletRounding: function(){
  50. this._leaflet_round = L.Point.prototype._round;
  51. L.Point.prototype._round = function(){ return this; };
  52. },
  53.  
  54. _enableLeafletRounding: function(){
  55. L.Point.prototype._round = this._leaflet_round;
  56. },
  57.  
  58. draw: function () {
  59. this._disableLeafletRounding();
  60. this._drawCallback(this.selection, this.projection, this.map.getZoom());
  61. this._enableLeafletRounding();
  62. },
  63.  
  64. initialize: function (drawCallback, options) { // (Function(selection, projection)), (Object)options
  65. this._options(options || {});
  66. this._drawCallback = drawCallback;
  67. },
  68.  
  69. // Handler for "viewreset"-like events, updates scale and shift after the animation
  70. _zoomChange: function (evt) {
  71. this._disableLeafletRounding();
  72. var newZoom = this._undef(evt.zoom) ? this.map._zoom : evt.zoom; // "viewreset" event in Leaflet has not zoom/center parameters like zoomanim
  73. this._zoomDiff = newZoom - this._zoom;
  74. this._scale = Math.pow(2, this._zoomDiff);
  75. this.projection.scale = this._scale;
  76. this._shift = this.map.latLngToLayerPoint(this._wgsOrigin)
  77. ._subtract(this._wgsInitialShift.multiplyBy(this._scale));
  78.  
  79. var shift = ["translate(", this._shift.x, ",", this._shift.y, ") "];
  80. var scale = ["scale(", this._scale, ",", this._scale,") "];
  81. this._rootGroup.attr("transform", shift.concat(scale).join(""));
  82.  
  83. if (this.options.zoomDraw) { this.draw() }
  84. this._enableLeafletRounding();
  85. },
  86.  
  87. onAdd: function (map) {
  88. this.map = map;
  89. var _layer = this;
  90.  
  91. // SVG element
  92. if (L.version < "1.0") {
  93. map._initPathRoot();
  94. this._svg = d3.select(map._panes.overlayPane)
  95. .select("svg");
  96. this._rootGroup = this._svg.append("g");
  97. } else {
  98. this._svg = L.svg();
  99. map.addLayer(this._svg);
  100. this._rootGroup = d3.select(this._svg._rootGroup).classed("d3-overlay", true);
  101. }
  102. this._rootGroup.classed("leaflet-zoom-hide", this.options.zoomHide);
  103. this.selection = this._rootGroup;
  104.  
  105. // Init shift/scale invariance helper values
  106. this._pixelOrigin = map.getPixelOrigin();
  107. this._wgsOrigin = L.latLng([0, 0]);
  108. this._wgsInitialShift = this.map.latLngToLayerPoint(this._wgsOrigin);
  109. this._zoom = this.map.getZoom();
  110. this._shift = L.point(0, 0);
  111. this._scale = 1;
  112.  
  113. // Create projection object
  114. this.projection = {
  115. latLngToLayerPoint: function (latLng, zoom) {
  116. zoom = _layer._undef(zoom) ? _layer._zoom : zoom;
  117. var projectedPoint = _layer.map.project(L.latLng(latLng), zoom)._round();
  118. return projectedPoint._subtract(_layer._pixelOrigin);
  119. },
  120. layerPointToLatLng: function (point, zoom) {
  121. zoom = _layer._undef(zoom) ? _layer._zoom : zoom;
  122. var projectedPoint = L.point(point).add(_layer._pixelOrigin);
  123. return _layer.map.unproject(projectedPoint, zoom);
  124. },
  125. unitsPerMeter: 256 * Math.pow(2, _layer._zoom) / 40075017,
  126. map: _layer.map,
  127. layer: _layer,
  128. scale: 1
  129. };
  130. this.projection._projectPoint = function(x, y) {
  131. var point = _layer.projection.latLngToLayerPoint(new L.LatLng(y, x));
  132. this.stream.point(point.x, point.y);
  133. };
  134. this.projection.pathFromGeojson =
  135. d3.geo.path().projection(d3.geo.transform({point: this.projection._projectPoint}));
  136.  
  137. // Compatibility with v.1
  138. this.projection.latLngToLayerFloatPoint = this.projection.latLngToLayerPoint;
  139. this.projection.getZoom = this.map.getZoom.bind(this.map);
  140. this.projection.getBounds = this.map.getBounds.bind(this.map);
  141. this.selection = this._rootGroup;
  142.  
  143. if (L.version < "1.0") map.on("viewreset", this._zoomChange, this);
  144.  
  145. // Initial draw
  146. this.draw();
  147. },
  148.  
  149. // Leaflet 1.0
  150. getEvents: function() { return {zoomend: this._zoomChange}; },
  151.  
  152. onRemove: function (map) {
  153. if (L.version < "1.0") {
  154. map.off("viewreset", this._zoomChange, this);
  155. this._rootGroup.remove();
  156. } else {
  157. this._svg.remove();
  158. }
  159. },
  160.  
  161. addTo: function (map) {
  162. map.addLayer(this);
  163. return this;
  164. }
  165.  
  166. });
  167.  
  168. L.D3SvgOverlay.version = "2.2";
  169.  
  170. // Factory method
  171. L.d3SvgOverlay = function (drawCallback, options) {
  172. return new L.D3SvgOverlay(drawCallback, options);
  173. };
  174.  
  175. }));
Add Comment
Please, Sign In to add comment