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